function stringToDate(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);
   
   return dt;
}

function isValidDate3(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 isValidDate2(dateStr, format) {
   return isValidDate3(dateStr, format); // Lazy backwards compatibility
   
   /*
   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 isAgeCorrect(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);
   var now = new Date();
   var diff = (now - dt) / (1000 * 60 * 60 * 24 * 365);
   if ((diff < 18) || (diff > 70)) {
	return false;
   }
   return true;
}

function checkRecord() {
	// do form validation here
	var firstname = document.forms.formaccount.first_names.value;
	var surname = document.forms.formaccount.last_name.value;
	//var landline = document.forms.formaccount.home_telephone.value;
	//var mobile = document.forms.formaccount.mobile_telephone.value;
	var dob = document.forms.formaccount.dob.value;
	
	var errors = "";
	
	if (firstname == "") {
		errors += "First Names\n";
	}
	if (surname == "") {
		errors += "Last Name\n";
	}
	//if (landline == "") {
	//	errors += "Home Telephone\n";
	//}
	//if (mobile == "") {
	//	errors += "Mobile Telephone\n";
	//}
	if (!isValidDate2(dob,"DMY")) {
		errors += "DOB\n";
		
	} else if (!isAgeCorrect(dob,"DMY")) {
		errors += "You have to be between 18 and 70 years old\n";	
	}
	
	if (document.forms.formaccount['group_insurance_beneficiary_firstname_1']) {
		var total_percentaje = 0;
		for (i=1; i <= 8; i++) {
			var beneficiary_firstname = document.forms.formaccount['group_insurance_beneficiary_firstname_' + i].value;
			var beneficiary_lastname = document.forms.formaccount['group_insurance_beneficiary_lastname_' + i].value;
			var beneficiary_relationship = document.forms.formaccount['group_insurance_beneficiary_relationship_' + i].value;
			var beneficiary_pertentage = document.forms.formaccount['group_insurance_beneficiary_percentage_' + i].value;
			
			if ((beneficiary_pertentage != "") && isNaN(beneficiary_pertentage)) {
				errors += "Beneficiariare percentage " + i + " has to be a number\n";
				
			} else if ((beneficiary_pertentage != "") && !isNaN(beneficiary_pertentage)) {
				
				total_percentaje += parseFloat(beneficiary_pertentage);
				
				if (beneficiary_firstname == "") {
					errors += "Beneficiariare first name " + i + "\n";
				}
				
				if (beneficiary_lastname == "") {
					errors += "Beneficiariare last name " + i + "\n";
				}

				if (beneficiary_relationship == "") {
					errors += "Beneficiariare relationship " + i + "\n";
				}
				
			} else {
				if ((beneficiary_firstname == "") && ((beneficiary_relationship != "") || (beneficiary_lastname != ""))) {
					errors += "Beneficiariare first name " + i + "\n";
				}
				
				if ((beneficiary_lastname == "") && ((beneficiary_relationship != "") || (beneficiary_firstname != ""))) {
					errors += "Beneficiariare last name " + i + "\n";
				}
				
				if ((beneficiary_relationship == "") && ((beneficiary_firstname != "") || (beneficiary_firstname != ""))) {
					errors += "Beneficiariare relationship " + i + "\n";
				}
				
				if ((beneficiary_firstname != "") || (beneficiary_lastname != "") || (beneficiary_relationship != "")) {
					errors += "Beneficiariare percentage " + i + "\n";
				}
			}
		}
		
		if (total_percentaje > 100) {
			errors += "Total percentage of beneficiariares cannot be greater than 100\n";
		}
	}
	
        if (document.forms.formaccount['group_insurance_beneficiary_spouse_firstname_1']) {
		var total_percentaje = 0;
		for (i=1; i <= 8; i++) {
			var beneficiary_firstname = document.forms.formaccount['group_insurance_beneficiary_spouse_firstname_' + i].value;
			var beneficiary_lastname = document.forms.formaccount['group_insurance_beneficiary_spouse_lastname_' + i].value;
			var beneficiary_relationship = document.forms.formaccount['group_insurance_beneficiary_spouse_relationship_' + i].value;
			var beneficiary_pertentage = document.forms.formaccount['group_insurance_beneficiary_spouse_percentage_' + i].value;
			
			if ((beneficiary_pertentage != "") && isNaN(beneficiary_pertentage)) {
				errors += "Beneficiariare percentage " + i + " has to be a number\n";
				
			} else if ((beneficiary_pertentage != "") && !isNaN(beneficiary_pertentage)) {
				
				total_percentaje += parseFloat(beneficiary_pertentage);
				
				if (beneficiary_firstname == "") {
					errors += "Beneficiariare first name " + i + "\n";
				}
				
				if (beneficiary_lastname == "") {
					errors += "Beneficiariare last name " + i + "\n";
				}

				if (beneficiary_relationship == "") {
					errors += "Beneficiariare relationship " + i + "\n";
				}
				
			} else {
				if ((beneficiary_firstname == "") && ((beneficiary_relationship != "") || (beneficiary_lastname != ""))) {
					errors += "Beneficiariare first name " + i + "\n";
				}
				
				if ((beneficiary_lastname == "") && ((beneficiary_relationship != "") || (beneficiary_firstname != ""))) {
					errors += "Beneficiariare last name " + i + "\n";
				}
				
				if ((beneficiary_relationship == "") && ((beneficiary_firstname != "") || (beneficiary_firstname != ""))) {
					errors += "Beneficiariare relationship " + i + "\n";
				}
				
				if ((beneficiary_firstname != "") || (beneficiary_lastname != "") || (beneficiary_relationship != "")) {
					errors += "Beneficiariare percentage " + i + "\n";
				}
			}
		}
		
		if (total_percentaje > 100) {
			errors += "Total percentage of beneficiariares cannot be greater than 100\n";
		}
	}
        
	if (errors == "") {
		document.forms.formaccount.submit();
	} else {
		alert("Please review the following information:\n\n" + errors);
	}
}

function openInNew(page)
{
	url = page;
	var width  = 750;
	var height = 700;
	var left   = (screen.width  - width)/2;
	var top    = (screen.height - height)/2;
	var params = 'width='+width+', height='+height;
	params += ', top='+top+', left='+left;
	params += ', directories=no';
	params += ', location=no';
	params += ', menubar=no';
	params += ', resizable=yes';
	params += ', scrollbars=yes';
	params += ', status=no';
	params += ', toolbar=no';
	newwin=window.open(url,'pdf_holder',params);
	if (window.focus) {newwin.focus()}
	return false;
}

function backForm(params) {
	location.href='/members/forms-and-documents' + params;
}

function redirectForm(url) {
	document.forms.form_fields.target = "";
	document.forms.form_fields.action = url;
	document.forms.form_fields.submit();
}

function sendForm() {
	if (document.forms.form_fields.needsValidation.value == "yes") {
		if (validateForm()) {
			document.forms.form_fields.target = "";
			document.forms.form_fields.submit();
		}
	} else {
		document.forms.form_fields.target = "";
		document.forms.form_fields.submit();
	}
}

function previewForm() {
	openInNew("about:blank");
	document.forms.form_fields.target = "pdf_holder";
}

function checkLoginPressCentre() {
	// do form validation here
	var email = document.forms.formlogin.member_email.value;
	var password = document.forms.formlogin.member_password.value;
	
	var errors = "";

	if (!isValidEmail(email)) {
		errors += "Email address\n";
	}
	if (password == "") {
		errors += "Password\n";
	}
	
	if (errors == "") {
		document.forms.formlogin.submit();
	} else {
		alert("Please review the following information:\n\n" + errors);
	}
}

function checkRegisterPressCentre() {
	// do form validation here
	var surname = document.forms.formregister.member_lastname.value;
	var email = document.forms.formregister.member_email.value;
	var telephone = document.forms.formregister.member_phone.value;
        var password = document.forms.formregister.member_password.value;
	var confirm = document.forms.formregister.confirm.value;
        
	var errors = "";
	
	if (surname == "") {
		errors += "Surname\n";
	}
	if (!isValidEmail(email)) {
		errors += "Email address\n";
	}
        if (password != "") {
               if (!isValidUsernamePassword(password)) {
                       errors += "Password (must be alphanumeric with no spaces)\n";
               }
               if (password != confirm) {
                       errors += "Password confirmation\n";
               }
        } else {
               errors += "Password\n";
        }
	if (telephone == "") {
		errors += "Telephone\n";
	}
	
	if (errors == "") {
		document.forms.formregister.submit();
	} else {
		alert("Please review the following information:\n\n" + errors);
	}
}

function checkResponse() {
	var errors = "";
	
	//if (document.forms["formblog"]["response_title"].value == "") {
	//	errors += "Title\n";
	//}
	if (document.forms["formblog"]["response_text"].value == "") {
		errors += "Message\n";
	}
	
	if (errors == "") {
		//return true;
		document.forms["formblog"].submit();
	} else {
		alert("Please review the following information:\n\n" + errors);
		//return false;
	}
}

function checkPost() {
	var errors = "";
	
	if (document.forms["formblog"]["blog_title"].value == "") {
		errors += "Title\n";
	}
	if (document.forms["formblog"]["blog_text"].value == "") {
		errors += "Message\n";
	}
	
	if (errors == "") {
		//return true;
		document.forms["formblog"].submit();
	} else {
		alert("Please review the following information:\n\n" + errors);
		//return false;
	}
}

function checkAccount() {
	// do form validation here
	var password = document.forms.formaccount.member_password.value;
	var confirm = document.forms.formaccount.confirm.value;
	var firstname = document.forms.formaccount.member_firstname.value;
	var surname = document.forms.formaccount.member_lastname.value;
	var email = document.forms.formaccount.member_email.value;
	var telephone = document.forms.formaccount.member_phone.value;
	var dob = document.forms.formaccount.member_dob.value;
	
	var errors = "";
	
	if (password != "") {
		if (!isValidUsernamePassword(password)) {
			errors += "Password (must be alphanumeric with no spaces)\n";
		}
		if (password != confirm) {
			errors += "Password confirmation\n";
		}
	}
	if (firstname == "") {
		errors += "Firstname\n";
	}
	if (surname == "") {
		errors += "Surname\n";
	}
	if (!isValidEmail(email)) {
		errors += "Email address\n";
	}
	if (telephone == "") {
		errors += "Telephone\n";
	}
	if (!isValidDate(dob,"DMY")) {
		errors += "DOB\n";
	}
	
	if (errors == "") {
		document.forms.formaccount.submit();
	} else {
		alert("Please review the following information:\n\n" + errors);
	}
}

function checkReminder() {
	var errors = "";
	
	if (document.forms["formreminder"]["member_warrant"].value == "") {
		errors += "Warrant Number\n";
	}
	if (document.forms["formreminder"]["member_lastname"].value == "") {
		errors += "Surname\n";
	}
	
	if (errors == "") {
		//return true;
		document.forms["formreminder"].submit();
	} else {
		alert("Please review the following information:\n\n" + errors);
		//return false;
	}
}

function checkLogin() {
	var errors = "";
	
	if (document.forms["formlogin"]["member_warrant"].value == "") {
		errors += "Warrant Number\n";
	}
	if (document.forms["formlogin"]["member_lastname"].value == "") {
		errors += "Surname\n";
	}
	if (document.forms["formlogin"]["member_password"].value == "") {
		errors += "Password\n";
	}
	
	if (errors == "") {
		//return true;
		document.forms["formlogin"].submit();
	} else {
		alert("Please review the following information:\n\n" + errors);
		//return false;
	}
}

function checkContact()
{
	var errors = "";
		
	if (document.forms["formcontact"]["firstname"].value == "") {
		errors += "First name\n";
	}
	if (document.forms["formcontact"]["surname"].value == "") {
		errors += "Surname\n";
	}
	if (!isValidEmail(document.forms["formcontact"]["email"].value)) {
		errors += "Email\n";
	}
	if (document.forms["formcontact"]["comments"].value == "") {
		errors += "Comments,Feedback,Questions\n";
	}
	
	if (errors == "") {
		//return true;
		document.forms["formcontact"].submit();
	} else {
		alert("Please review the following information:\n\n" + errors);
		//return false;
	}
	
}

function popup(w,h,t,l,url,winName,status,toolBar,scrollBars,resizable) {
	if (scrollBars == undefined) scrollBars = "auto";
	if (resizable == undefined) resizable = "no";
  	var sWid = screen.width;
  	var sHi = screen.height;
	var wid = w;
	var hi = h;
	if ((t == 0) && (l == 0)) {
		var tp = (sHi/2)-(hi/2);
		var lft = (sWid/2)-(wid/2);
	} else {
		var tp = t;
		var lft = l;	
	}
	if (scroll && document.all && (navigator.userAgent.indexOf("Mac") > -1)) wid = wid+17;
	newwin=window.open(url,winName,"width=" + wid + ",height=" + hi + ",status=" + status + ",scrollbars=" + scrollBars + ",toolbar=" + toolBar + ",resizable=" + resizable + ", top = "+ tp +", left ="+ lft + ", screenX=" + lft +", screenY= "+tp);
	newwin.focus();
}

function checkRegister(validate) {
	// do form validation here
	var warrant = document.forms.formregister.member_warrant.value;
	var password = document.forms.formregister.member_password.value;
	var confirm = document.forms.formregister.confirm.value;
	var firstname = document.forms.formregister.member_firstname.value;
	var surname = document.forms.formregister.member_lastname.value;
	var email = document.forms.formregister.member_email.value;
	var telephone = document.forms.formregister.member_phone.value;
	//var dob = document.forms.formregister.member_dob.value;
        var dob = document.forms.formregister.member_dob_day.value + "-" + document.forms.formregister.member_dob_month.value + "-" + document.forms.formregister.member_dob_year.value;
	
        displayLayer('member_warrant_error',false);
        displayLayer('member_password_error',false);
        displayLayer('confirm_error',false);
        displayLayer('member_firstname_error',false);
        displayLayer('member_lastname_error',false);
        displayLayer('member_email_error',false);
        displayLayer('member_phone_error',false);
        displayLayer('member_dob_error',false);
        
	if (validate == "warrant") {
		if ((warrant != "") && (!isNaN(warrant))) {
			existWarrantNumber(warrant);
		} else {
                        displayLayer('member_warrant_error',true);
			alert("Please enter a correct Warrant Number");
		} 
	} else if ((validate == "validwarrant") && (surname != "") && (dob != "")) {
		if (!isValidDate(dob,"DMY")) {
                        displayLayer('member_dob_error',true);
			alert("Please enter a correct DOB");
		} else {
			validWarrantNumber(warrant,surname,dob);
		}
		
	/*
	} else if (validate == "username") {
		if ((username != "") && (isValidUsernamePassword(username))) {
			existUsername(username);
		} else {
			alert("Please enter a correct Username (must be alphanumeric with no spaces)");
		} 
	*/
	} else {
		var errors = "";
		
		if (password != "") {
			if (!isValidUsernamePassword(password)) {
				errors += "Password (must be alphanumeric with no spaces)\n";
                                displayLayer('member_password_error',true);
			}
			if (password != confirm) {
				errors += "Password confirmation\n";
                                displayLayer('confirmation_error',true);
			}
		} else {
			errors += "Password\n";
                        displayLayer('member_password_error',true);
		}
		if (firstname == "") {
			errors += "Firstname\n";
                        displayLayer('member_firstname_error',true);
		}
		if (surname == "") {
			errors += "Surname\n";
                        displayLayer('member_lastname_error',true);
		}
		if (!isValidEmail(email)) {
			errors += "Email address\n";
                        displayLayer('member_email_error',true);
		}
		if (telephone == "") {
			errors += "Telephone\n";
                        displayLayer('member_phone_error',true);
		}
		if (!isValidDate(dob,"DMY")) {
			errors += "DOB\n";
                        displayLayer('member_dob_error',true);
		}
		
		if (errors == "") {
			document.forms.formregister.submit();
		} else {
			alert("Please review the following information:\n\n" + errors);
		}
	}
}

