// Survey Personal Info Registration Form JScript source code
var isPopUp = true;

//******************************************************************
// Validates content of fields
//******************************************************************
function checkfield(fieldlabel,fieldname,acceptonly){
var s= new String();
	characters = new RegExp ("[^A-Za-zUOAuoa ]","i");
	numbers = new RegExp ("[^0-9]","i");
	numchar = new RegExp ("[^0-9-^A-Za-z ]","i");

	s = document.form1[fieldname].value;
	result = s.search(eval(acceptonly));
	if (result != -1){
		if (acceptonly == "numchar")
		{
			msg = fieldlabel + " can only contain numbers and characters" + ".";
		}
		else if  (acceptonly == "numbers")
		{
			msg = fieldlabel + " can only contain numbers" + ".";
		}
		else
		{
			msg = fieldlabel + " can only contain characters" + ".";
		}
		document.form1[fieldname].value = ""
		alert(msg);
		document.form1[fieldname].focus();
		return false;
	}
}
//----------------------------------------------------------	
function isFIrstNameOK(){
	var field_val=document.form1.first_name.value.replace(/[ ]/g, "");
	if (field_val.length==0){
		alert('Please type your first name');
		document.form1.first_name.value=""
		document.form1.first_name.focus();
		return false;
		}
	return true;
}

function isLastNameOK(){
	var field_val=document.form1.last_name.value.replace(/[ ]/g, "");
	if (field_val.length==0){
		alert('Please type your last name');
		document.form1.last_name.value=""
		document.form1.last_name.focus();
		return false;
		}
	return true;
}

function isGenderOK(){
	if(!document.form1.gender[0].checked && !document.form1.gender[1].checked){
		alert('Please select your gender.');
		document.form1.gender[0].focus();
		return false;
	}
return true;
}

function isAddres1OK(){
	var field_val=document.form1.address1.value.replace(/[ ]/g, "");
	if (field_val.length==0){
		alert('Please type your post address.');
		document.form1.address1.value=""
		document.form1.address1.focus();
		return false;
		}
	return true;
}

function isAddres2OK(){
	var field_val=document.form1.address2.value.replace(/[ ]/g, "");
	if (field_val.length==0){
		alert('Please type your House/Flat number.');
		document.form1.address2.focus();
		return false;
	}
	/*else if (!isInteger(field_val)){
		alert('Please type your house number(digits only)!.');
		document.form1.address2.value=""
		document.form1.address2.focus();
		return false;
	}*/
	return true;
}


function isCityOK(){
	var field_val=document.form1.city.value.replace(/[ ]/g, "");
	if (field_val.length==0){
		alert('Please type your city.');
		document.form1.city.value=""
		document.form1.city.focus();
		return false;
		}
	return true;
}

function month_of_birthOK(){
	if(document.form1.birth_month.value=="N"){
	if (document.form1.birth_month.options[document.form1.birth_month.selectedIndex].value == "N")
		{
		alert('Plese select your month of birth.');
		document.form1.birth_month.focus();
        return false;
		}
	}	
return true;
}
	
function day_of_birthOK(){
	if(document.form1.birth_day.value=="N"){
	if (document.form1.birth_day.options[document.form1.birth_day.selectedIndex].value == "N")
		{
		alert('Plese select your day of birth.');
		document.form1.birth_day.focus();
        return false;
		}
	}	
return true;
}

function year_of_birthOK()
	{
	if(document.form1.birth_year.value=="1900")        
		{
		alert('Plese select your year of birth.');
		document.form1.birth_year.focus();
        return false;
		}
	if(document.form1.birth_year.value < 1900 || document.form1.birth_year.value >= 2003 )
		{
		alert("Year of Birth field is Invalid.\nPlease enter a proper value.");
		document.form1.birth_year.value="";
		document.form1.birth_year.focus();
		return false;
		}
	return true;
}

function isZipOK(){
	var field_val=document.form1.zip.value;
	if (!checkPostCode(field_val)){
		//alert('Postcode has invalid format.');
		document.form1.zip.value=""
		document.form1.zip.focus();
		return false;
	}
	return true;
}
//check UK ZipCode

function checkPostCode(toCheck){ //check postcode format is valid
 var PostCode = toCheck;
 var size;
 size = PostCode.length
 PostCode = PostCode.toUpperCase(); //Change to uppercase
 while (PostCode.slice(0,1) == " ") //Strip leading spaces
  {PostCode = PostCode.substr(1,size-1);size = PostCode.length
  }
 while(PostCode.slice(size-1,size)== " ") //Strip trailing spaces
  {PostCode = PostCode.substr(0,size-1);size = PostCode.length
  }
	PostCode = toCheck;
 if (size < 6 || size > 8){ //Code length rule
  alert(PostCode + " is not a valid postcode");
  return false;
  }
 if (!(isNaN(PostCode.charAt(0)))){ //leftmost character must be alpha character rule
	 alert(PostCode + " is not a valid postcode");
   return false;
  }
 if (isNaN(PostCode.charAt(size-3))){ //first character of inward code must be numeric rule
  alert(PostCode + " is not a valid postcode");
   return false;
  }
 if (!(isNaN(PostCode.charAt(size-2)))){ //second character of inward code must be alpha rule
 alert(PostCode + " is not a valid postcode");
   return false;
  }
 if (!(isNaN(PostCode.charAt(size-1)))){ //third character of inward code must be alpha rule
  alert(PostCode + " is not a valid postcode");
   return false;
  }
 if (!(PostCode.charAt(size-4) == " ")){//space in position length-3 rule
   alert(PostCode + " is not a valid postcode - no space or space in wrong position");
   return false;
   }
 count1 = PostCode.indexOf(" ");count2 = PostCode.lastIndexOf(" ");
 if (count1 != count2){//only one space rule
   alert(PostCode + " is not a valid postcode - only one space allowed");
   return false;
  }
return true;
}

