/*

PUBLIC.JS - Common Javascript functions

checkIsEmpty		- Tests if a text box object is empty and displays a message if it is.
checkComboSelected	- Tests if a drop down list has been selected and displays a message if not


Various Field Validation functions - These functions are used to test fields as the form is being submitted

isNumeric
isAlpha
isAlphaAndSpace
isPostCode
isEmail


Various KeyPress function - These functions are used to restrict the keys being pressed in a field

MC_restrictToNumeric
MC_restrictToAlphaNumeric
Capitalise
Validation Functions
Macromedia Roll-Over support function
*/


/******************************************************************************/


/******************************************************************************/
/*

	Validation Functions

*/
	function Capitalise(s) {
	
		//Turn first letter to capital
		var strTemp
		var strLetter1
		var strRest
			
		strTemp = s.value
		strLetter1 = strTemp.substr(0,1)
		strRest = strTemp.substr(1)

		strTemp =  strLetter1.toUpperCase() + strRest.toLowerCase()

		//Capitalise letter after an apostorphe exp. O'Brien
		var intFindQuote
		intFindQuote = strTemp.search("'")
	
		if(intFindQuote != -1) {
			strRest = strTemp.substr(intFindQuote + 2)
			strLetter1 = strTemp.substr(intFindQuote+1,1)
			strTemp = strTemp.substr(0,intFindQuote+1) + strLetter1.toUpperCase() + strRest	
		}

		
		//Capitlise letter after hyphen example Finch-Hutchins
		intFindQuote = strTemp.search("-")
	
		if(intFindQuote != -1) {
			strRest = strTemp.substr(intFindQuote + 2)
			strLetter1 = strTemp.substr(intFindQuote+1,1)
			strTemp = strTemp.substr(0,intFindQuote+1) + strLetter1.toUpperCase() + strRest	
		}




//This Capitlises all the letters after a space: eg 22 park row > 22 Park Row
for(var i=0;i<strTemp.length;i++){

if(strTemp.charAt(i)==" "){
var str1stHalf
var str2ndHalf
var strLet2Capitilise

str1stHalf= strTemp.slice(0,i)
strLet2Capitilise = strTemp.slice(i+1,i+2)
str2ndHalf= strTemp.slice(i+2)
strTemp = str1stHalf + " " + strLet2Capitilise.toUpperCase() + str2ndHalf
}

}










		s.value = strTemp
		return false

	}

function CorrectFormat(objTest){
	
	var intTemp = new Number(objTest.value)

	if(intTemp>0){
		var strTemp = new String(Math.round((intTemp*100))/100)
		var intLength = strTemp.length

		if(strTemp.substr((intLength-1),1)=="."){
			strTemp+='00'
		}
		if(strTemp.substr((intLength-2),1)=="."){
			strTemp+='0'
		}

		if(strTemp.indexOf(".")==-1){
			strTemp+='.00'
		}

		objTest.value = strTemp
	}
}

// this function does not display an alert message if the postcode is invalid
function IsPostcodeValid(s)
{
	var strTemp
	var strCode1
	var strCode2
	var strRest
	var intPostLength
	strTemp = s.value
	strTemp = strTemp.replace(" ", "")
	strTemp = strTemp.toUpperCase()

	if(strTemp.substr(0,4)=='BFPO')
	{
		s.value = strTemp
		var bValidPostCode =true
	}
	else
	{
		intPostLength = strTemp.length
		
		strCode2 = strTemp.substr(intPostLength-3)
		strCode1 = strTemp.substr(0,intPostLength-3)

		if (intPostLength > 0)
		{
			strTemp = strCode1.toUpperCase() + " " + strCode2.toUpperCase()
		} 
		s.value = strTemp
		var bValidPostCode = (s.value && isPostCode(s.value)) ? true : false;
	}

	if (!bValidPostCode)
	{
      s.select()
	  return false
	}
	  return true
}

function PostCodeSpace(s) {

	var strTemp
	var strCode1
	var strCode2
	var strRest
	var intPostLength
	strTemp = s.value
	strTemp = strTemp.replace(" ", "")
	strTemp = strTemp.toUpperCase()

	if(strTemp.substr(0,4)=='BFPO'){
		s.value = strTemp
		var bValidPostCode =true
	} else {
		intPostLength = strTemp.length
		
		strCode2 = strTemp.substr(intPostLength-3)
		strCode1 = strTemp.substr(0,intPostLength-3)

		if (intPostLength > 0) {
			strTemp = strCode1.toUpperCase() + " " + strCode2.toUpperCase()
		} 
		s.value = strTemp
		var bValidPostCode = (s.value && isPostCode(s.value)) ? true : false;
	}



	if (!bValidPostCode) {
	  alert ("The Post Code you entered is not in a valid format. Please Re-enter")
      s.select()
	  return false
	}
	  return true
	}
	
	function PostCodeSpace5(s) {

	var strTemp
	var strCode1
	var strCode2
	var strRest
	var intPostLength
	strTemp = s.value
	strTemp = strTemp.replace(" ", "")
	strTemp = strTemp.toUpperCase()

	if(strTemp.substr(0,4)=='BFPO'){
		s.value = strTemp
		var bValidPostCode =true
	} else {
		intPostLength = strTemp.length
		
		strCode2 = strTemp.substr(intPostLength-3)
		strCode1 = strTemp.substr(0,intPostLength-3)

		if (intPostLength > 0) {
			strTemp = strCode1.toUpperCase() + " " + strCode2.toUpperCase()
		} 
		s.value = strTemp
		var bValidPostCode = (s.value && isPostCode(s.value)) ? true : false;
	}



	if (!bValidPostCode) {
	  alert ("The Post Code you entered is not in a valid format. Please Re-enter")
      s.select()
	  return false
	}
	  return true
	}
	


	function IsEmailValid(t)
	{
		var bValidEmail = (t.value.length == 0) ? true : ((isEmail(t.value)) ? true : false);

		if(!bValidEmail)
		{
			return false	
		}
		
		return true   
	}

	function CheckEmail(t) {
	
		//var f = document.frmQuote
		var bValidEmail = (t.value.length == 0) ? true : ((isEmail(t.value)) ? true : false);

		if (!bValidEmail) {
			alert ("The email address you entered is not in a valid format.")
			t.select();
		}     
	}