function existUsername(username) {
	var xmlhttp = InstanceXMLHttpRequest();
	xmlhttp.open('POST', '/ajax?method=existusername', true);

	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	xmlhttp.onreadystatechange = function() {		
		if (xmlhttp.readyState == 4) {
			//alert(xmlhttp.responseText);
			var result = parseInt(xmlhttp.responseText);
			if (!isNaN(result) && (result > 0)) {
				alert("Username already exists in the database, please try a different one.");
			} else {
				checkRegister("");
			}			
		}
	}
	xmlhttp.send('username=' + username);
}

function existWarrantNumber(warrant) {
	var xmlhttp = InstanceXMLHttpRequest();
	xmlhttp.open('POST', '/ajax?method=existwarrantnumber', true);

	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	xmlhttp.onreadystatechange = function() {		
		if (xmlhttp.readyState == 4) {
			//alert(xmlhttp.responseText);
			var result = parseInt(xmlhttp.responseText);
			if (!isNaN(result) && (result > 0)) {
                                 displayLayer('member_warrant_error',true);
                                 alert("Warrant Number already exists in the database, please try a different one.");
                                
			} else {
				//checkRegister("username");
				checkRegister("validwarrant");
			}			
		}
	}
	xmlhttp.send('warrant=' + warrant);
}

