/************************************************
form_comn.js - Common form routines
Copyright 2003-2004 Better-Mortgages.co.uk Limited
All rights reserved. Version 2.4
Used in better-mortgages.co.uk & better-insurance.co.uk  
 ************************************************/
 
// All multi-field form display pages
// Check browser capability 
var br;
var bt;
if (document.getElementById) {		// N6+ and IE5+
  br = "DOM";
  if (navigator.appName == "Netscape") { 
    bt = "Nets";
  } else if (navigator.appName == "Microsoft Internet Explorer") {
    bt = "MSIE";
  } else { bt = "Unknown";}
}
else if (document.all) br = "IE";  // IE  => V3
else if (document.layers) br = "NS";  // NE  => V2 
else br = "Unknown";

function makeArray(len) { // Form pages with message/image arrays
    for (var i = 0; i <= len; i++) this[i] = null;
    this.length = len;
}

helpImage = new makeArray(2);
helpImage[0] = new Image(); helpImage[0].src = location.protocol + '//' + location.hostname + '/images/help.jpg';
helpImage[1] = new Image(); helpImage[1].src = location.protocol + '//' + location.hostname + '/images/help2.jpg';

// --- form display / hide base routine 
function showItem(status, item) { 
  if (status == "ON") {
     if (br == "DOM") {
        if (bt == "Nets") {  // Mozilla and NS6. Should look for 'Gecko' instead?
           (item.substring(0,1) == "T") ? displayStat = "table" : displayStat = "table-row";
        } else if (bt == "MSIE") { // IE 6.  Should work with IE 5 - TEST
           displayStat = "";
        }
     } else if (br == "IE") {  // IE3 & 4? - TEST
        displayStat = "";
     } else if (br == "NS") {  // NS4.6 desn't respond to style.display anyway (see below).
        displayStat = "";
     }
  } else {  // status = OFF
     displayStat = "none";
  }
  if (br == "DOM") {
     document.getElementById(item).style.display = displayStat;
  } else if (br == "IE") {
     document.all.item.style.display = displayStat;
  } else  if (br == "NS") {
     document.item.style.display = displayStat;
  }
}

// general routines
function refocus(field) {
  if( field.style ) {
    if (br == "DOM") { // include red border around the field in question
      document.getElementById(field.name).style.borderColor="red";
    } else if (br == "IE") {
      document.all.field.style.borderColor="red";
    } else  if (br == "NS") {
      document.field.style.borderColor="red";
    }
  }
  field.select();
  field.focus();
}

function isFilled(field, message) {  // checks for empty field
 if ((field.value === "") || (field.value === null)) { 
   alert(message); 
   refocus(field);
   return false; 
 }
 return true;
}

function toggleHelpImage(Num) {
 if (document.images) { 
  var id = "i" + Num;
  (document.images[id].src == helpImage[0].src) ? document.images[id].src = helpImage[1].src : document.images[id].src = helpImage[0].src;
 }  
}

function triggerAlert(alertNum, imageId) {
 toggleHelpImage(imageId); 
 alert(helpText[alertNum]); 
 toggleHelpImage(imageId);
}

function insertCommas(inputValue) {
 var theNumber = new makeArray(2); 
 var theOutput = ""; 
 theNumber = inputValue.split(".");
 if (theNumber[1] !== null ) theOutput = "." + theNumber[1]; var x = 1;
 for (var i = theNumber[0].length; i > 0; --i) {
  theOutput = theNumber[0].substring(i, i-1) + theOutput;
  if (x == 3) { theOutput = "," + theOutput; x = 1; }
  else x++;
 }
 if (theOutput.charAt(0) == ",") theOutput = theOutput.substring(1, theOutput.length);
 return theOutput;
}

function isInteger(field) {  // checks for only integers
  var re = /^\d+$/;
  if (!re.test(field.value)) {
    alert("This is not a whole number. Please enter only digits between 0 and 9.");
    refocus(field); return false;
  }
  return true; 
}

function isEmail(field) {  // checks basic email format
  re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,6})+$/;
  if (!re.test(field.value)) {
    alert("Your email address format seems incorrect. Please check your email address.");
    refocus(field); return false;
  }
