function sendValuesToServlet(values) {
    //alert(values);
    
    var xmlhttp = InstanceXMLHttpRequest();
    xmlhttp.open('POST', 'GoogleAdSense', true);

    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    xmlhttp.onreadystatechange = function() {		
        if (xmlhttp.readyState == 4) {
            //alert(xmlhttp.responseText);
        }
    }
    xmlhttp.send(values);
}

function InstanceXMLHttpRequest() {
    if(window.XMLHttpRequest) {
        try {
            req = new XMLHttpRequest();
        } catch(e) {
            req = false;
        }
        // branch for IE/Windows ActiveX version
    } else if(window.ActiveXObject) {
        try {
            req = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                req = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                req = false;
            }
        }
    }
    return req;
}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

String.prototype.trim = function (){
	return this.replace(/(^\s+)/g, "").replace(/(\s+$)/g, "");
}

function checkPostCode(toCheck) {

  // Permitted letters depend upon their position in the postcode.
  var alpha1 = "[abcdefghijklmnoprstuwyz]";                       // Character 1
  var alpha2 = "[abcdefghklmnopqrstuvwxy]";                       // Character 2
  var alpha3 = "[abcdefghjkstuw]";                                // Character 3
  var alpha4 = "[abehmnprvwxy]";                                  // Character 4
  var alpha5 = "[abdefghjlnpqrstuwxyz]";                          // Character 5
  
  // Array holds the regular expressions for the valid postcodes
  var pcexp = new Array ();

  // Expression for postcodes: AN NAA, ANN NAA, AAN NAA, and AANN NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1,2})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Expression for postcodes: ANA NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}[0-9]{1}" + alpha3 + "{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));

  // Expression for postcodes: AANA  NAA
  pcexp.push (new RegExp ("^(" + alpha1 + "{1}" + alpha2 + "?[0-9]{1}" + alpha4 +"{1})(\\s*)([0-9]{1}" + alpha5 + "{2})$","i"));
  
  // Exception for the special postcode GIR 0AA
  pcexp.push (/^(GIR)(\s*)(0AA)$/i);
  
  // Standard BFPO numbers
  pcexp.push (/^(bfpo)(\s*)([0-9]{1,4})$/i);
  
  // c/o BFPO numbers
  pcexp.push (/^(bfpo)(\s*)(c\/o\s*[0-9]{1,3})$/i);
  
  // Overseas Territories
  pcexp.push (/^([A-Z]{4})(\s*)(1ZZ)$/i);

  // Load up the string to check
  var postCode = toCheck;

  // Assume we're not going to find a valid postcode
  var valid = false;
  
  // Check the string against the types of post codes
  for ( var i=0; i<pcexp.length; i++) {
    if (pcexp[i].test(postCode)) {
    
      // The post code is valid - split the post code into component parts
      pcexp[i].exec(postCode);
      
      // Copy it back into the original string, converting it to uppercase and
      // inserting a space between the inward and outward codes
      postCode = RegExp.$1.toUpperCase() + " " + RegExp.$3.toUpperCase();
      
      // If it is a BFPO c/o type postcode, tidy up the "c/o" part
      postCode = postCode.replace (/C\/O\s*/,"c/o ");
      
      // Load new postcode back into the form element
      valid = true;
      
      // Remember that we have found that the code is valid and break from loop
      break;
    }
  }
  
  // Return with either the reformatted valid postcode or the original invalid 
  // postcode
  if (valid) {return postCode;} else return false;
}

function isValidDate(dateStr, format) {
   if (format == null) { format = "MDY"; }
   format = format.toUpperCase();
   if (format.length != 3) { format = "MDY"; }
   if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || (format.indexOf("Y") == -1) ) { format = "MDY"; }
   if (format.substring(0, 1) == "Y") { // If the year is first
      var reg1 = /^\d{4}(\-)\d{1,2}\1\d{1,2}$/

   } else if (format.substring(1, 2) == "Y") { // If the year is second
      var reg1 = /^\d{1,2}(\-)\d{4}\1\d{1,2}$/

   } else { // The year must be third
      var reg1 = /^\d{1,2}(\-)\d{1,2}\1\d{4}$/

   }
   if ( reg1.test(dateStr) == false ) { return false; }
   var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
   // Check to see if the 3 parts end up making a valid date
   if (format.substring(0, 1) == "M") { var mm = parts[0]; } else 
      if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
   if (format.substring(0, 1) == "D") { var dd = parts[0]; } else
      if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
   if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else
      if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
   if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
   if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
   var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
   if (parseFloat(dd) != dt.getDate()) { return false; }
   if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
   return true;
}