function validWarrantNumber(warrant,surname,dob) {
	var xmlhttp = InstanceXMLHttpRequest();
	xmlhttp.open('POST', '/ajax?method=validwarrantnumber', true);

	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	xmlhttp.onreadystatechange = function() {		
		if (xmlhttp.readyState == 4) {
			//alert(xmlhttp.responseText);
			var result = xmlhttp.responseText;
			if (result != "") {
				document.forms.formregister.constable_dob.value = result;
				checkRegister("");
			} else {
                                 displayLayer('member_warrant_error',true);
                                 foundWarrantNumber(warrant);
			}			
		}
	}
	xmlhttp.send('warrant=' + warrant + '&surname=' + surname + '&dob=' + dob);
}

function foundWarrantNumber(warrant) {
	var xmlhttp = InstanceXMLHttpRequest();
	xmlhttp.open('POST', '/ajax?method=foundwarrantnumber', true);

	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	xmlhttp.onreadystatechange = function() {		
		if (xmlhttp.readyState == 4) {
			//alert(xmlhttp.responseText);
			var result = xmlhttp.responseText;
			if (result != "") {
                                 displayLayer('member_lastname_error',true);
				 alert("Warrant Number found but incorrect Surname.");
			} else {
				 alert("Please check all fields are completed correctly.");
			}		
		}
	}
	xmlhttp.send('warrant=' + warrant);
}

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 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}) *$/;
	var re = /^[\w\+\\'\.-]+@[\w\\'\.-]+\.[a-zA-Z]{2,}$/;
	return (re.test(email.toLowerCase()));
}