function checkPostCode_old (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);

  // 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;
}
//=============================================================================
//=== Check UK fhone number
function isPnone1OK(){
	var field_val1=document.form1.phone1.value.replace(/[ ]/g, "");
	if (!checkUKTelephone(field_val1)){
		alert (telNumberErrors[telNumberErrorNo]);
		document.form1.phone1.focus();
		return false;
	}
return true;
}

function checkUKTelephone (telephoneNumber) {

  // Convert into a string and check that we were provided with something
  var telnum = telephoneNumber + " ";
  if (telnum.length == 1)  {
     telNumberErrorNo = 1;
     return false
  }
  telnum.length = telnum.length - 1;
  
  // Don't allow country codes to be included (assumes a leading "+")
  var exp = /^(\+)[\s]*(.*)$/;
  if (exp.test(telnum) == true) {
     telNumberErrorNo = 2;
     return false;
  }
  
  // Remove spaces from the telephone number to help validation
  while (telnum.indexOf(" ")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)
  }
  
  // Remove hyphens from the telephone number to help validation
  while (telnum.indexOf("-")!= -1)  {
    telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)
  }  
  
  // Now check that all the characters are digits
  exp = /^[0-9]{10,11}$/
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 3;
     return false;
  }
  
  // Now check that the first digit is 0
  exp = /^0[0-9]{9,10}$/
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 4;
     return false;
  }
  
  // Finally check that the telephone number is appropriate.
  exp = /^(01|02|05|070|077|078|079|08)[0-9]+$/;
  if (exp.test(telnum) != true) {
     telNumberErrorNo = 5;
     return false;
  }
  
  // Telephone number seems to be valid - return the stripped telehone number  
  return telnum;
}
var telNumberErrorNo = 0;
var telNumberErrors = new Array ();
telNumberErrors[0] = "Valid UK telephone number";
telNumberErrors[1] = "Telephone number not provided";
telNumberErrors[2] = "UK telephone number without the country code, please";
telNumberErrors[3] = "UK telephone numbers should contain 10 or 11 digits";
telNumberErrors[4] = "The telephone number should start with a 0";
telNumberErrors[5] = "The telephone number is either invalid or inappropriate";
//=============================================================================


function isEMailOK(){
	var emstr;  // e-mail string
	var emstr_confirm;  // e-mail string
	var sht;    // '@' position in e-mail
	var dot;    // '.' position in e-mail
	var Wrongsim='"' + " :*'!%+=;,/#";
	
	emstr=document.form1.email.value.toLowerCase();

	sht=emstr.indexOf('@');
	dot=emstr.indexOf('.');

	if(sht==-1 || dot==-1)
		{
		alert("Invalid E-mail!\nPlease Type Again.");
		document.form1.email.value="";
		document.form1.email.focus();
		return false;
		}
	if(sht==0||dot==0)
		{
		alert("Invalid E-mail!\nPlease Type Again.");
		document.form1.email.value="";
		document.form1.email.focus();
		return false;
		}
	if(emstr.charAt(emstr.length-1)=='@'||emstr.charAt(emstr.length-1)=='.')
		{
		alert("Invalid E-mail!\nPlease Type Again.");
		document.form1.email.value="";
		document.form1.email.focus();
		return false;
		}
	if(emstr.split('@').length!=2)
		{
		alert("Invalid E-mail!\nPlease Type Again.");
		document.form1.email.value="";
		document.form1.email.focus();
		return false;
		}
	if(emstr.split('@')[1].indexOf('.')==-1||emstr.split('@')[1].indexOf('.')==0)
		{
		alert("Invalid E-mail!\nPlease Type Again.");
		document.form1.email.value="";
		document.form1.email.focus();
		return false;
		}
	for(i=0;i<emstr.length;i++)
		{
		if(Wrongsim.indexOf(emstr.charAt(i))!=-1)
			{
			alert("Invalid E-mail!\nPlease Type Again.");
			document.form1.email.value="";
			document.form1.email.focus();
			return false;
			}
		}
	return true;
}

function isMaritalStatusOk(){
	if(document.form1.marital_status.selectedIndex==0){
		alert("Please select your marital status");
		document.form1.marital_status.focus();
		return false;
	}
	return true;
}

function isAccomodationOk(){
	/*if(document.form1.accomodation.selectedIndex==0){
		alert("Please select your accomodation");
		document.form1.accomodation.focus();
		return false;
	}*/
	return true;
}

function isWorkStatusOk(){
/*	if(document.form1.work_status.selectedIndex==0){
		alert("Please select your work status");
		document.form1.work_status.focus();
		return false;
	}*/
	return true;
}


function isOccupationOk(){
	/*if(document.form1.occupation.selectedIndex==0){
		alert("Please select your current occupation");
		document.form1.occupation.focus();
		return false;
	}*/
	return true;
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(InMonth,InDay,InYear){
	var daysInMonth = DaysArray(12)
	var strMonth=InMonth
	var strDay=InDay
	var strYear=InYear
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)

	if (strMonth.length<1 || month<1 || month>12 || !isInteger(month)){
		alert("Please enter a valid month")
		document.form1.birth_month.focus();
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || !isInteger(day) || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		document.form1.birth_day.focus();
		return false
	}
	if (strYear.length != 4 || year==0 || !isInteger(year)){
		alert("Please enter a valid 4 digit year")
		document.form1.birth_year.focus();
		return false
	}
return true
}

function ValidateBirthDate(){
	
	var month = document.form1.birth_month.value
	var day = document.form1.birth_day.value
	var year = document.form1.birth_year.value
	if (isDate(month,day,year)==false){
		return false
	}
    return true
 }