function isValidEmail(email) {
    var re = /^ *([a-z0-9_-]+\.)*[a-z0-9_-]+@(([a-z0-9-]+\.)+(com|net|org|mil|edu|gov|arpa|info|biz|inc|name|[a-z]{2})|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}) *$/;
    return (re.test(email.toLowerCase()));
}

function traceCookies() {
    var trace = ""
    var ca = document.cookie.split(';');
    for (var i=0; i < ca.length; i++) {
	var c = ca[i];
	while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
	trace += c + "\n";
	
    }
    alert(trace);
}

function readCookie(name) {
    var ca = document.cookie.split(';');
    var nameEQ = name + "=";
    for (var i=0; i < ca.length; i++) {
	var c = ca[i];
	while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
	if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}


function createCookie(name, value, days) {
    //alert("create cookie: " + name + "=" + value + " (" + days + ")");
    if (days) {
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
    } else {
	var expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/";
}


function eraseCookie(name) {
    createCookie(name, "", -1);
}

function saveForm(formname) {
    for (i = 0; i < document.forms[formname].length; i++) {
	var name = document.forms[formname][i].name;
	var type = document.forms[formname][i].type;
	var value = "";
	if (type == "checkbox") {
	    value = document.forms[formname][i].checked;
	    createCookie(name,value,365*100);
	} else if (type == "radio") {
	    if (document.forms[formname][i].checked) {
		    value = document.forms[formname][i].value;
		    createCookie(name,value,365*100);
	    }
	} else if (type == "select-one") {
	    value = document.forms[formname][i].selectedIndex;
	    createCookie(name,value,365*100);
	    
	} else if ((type == "button") || (type == "submit")) {
	    // ignore
	    
	} else {
	    value = document.forms[formname][i].value;
	    createCookie(name,value,365*100);
	}
	
	//alert(name + "=" + value + " (" + type + ")");
    }	
}

function restoreForm(formname) {
    for (i = 0; i < document.forms[formname].length-1; i++) {
	var name = document.forms[formname][i].name;
	var type = document.forms[formname][i].type;
	var value = readCookie(name);
	
	//alert(name + "=" + value + " (" + type + ")");
	
	if (type == "checkbox") {
	    document.forms[formname][i].checked = value == "true";
	    
	} else if (type == "radio") {
	    if (document.forms[formname][i].value == value) {
		    document.forms[formname][i].checked = true;
	    }
	} else if (type == "select-one") {
	    document.forms[formname][i].selectedIndex = value;
	
	} else if ((type == "button") || (type == "submit")) {
	    // ignore
	    
	} else {
	    document.forms[formname][i].value = value;
	}
    }
}

function clearForm(formname) {
    for (i = 0; i < document.forms[formname].length-1; i++) {
	var name = document.forms[formname][i].name;
	eraseCookie(name);
    }
}

function changeClassName(name, className){
    var obj = document.getElementById(name);
    obj.className = className;
}


function writeLayer(name, txt){
    var layer = document.getElementById(name);
    layer.innerHTML = txt;
}

function displayLayer(name, visible){
    var foc = document.getElementById(name);
    if (foc) {
	if (visible) {
	    foc.style.display='block';
	} else {
	    foc.style.display='none';
	}
    }
}

function toggleLayer(name){
    var foc = document.getElementById(name);
    if (foc) {
	if (foc.style.display=='block') {
	    foc.style.display='none';
	} else {
	    foc.style.display='block';
	}
    }
}

function setStyle(name, property, value){
    var foc = document.getElementById(name);
    if (foc) {
	foc.style[property] = value;
    }
}