function isValidUsernamePassword(value) {
	re = /^\w+$/;
	return (re.test(value.toLowerCase()));
}

String.prototype.trim = function (){
	return this.replace(/(^\s+)/g, "").replace(/(\s+$)/g, "");
}

function diffDates(dateStr, checkDate, format) {
   if (isValidDate(dateStr, format)) {
	// date format is ok	
	if (format == null) { format = "MDY"; }
	format = format.toUpperCase();
	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);
	return (checkDate.getTime() - (checkDate.getTimezoneOffset()*60*1000)) - (dt.getTime() - (dt.getTimezoneOffset()*60*1000));
        //return (checkDate.getTime() - dt.getTime());

   } else {
	return false;
   }
}

function isValidDate(dateStr, format) {
   return isValidDate3(dateStr, format); // Lazy backwards compatibility
   
   /*
   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 changeClassName(name, className){
	var obj = document.getElementById(name);
	obj.className = className;
}


function displayLayer(name, visible){
	var foc = document.getElementById(name);
	if (foc) {
		if (visible) {
			foc.style.display='block';
		} else {
			foc.style.display='none';
		}
	}
}

function writeLayer(name, txt){
	var layer = document.getElementById(name);
	layer.innerHTML = txt;
}

function setStyle(name, property, value){
	var foc = document.getElementById(name);
	if (foc) {
		foc.style[property] = value;
	}
}
