function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;

  while (i < clen) {
    var j = i + alen;

   if (document.cookie.substring(i, j) == arg)
       return getCookieVal (j);

      i = document.cookie.indexOf(" ", i) + 1;

 if (i == 0) break;

}

return null;

}

// open window
function openWindow(theURL) {

	window.open(theURL,'Popup','scrollbars=yes,width=500,height=500');

	return false;

}

// open ssl window
function openSSL(theURL){

	window.open(theURL,'Popup','scrollbars=yes,width=430,height=580');
	//alert(theURL)

}


//Show / hide div
function show_hide(elementid) {

	// show the requested div

	if (document.getElementById(elementid).style.display == '')

		{

		document.getElementById(elementid).style.display = 'none';

		}

	else

		{

		document.getElementById(elementid).style.display = '';

		}

	return false;

}

function hideDiv(thisdiv) {

	document.getElementById(thisdiv).style.display = 'none';

}




function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");

}

function DeleteCookie (name) {
  var exp = new Date();
  exp.setTime (exp.getTime() - 1);
  var cval = GetCookie (name);
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();

}


function amt(){
  var count = GetCookie('count');

  if(count == null) {
    SetCookie('count','1');
    return 1;
} else {
   var newcount = parseInt(count) + 1;
   DeleteCookie('count');
   SetCookie('count',newcount,exp);
return count

}

}

function getCookieVal(offset) {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
     endstr = document.cookie.length;
     return unescape(document.cookie.substring(offset, endstr));
}



function checkCount() {
  var count = GetCookie('count');
  if (count == null) {
    count=1;
    SetCookie('count', count, exp);
    window.open(page, "", windowprops);
  } else {
    count++;
    SetCookie('count', count, exp);
  }

}

function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function checkDate(strDate,strFormat){
    if(strDate.length>0){
    	var dateregex=/^[ ]*[0]?(\d{1,2})\/(\d{1,2})\/(\d{4,})[ ]*$/;

        var match=strDate.match(dateregex);
        if (match){
        	switch (strFormat) {
				case 'MDY':
					var tmpdate=new Date(match[3],parseInt(match[1],10)-1,match[2]);
					if (tmpdate.getDate()==parseInt(match[2],10) && tmpdate.getFullYear()==parseInt(match[3],10) && (tmpdate.getMonth()+1)==parseInt(match[1],10)){
						return true;
					}
					break;
				case 'DMY':
					var tmpdate=new Date(match[3],parseInt(match[1],10)-1,match[2]);
					if (tmpdate.getDate()==parseInt(match[1],10) && tmpdate.getFullYear()==parseInt(match[3],10) && (tmpdate.getMonth()+1)==parseInt(match[2],10)){
						return true;
					}
					break;
				default:
					return false;
			}
    	}
    	alert('no match on ' + strDate + '\n' + match[1]);
        return false;
    }
    else{
        return true;
    }
}

