//Cookie name text
var txtSize= 'userTxtSize'
var returnURL =  getNodeInfo("GLOBAL","returnHouseCallURL");
var topImage =  "images/" + getNodeInfo("GLOBAL","topImage");
var loginPage = getNodeInfo("GLOBAL","LoginPage");
var addRepairLink = ""; 	

function LoadXMLString() {
  if (window.DOMParser) {
    parser=new DOMParser();
    xmlDoc=parser.parseFromString(text,"text/xml");
  }
  else // Internet Explorer
  {
    xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async="false";
    xmlDoc.loadXML(text);
  }	
} 

//Opens xml file for specific browser
function loadXMLDoc(fileName) {
  if (window.XMLHttpRequest)
  {
    xhttp=new window.XMLHttpRequest();
  }
  else
  {
    xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xhttp.open("GET",fileName,false);
  xhttp.send("");
  return xhttp.responseXML;
} 

function toJavaDateFormat(field) {
  if (field.value != '') {
    dateArray = field.value.split("/");
    return dateArray[2]+ "-" + dateArray[1] + "-" + dateArray[0] + "T00:00";
  }
  else {
    return '';
  }
}

function toUKDateFormat(field) {
  if (field.value != '') {
    dateTimeSplit = field.value.split("T");
    datePart = dateTimeSplit[0].split("-");
    return datePart[2] + "/" + datePart[1] + "/" + datePart[0];
  }
  else {
    return '';
  }
}

//Opens XML Content file and displays content for page
function getNodeInfo(page,node) {
  xmlDoc=loadXMLDoc("custom/SSPData.xml");
  x=xmlDoc.getElementsByTagName("Page");
  for(i=0;i<x.length;i++) {
    //Evaluate node attributes for a value of page parameter
    if(x[i].attributes[0].nodeValue == page) {
      //If true iterate childnodes
      for(j=0;j<x[i].childNodes.length;j++) {
        //Is childnode name equal to node parameter
        if(x.item(i).childNodes[j].nodeName == node) {
          return x[i].childNodes[j].firstChild.nodeValue;
        }
      }
    }
  }
}

//Opens XML Content file and displays button config
function getButtonInfo(buttonName,attributeIndex) {
  xmlDoc=loadXMLDoc("custom/Button.xml");
  x=xmlDoc.getElementsByTagName("Button");
  for(i=0;i<x.length;i++) {
    //Evaluate node attributes for a value of page parameter
    if(x[i].attributes[0].nodeValue == buttonName) {
      return x[i].attributes[attributeIndex].nodeValue;
    }
  }
}

//Validates password using regular expression
function checkPasswordsMatch(theForm) {
  var alphaExp = /^[0-9a-zA-Z]+$/;
  var valid = true;
  var password = theForm.PASSWORD;
  var newPassword = theForm.NEWPASSWORD;
  if (password.value.match(alphaExp)) {
    if (password.value != newPassword.value) {
      var valid = false;
      alert("Your new passwords do not match");
    }
    else {
      if (password.value.length > 20) {
        var valid = false;
        alert("Your new password cannot exceed 20 characters");
      }								
    }
  }
  else {
    var valid = false;
    alert("Your new password must contain valid numbers or characters");
  }
  return valid;
}

//Validates reminder form
function CheckPersonNum(theForm) {
  var valid = true;
  var msg = "\n";  //Appended error description variable
  var field = theForm.email;
  var ConfField = theForm.email;		
  var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
  if (!goodEmail) {
    valid = setBackgroundColour(field,true)	
    msg = msg + "Field: Email-address - Not formatted correctly. e.g. fred@bloggs.com\n"
  }
  else {
    tmp = setBackgroundColour(field,false)				 
  }
  var field = theForm.choice;
  if (field.value == "NULL") {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Select a login credential\n";
  }
  else {
  tmp = setBackgroundColour(field,false)
  }
  //Display an error message describing the nature of the problem
  if (!valid) {
    alert("Please correct the following form fields (Highlighted Yellow)\n\n" + msg);
  }
  //Return the status of the function
  return valid
}

//Check Feedback form
function checkFeedback(theForm) {
  //Global function return variable
  var valid = true;
  var msg = "\n";  //Appended error description variable
  //Ensure that feedback has been given
  var field = theForm.feedbackNotes;
  if (field.value == "") {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Feedback is mandatory\n";
  }
  else {
    if (field.value.length >2000) {
      valid = setBackgroundColour(field,true);
      msg = msg + "Field: Maximum number of characters is 2000\n";
    }
    else {
      if (valid) {
        tmp = setBackgroundColour(field,false);
      }
    }
  }
  //Display an error message describing the nature of the problem
  if (!valid) {
    alert("Please correct the following form fields (Highlighted Yellow)\n\n" + msg);
  }
  return valid
}

// Validate message sent via website
function checkMessage(theForm) {
  //Global function return variable
  var valid = true;
  var msg = "\n";
  var field = theForm.message;
  if (field.value == "") {
    valid = setBackgroundColour(field,true);
    msg = msg + "Please provide a message\n";
  }
  else {
    if (field.value.length >2000) {
      valid = setBackgroundColour(field,true);
      msg = msg + "Field: Maximum number of characters is 2000\n";
    }
    else {
      if (valid) {
        tmp = setBackgroundColour(field,false);
      }
    }
  }
  //Display an error message describing the nature of the problem
  if (!valid) {
    alert("Please correct the following form fields (Highlighted Yellow)\n\n" + msg);
  }
  return valid
}

// tests to see if string is in correct UK style postcode: AL1 1AB, BM1 5YZ etc.
function isValidPostcode(p) {
  var postcodeRegEx = /[A-Z]{1,2}[0-9]{1,2} ?[0-9][A-Z]{2}/i;
  return postcodeRegEx.test(p);
}

// formats a valid postcode nicely: AB120XY -> AB1 0XY
function formatPostcode(p) {
  if (isValidPostcode(p)) {
    var postcodeRegEx = /(^[A-Z]{1,2}[0-9]{1,2})([0-9][A-Z]{2}$)/i;
    return p.replace(postcodeRegEx,"$1 $2");
  } else {
    return p;
  }
}

function checkCorrAddress(theForm) {
  //Global function return variable
  var valid = true;
  var msg = "\n";  //Appended error description variable
  var field = theForm.ADDRESS1;
  if (field.value == "") {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Address1:- Requires a value\n";
  }
  else {
    if (field.value.length >40) {
      valid = setBackgroundColour(field,true);
      msg = msg + "Field: Address1:- Maximum number of characters is 40\n";
    }
    else {
      if (valid) {
        tmp = setBackgroundColour(field,false);
      }
    }
  }
  var field = theForm.ADDRESS2;
  if (field.value == "") {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Address2:- Requires a value\n";
  }
  else {
    if (field.value.length >40) {
      valid = setBackgroundColour(field,true);
      msg = msg + "Field: Address2:- Maximum number of characters is 40\n";
    }
    else {
      if (valid) {
        tmp = setBackgroundColour(field,false);
      }
    }
  }
  var field = theForm.ADDRESS3;
  if (field.value == "") {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Address3:- Requires a value\n";
  }
  else {
    if (field.value.length >40) {
      valid = setBackgroundColour(field,true);
      msg = msg + "Field: Address3:- Maximum number of characters is 40\n";
    }
    else {
      if (valid) {
        tmp = setBackgroundColour(field,false);
      }
    }
  }
  var field = theForm.POSTCODE;
  if (!isValidPostcode(field.value)) {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Postcode - incorrect format\n";
  }
  else {
    if (valid) {
      tmp = setBackgroundColour(field,false);
      field.value = formatPostcode(field.value);
    }
  }
  var field = theForm.effectFromFormField;
  if (field.value == "") {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Effective From:- Cannot be blank\n";
  }
  else {
    if (toJavaDateFormat(field) <= theForm.EXISTINGSTARTDATE.value) {
      valid = setBackgroundColour(field,true);
      msg = msg + "You already have a correspondence address starting " + toUKDateFormat(theForm.EXISTINGSTARTDATE) + "; Please choose a later date.\n";
    }
    else {
      tmp = setBackgroundColour(field,false);
      if (valid) {
        theForm.EFFECTFROM.value = toJavaDateFormat(field);
      }
    }
  }
  if (valid) {
    theForm.EFFECTTO.value = toJavaDateFormat(theForm.effectToFormField);
    if (theForm.EFFECTTO.value != "" && theForm.EFFECTFROM.value > theForm.EFFECTTO.value) {
      valid = setBackgroundColour(theForm.effectToFormField,true);
      msg=msg + "TO date must be on or after FROM date\n";
    }
  }

  //Display an error message describing the nature of the problem
  if (!valid) {
    alert("Please correct the following form fields (Highlighted Yellow)\n\n" + msg);
  }
  return valid
}

function checkForm(theForm) {
  //Global function return variable
  var valid = true;
  var msg = "\n";  //Appended error description variable
  //Ensure that a person number has been entered and is a number value
  var field = theForm.pernum;
  if ((field.value == "") || isNaN(field.value)) {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Personal ID - requires a numeric value\n";
  }
  else {
    tmp = setBackgroundColour(field,false);
  }
  //Ensure a password has been provided
  var field = theForm.password;
  if (field.value == "" ) {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Password - requires a value\n";
  }
  else {
    tmp = setBackgroundColour(field,false);
  }
  //Ensure a mother's maiden name has been provided
  var field = theForm.mothersmaidenname;
  if (field.value == "" ) {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Mother's Maiden Name - requires a value\n";
  }
  else {
    tmp = setBackgroundColour(field,false);
  }
  //Display an error message describing the nature of the problem
  if (!valid) {
    alert("Please correct the following form fields (Highlighted Yellow)\n\n" + msg);
  }
  //Return the status of the function
  return valid
}
	
function setBackgroundColour(field,condTest) {
  if (condTest) {
    field.style.backgroundColor = "yellow";
    return valid = false;
  }
  else {
    field.style.backgroundColor = "white";
  }
}

function checkRegistrationForm(theForm) {
  //Global function return variable
  var valid = true;
  var msg = "\n\n";
  //Ensure that a tenancy number has been entered and is a number value
  var field = theForm.tenancynumber;
  if ((field.value == "") || isNaN(field.value)) {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Tenancy Number - requires a numeric value\n";
  }
  else {
    tmp = setBackgroundColour(field,false);
  }
  //Ensure a surname has been provided
  var field = theForm.surname;
  if (field.value == "" ) {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Surname - requires a value\n";
  }
  else {
    tmp = setBackgroundColour(field,false);
  }
  //Ensure a first name has been provided
  var field = theForm.leadinginitial;
  if (field.value == "" ) {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: First name(s) - requires a value\n";
  }
  else {
    tmp = setBackgroundColour(field,false);
  }
  //Ensure a password has been provided
  var field = theForm.password;
  var Conffield = theForm.Confpassword;
  var alphaExp = /^[0-9a-zA-Z]+$/;
  var password = theForm.password;
  if (password.value.match(alphaExp)) {
    tmp = setBackgroundColour(field,false);
    tmp = setBackgroundColour(Conffield,false);
    if (password.value.length > 20) {
      valid = setBackgroundColour(field,true);
      msg = msg + "Field: Password cannot exceed 20 characters\n";
    }
    else {
      if (password.value != Conffield.value) {
        valid = setBackgroundColour(field,true);
        valid = setBackgroundColour(Conffield,true);
        msg = msg + "Fields: Password and Confirm Password must match \n";
      }
    }
  }
  else {
    var valid = false;
    msg = msg + "Field: Password must contain valid numbers or characters\n";
    valid = setBackgroundColour(field,true);
  }
  //Ensure a mother's maiden name has been provided
  var field = theForm.mothersmaidenname;
  if (field.value == "" ) {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Mother's maiden name - requires a value\n";
  }
  else {
    tmp = setBackgroundColour(field,false);
  }
  //Ensure that a correctly formatted email address is entered
  var field = theForm.email;
  var ConfField = theForm.Confemail;
  var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
  if (!goodEmail) {
    valid = setBackgroundColour(field,true)
    msg = msg + "Field: Email-address - Not formatted correctly. i.e. fred@bloggs.com\n"
  }
  else {
    if (field.value != ConfField.value) {
      msg = msg + "Field: Email-address and Confirm email-address do not match\n"
      valid = setBackgroundColour(field,true)
      valid = setBackgroundColour(ConfField,true)
    }
    else {
      tmp = setBackgroundColour(field,false)
      tmp = setBackgroundColour(ConfField,false)
    }
  }
  //Display an error message describing the nature of the problem
  if (!valid) {
    alert("Please correct the following fields (Highlighted Yellow)\n\n" + msg);
  }
  //Return the status of the function
  return valid
}

//Function sets the size of the the text in 'wrap' section 
//of each content page. The size of the text is set by using 
//the intValue parameter which is passed by the control control
//on each page. This setting is then wrote to the documents cookie
//and used across subsequent pages during the surfers current visit. 
function setSize(intValue){
  if (document.getElementById("wrap") && intValue != 0){
    divSectionObj = document.getElementById("wrap").style;
    divSectionObj.fontSize = intValue + "px";
   	if (document.cookie){
		  expireDate = new Date;
			expireDate.setMonth(expireDate.getMonth()+3);			
      document.cookie = txtSize + "=" + escape(divSectionObj.fontSize)+ ";expires=" + expireDate.toGMTString();
	  }	
   }
}

//Function retrives the userTxtSize value from the document cookie. Once
//retrieved it is applied to the wrap section of the current page.
function setFontPreference(){
  if (document.cookie && document.getElementById('wrap') ){
     divSectionObj = document.getElementById('wrap').style
     fntSize = getCookieValues();
     if (fntSize != ""){
	     divSectionObj.fontSize = fntSize
     }		
  }
}

//Function retrieves the user specified font size from the cookie and returns
//it to the calling variable.
function getCookieValues(){   
   var userCookie = unescape(document.cookie.split(";")[0])
   var fntSize = userCookie.split("=")[1]
	 if (fntSize < 2){
	    fntSize = "12";
	 }
   return fntSize 
}

function validTelephoneNumber (field) {
  return field.value.match(/^[+]?[\d() -]+$/);
}

//Check inputs on person update form	  
function CheckPersonUpdate (theForm) {
  //Global function return variable
  var valid = true;
  var msg = "\n";
  //Ensure that a homephone is a number and less than 15 characters
  var field = theForm.HOMEPHONE;
  var blankField = theForm.blankHOMEPHONE.checked;
  if (blankField == false) {
    if (!validTelephoneNumber (field)) {
      valid = setBackgroundColour(field,true);
      msg = msg + "Field: Home Telephone Number - invalid, should contain only digits, spaces, brackets, + and - symbols\n";
    }
    else {
      if (field.value.length > 15) {
        valid = setBackgroundColour(field,true);
        msg = msg + "Field: Home Telephone Number - cannot be more than 15 characters\n";
      }
      else {
        if (field.value.length == 0) {
          valid = setBackgroundColour(field,true);
          msg = msg + "Field: Home Telephone Number - cannot be blank, unless specified to be so\n";
        }
        else {
          if (!valid) {
            tmp = setBackgroundColour(field,false);
          }
        }
      }
    }
  }
  else {
    setBackgroundColour(field,false)
  }

  //Ensure that a mobile is a number and less than 15 characters
  var field = theForm.MOBILEPHONE;
  var blankField = theForm.blankMOBILEPHONE.checked;
  if (blankField == false) {
    if (!validTelephoneNumber (field)) {
      valid = setBackgroundColour(field,true);
      msg = msg + "Field: Mobile Phone Number - invalid, should contain only digits, spaces, brackets, + or -\n";
    }
    else {
      if (field.value.length > 15) {
        valid = setBackgroundColour(field,true);
        msg = msg + "Field: Mobile Telephone Number - cannot be more than 15 characters\n";
      }
      else {
        if (field.value.length == 0) {
          valid = setBackgroundColour(field,true);
          msg = msg + "Field: Mobile Telephone Number - cannot be blank, unless specified to be so\n";
        }
        else {
          if (!valid) {
            tmp = setBackgroundColour(field,false);
          }
        }
      }
    }
  }
  else {
    setBackgroundColour(field,false)
  }
  
  //Ensure daytime phone number is a number and less than 15 characters
  var field = theForm.DAYTIMEPHONE;
  var blankField = theForm.blankDAYTIMEPHONE.checked;
  if (blankField == false) {
    if (!validTelephoneNumber (field)) {
      valid = setBackgroundColour(field,true);
      msg = msg + "Field: Daytime Phone Number - invalid, should contain only digits, spaces, brackets, + or -\n";
    }
    else {
      if (field.value.length > 15) {
        valid = setBackgroundColour(field,true);
        msg = msg + "Field: Daytime Phone Number - cannot be more than 15 characters\n";
      }
      else {
        if (field.value.length == 0) {
          valid = setBackgroundColour(field,true);
          msg = msg + "Field: Daytime Phone Number - cannot be blank, unless specified to be so\n";
        }
        else {
          if (!valid) {
            tmp = setBackgroundColour(field,false);
          }
        }
      }
    }
  }
  else {
    setBackgroundColour(field,false)
  }
  
  //Ensure that a correctly formatted email address is entered
  var field = theForm.EMAIL;
  var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
  if (!goodEmail) {
    valid = setBackgroundColour(field,true)
    msg = msg + "Field: Email - Not formatted correctly. i.e. fred@bloggs.com\n"
  }
  else {
    if (field.value.length > 50) {
      valid = setBackgroundColour(field,true)
      msg = msg + "Field: Email - Cannot be greater than 50 characters\n"
    }
    else {
      if (field.value == "") {
        valid = setBackgroundColour(field,true)
        msg = msg + "Field: Email - A value is required\n"
      }
      else {
        setBackgroundColour(field,false)
      }
    }
  }
  //Display an error message describing the nature of the problem
  if (!valid) {
    alert("Please address the following form fields (Highlighted Yellow)\n\n" + msg);
  }
  return valid
}

//Calculates the number of characters left
function CountLeft(field, count, max) {
  if (field.value.length > max) {
    field.value = field.value.substring(0, max);
  }
  else {
    count.value = max - field.value.length;
  }
} 

function checkDisabilityForm(theForm) {
  //Global function return variable
  var valid = true;
  var msg = "\n";
  var field = theForm.effectFromFormField;
  if (field.value == "") {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Effective From:- Requires a value\n";
  }
  else {
    tmp = setBackgroundColour(field,false);
    theForm.EFFECTFROM.value = toJavaDateFormat(field);
  }
  theForm.EFFECTTO.value = toJavaDateFormat(theForm.effectToFormField);
  if (theForm.EFFECTTO.value != "" && theForm.EFFECTFROM.value > theForm.EFFECTTO.value) {
    valid = setBackgroundColour(theForm.effectToFormField,true);
    msg=msg + "TO date must be on or after FROM date\n";
  }
  if (!valid) {
    alert("Please resolve the errors in the highlighted form fields\n\n" + msg);
  }
  return valid;
}

function CheckInputs(theForm) {
  //Global function return variable
  var valid = true;
  var msg = "\n";
  //Ensure that the location field contains a value
  var field = theForm.location;
  if (field.value == "") {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Location - requires a value\n";
  }
  else {
    setBackgroundColour(field,false)
  }
  var field = theForm.desc1;
  if (field.value == "") {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Job Description  - requires a value\n";
    if (field.value.length > 100) {
      valid = setBackgroundColour(field,true);
      msg = msg + "Field: Job Description  - cannot be greater than 255 characters\n";
    }
  }
  else {
    setBackgroundColour(field,false)
  }
  //Ensure Contact Telephone Number is a number and less than 15 characters
  // and is a numeric field
  var field = theForm.home;
  if (field.value.length > 15) {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Contact Telephone Number - Cannot be more than 15 characters\n";
  }
  else if (field.value.length == 0) {
    valid = setBackgroundColour(field,true);
    msg = msg + "Field: Contact Telephone Number - Must contain a value\n";
  }
  else if (!valid) {
    tmp = setBackgroundColour(field,false);
  }
  else {
    setBackgroundColour(field,false);
  }
  //Display an error message describing the nature of the problem
  if (!valid) {
    alert("Please address the following form fields (Highlighted Yellow)\n\n" + msg);
  }
  return valid;
}

function showMessage(authenticated) {
  strLink = "<a title='Click to send us a message' href='sendMessageForm.jsp'><img src='images/sendUsMessage.gif' alt='Click to send us a message'></a><br/>";
  if (authenticated) {
    return strLink;
  }
  else {
    return "";
  }
}

//Check to date is after from date
function checkDateRange (theForm) {
  //Global function return variable
  var valid = true;
  var msg = "\n";
  if (theForm.fromDate.velue != "" && theForm.toDate.value != "") {
    var fromDate = new Date(theForm.fromDate.value);
    var toDate = new Date(theForm.toDate.value);
    if (toJavaDateFormat(fromDate) > toJavaDateFormat(toDate)) {
      valid = setBackgroundColour(theForm.toDate,true);
      alert("The 'To Date' must be the same as or later than the 'From Date'");
    }
  }
  return valid
}