return true; 
}

function isHTTP(field) {
//  re = /^https?:\/\/[\w\.-]+[\w]{2,5}[\/~\w-]*[\/]?([\w-]*\.[\w]{1,5}[\?]?[\w=%;&-\"]*)?$/;  //original with error around &-\
  re = /^https?:\/\/[\w\.-]+[\w]{2,5}[\/~\w-]*[\/]?([\w-]*\.[\w]{1,5}[\?]?[\w=%;\"&-]*)?$/;
  if (!re.test(field.value)) {
    alert("Your HTTP address format seems incorrect.\nNo 'FTP' or port numbers allowed, only standard HTTP URL characters.\nPlease check your URL.");
    refocus(field); return false;
  }
  return true; 
}

function isURL(field) {  // is this different from isHTTP? If not rationalise and delete one
  re = /^http(s?):\/\/\w+([\.-]?\w+)*\.\w{2,6}(\/[\w-_~]*)*(\.\w{1,4})?(\?{1}(\w+[\+\=\&])+\w+)?$/;
  if (!re.test(field.value)) {
    alert("This URL address format seems incorrect. Please check the URL address.");
    refocus(field); return false;
  }
	return true; 
}

//isTelnum() checks for UK telephone format - digits only plus => 10 digits.
function isTelnum(field) {  
  var fieldNum = '' + field.value;
  var Telnum = stripstr(fieldNum, " ");
  re = /^\d{10,}$/;
  if (!re.test(Telnum)) {
    alert("This does not seem to be a full UK telephone number. Please enter your UK telephone number using numbers 0 to 9 and use the full STD code.");
    refocus(field);
    return false;
  }
return true;
}

function isPostCode(field) {
  var localstring = field.value.toUpperCase();
  var re = /^[A-Z]{1,2}[0-9]{1,2}[A-Z]{0,1}[ ]?[0-9]{1}[A-Z]{2}$/ ;
  if (!re.test(localstring)) {
    alert("This does not seem to be a UK Postcode. Please enter your Postcode in standard format.\nThe first section (outward code) is a single letter followed by up to 3 letters or digits.\nThe second section (inward code) is a single digit followed by two letters. The inward and\noutward sections are seperated by a single space.");
    refocus(field);
    return false;
  }
  return true;
}

function getDomain(itemstring) {
  var re = /^http[s]?:\/\/([w]{3}\.)?/;
  itemstring = itemstring.replace(re, ""); // strip off leading text up to www.  
  re = /\/[\S]*/;
  itemstring = itemstring.replace(re, ""); // strip off trailing text from / to the end
  return itemstring;
}

function isDecimal(field) {   // checks for decimal number
  var re = /^\d+(\.)?(\d+)?$/;
  if (!re.test(field.value)) {
    alert("This does not seem to be a decimal number. Please enter the number using numbers 0 to 9 and a decimal point only ");
    refocus(field); 
    return false;
  }
return true; 
}

function isDate(field) {    // checks for UK dd/mm/yyyy date format
  re = /^(0[1-9]|[12]\d|3[01])\/(0[1-9]|1[0-2])\/(19|20)\d{2}$/; // range 1900 to 2099
  if (!re.test(field.value)) {
    alert("Please enter the date in the format dd/mm/yyyy using only digits and the '/' seperator.");
    refocus(field); 
    return false;
  }
  var splitDate = field.value.split("/"); 
  var month = splitDate[1]*1; 
  var day = splitDate[0]*1;
  if (((month == 4) || (month == 6) || (month == 9) || (month == 11)) && (day > 30)) {
    alert("Please enter a two digit number between 01 and 30.");
    refocus(field); 
    return false;    
  } else if ((month == 2) && (day > 29) )  {  //  no 'leap year' routine
    alert("Please enter a two digit number between 01 and 29.");
    refocus(field); 
    return false;
  }else if (day > 31) {
    alert("Please enter a two digit number between 01 and 31.");
    refocus(field); 
    return false;    
  }
return true;
}