/******************************************************************************/
function IsTextboxEmpty(oTxt)
{
	if(oTxt.value.length==0)
	{
		return true;
	}
	
	return false;
}

function checkIsEmpty(oTxt, sMsg) {
    if (oTxt.value.length==0) {
		if (sMsg.length > 0) { 
	        alert (sMsg)
		    oTxt.focus();
		}
        return true;
    } else {
        return false;
    }

}
/******************************************************************************/

/***********Same as above but checks for 2 fields*************************/
function checkAreEmpty(oTxt1,oTxt2,sMsg) {

    if ((oTxt1.value.length==0) && (oTxt2.value.length==0)) {
		if (sMsg.length > 0) { 
	        alert (sMsg)
			if (oTxt1.value.length==0) {
		    oTxt1.focus();
			} else {
				oTxt2.focus();
			}
		}
        return true;
    } else {
        return false;
    }

}
/******************************************************************************/
/***********Same as above but checks for 2 fields*************************/
function checkOneOfThree(oTxt1,oTxt2,oTxt3,sMsg) {

    if ((oTxt1.value.length==0) && (oTxt2.value.length==0) && (oTxt3.value.length==0)) {
		if (sMsg.length > 0) { 
	        alert (sMsg)
		    oTxt1.focus();
		}
        return true;
    } else {
        return false;
    }

}
/******************************************************************************/

/******************************************************************************/
/*
	Usage Example of checkIsEmpty function)
*/
function checkTextbox() {

    var f = document.frmTest
    
    if (!checkIsEmpty(f.txtTest, "Text box cannot be empty")) {
        alert ("ok")
    }

}
/******************************************************************************/

/******************************************************************************/
function checkComboSelected(oCbo,sMsg) {

	if (oCbo.options[oCbo.selectedIndex].value.length==0) {
		if (sMsg.length > 0) {
			alert (sMsg)
			oCbo.focus();
		}
		return false;
	} else {
		return true;
	}
}

/******************************************************************************/
/******************************************************************************/
function checkComboFirstSelected(oCbo,sMsg) {
	if (oCbo.selectedIndex==0) {
		if (sMsg.length > 0) {
			alert (sMsg)
			oCbo.focus();
		}
		return false;
	} else {
		return true;
	}
}

/******************************************************************************/
/*
	FIELD VALIDATION

*/
function isNumeric(s) {

	var re = /^[\d]+$/g
	return (re.test(s)) ? true : false

}

function isAlpha(s) {

	var re = /^[a-zA-Z]+$/g
	return (re.test(s)) ? true : false

}

function isAlphaAndSpace(s) {

	var re = /^[a-zA-Z\s]+$/g
	return (re.test(s)) ? true : false

}

function isPostCode(s) {

	var re = /^[a-zA-Z]{1,2}\d{1,2}[a-zA-Z]?\s?\d[a-zA-Z]{2}/
	return (re.test(s)) ? true : false

}

function isEmail (s){
var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
return filter.test(s);
}
/******************************************************************************/


/******************************************************************************/
/*

	Key Press functions

*/
function MC_restrictToNumeric(e) {
	return (e.keyCode < 48 || e.keyCode > 57) ? false : true
}

function MC_restrictToAlphaNumeric(e) {
    var k = e.keyCode
	return ((k > 47 && k < 58) || (k > 65 && k < 91) || (k > 96 && k < 123)) ? true : false
}

function MC_restrictToNonNumeric(e) {
    var k = e.keyCode

	return ((k > 96 && k < 123) || (k == 45) || (k==39) || (k > 64 && k < 91)) ? true : false
}




/******************************************************************************/

/******************************************************************************/
/*

	functions required for image rollover support

*/
function MC_preloadimages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
  var i,j=d.MM_p.length,a=MC_preloadimages.arguments; for(i=0; i<a.length; i++)
  if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

  function MC_swapimagerestore() { //v3.0
    var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
  }

  function MC_swapimage() { //v3.0
    var i,j=0,x,a=MC_swapimage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
     if ((x=MC_findobject(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
  }

  function MC_findobject(n, d) { //v4.0
    var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
      d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MC_findobject(n,d.layers[i].document);
    if(!x && document.getElementById) x=document.getElementById(n); return x;
  }