var popWin = "";
// * FORECOLOUR POPUP WINDOW
function popwindow(pageaddress){

var w = 480, h = 340;

if (document.all) {

   w = document.body.clientWidth;
   h = document.body.clientHeight;
}
else if (document.layers) {
   w = window.innerWidth;
   h = window.innerHeight;
}

var popW = 275, popH = 200;
var leftPos = (w-popW)/2, topPos = (h-popH)/2;

if (popWin != ""){popWin.close()}
popWin = window.open(""+pageaddress+"","window","toolbar=0,status=0,menubar=0,scrollbars=0,resizable=0,width=" + popW + ",height="+popH+",top="+topPos+",left="+leftPos)
window.opener = self;

}

if (false&&document.images)
{
	house5On=new Image;
	house5On.src="../images/house_on.gif";
	house5Off=new Image;
	house5Off.src="../images/house_off.gif";
	
	mail5On=new Image;
	mail5On.src="../images/mail_on.gif";
	mail5Off=new Image;
	mail5Off.src="../images/mail_off.gif";
	
	home5On=new Image;
	home5On.src="../images/home_on.jpg";
	home5Off=new Image;
	home5Off.src="../images/home_off.jpg";
	
	about5On=new Image;
	about5On.src="../images/about_on.jpg";
	about5Off=new Image;
	about5Off.src="../images/about_off.jpg";
	
	solbus5On=new Image;
	solbus5On.src="../images/solbus_on.jpg";
	solbus5Off=new Image;
	solbus5Off.src="../images/solbus_off.jpg";
	
	solhome5On=new Image;
	solhome5On.src="../images/solhome_on.jpg";
	solhome5Off=new Image;
	solhome5Off.src="../images/solhome_off.jpg";
	
	human5On=new Image;
	human5On.src="../images/human_on.jpg";
	human5Off=new Image;
	human5Off.src="../images/human_off.jpg";
	
	products5On=new Image;
	products5On.src="../images/products_on.jpg";
	products5Off=new Image;
	products5Off.src="../images/products_off.jpg";
	
	links5On=new Image;
	links5On.src="../images/links_on.jpg";
	links5Off=new Image;
	links5Off.src="../images/links_off.jpg";
	
	contact5On=new Image;
	contact5On.src="../images/contact_on.jpg";
	contact5Off=new Image;
	contact5Off.src="../images/contact_off.jpg";
	
	terms5On=new Image;
	terms5On.src="../images/terms_on.jpg";
	terms5Off=new Image;
	terms5Off.src="../images/terms_off.jpg";
}

function on(pic)
{
if (document.images)
	{
	document.images[pic].src=eval(pic + "On.src");
	}
}

function off(pic)
{
if (document.images)
	{
	document.images[pic].src=eval(pic + "Off.src");
	}
}

function emailCheck (element) 
{
	emailStr=element;
	
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	var matchArray=emailStr.match(emailPat)
	
	if (matchArray==null) {
		return false
	}
	
	var user=matchArray[1]
	var domain=matchArray[2]
	
	// See if "user" is valid 
	if (user.match(userPat)==null) {
		// user is not valid
		return false
	}
	
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		// this is an IP address
		  for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
			return false
			}
		}
		return true
	}
	
	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		return false
	}
	
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	
	if (domArr[domArr.length-1].length<2 || 
		domArr[domArr.length-1].length>3) {
	   // the address must end in a two letter or three letter word.
	   return false
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="This address is missing a hostname!"
	   return false
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}


//  End -->

function ShowTR(id)
{
	if(document.getElementById(id).style.display == 'none')
		document.getElementById(id).style.display = 'block';
	else
		document.getElementById(id).style.display = 'none';
}

function checkphone(input)
{
	var returnval = false;
	input=input.replace(/[ ()]/g, "");
	var re = new RegExp('^[+]{0,1}[0]{0,2}[1-9][0-9]{8,}$');
	if (input.match(re)) {
		returnval = true;
	}
	return returnval;
}

function checkdate(input)
{
	var returnval = false;
	
	var re = new RegExp('(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d');
	if (input.match(re)) {
		returnval = true;
	}	
	return returnval;
}

function checkdob(strDate)
{
 if (strDate=="") { return false; }
 arrBits=strDate.split("/");
 if (arrBits.length!=3) { return false; }
 if (isNaN(iDay=parseInt(arrBits[0],10))) return false;
 if (isNaN(iMonth=parseInt(arrBits[1],10)-1)) return false;
 if (isNaN(iYear=parseInt(arrBits[2],10))) return false;
 if ((iYear<1900)||(iYear>2500)) return false;
 if ((iMonth<0)||(iMonth>11)) return false;
 if ((iDay<1)||(iDay>31)) return false;
 dtMonthEnd=new Date(iYear, iMonth+1, 0);
 iMaxDay=dtMonthEnd.getDate();
 if (iDay>iMaxDay) iDay=iMaxDay;
 
 return true;
}

function checkcurrency(input)
{
	var returnval = false;
	
	var re = new RegExp('^\\d+(\\.\\d\\d){0,1}$');
	if (input.match(re)) {
		returnval = true;
	}	
	return returnval;
}

function checknumber(input)
{
	var returnval = false;
	var re = new RegExp('^[0-9]\\d*$');
	if (input.match(re)) {
		returnval = true;
	}	
	return returnval;
}

function checksortcode(input)
{
	var returnval = false;
	
	var re = new RegExp('^([0-9]{2})$');
	if (input.match(re)) {
		returnval = true;
	}	
	return returnval;
}

function checktime(input)
{
	var returnval = false;
	
	var re = new RegExp('^([01]?[0-9]|[2][0-3])(:[0-5][0-9])?$');
	if (input.match(re)) {
		returnval = true;
	}	
	return returnval;
}

function checkccard(input, nl)
{
	var returnval = false;
	
	var re = new RegExp('^([0-9]{'+nl+'})$');
	if (input.match(re)) {
		returnval = true;
	}	
	return returnval;
}

function checkrequired(which) 	
{
	var pass=true;

	if (document.images) 
	{
		for (i=0;i<which.length;i++)
		{
			var tempobj=which.elements[i];
			//if (tempobj.name.substring(0,8)=="required") 
			if (tempobj.getAttribute("required") == "1")
			{
			if (
    			(
          	(tempobj.nodeName.toUpperCase()=='INPUT')
          	&&
          	(
    					(tempobj.type=="checkbox" && tempobj.checked==false)
          		||
          		(
              	(
                	tempobj.type=="text"
                	||
                  tempobj.type=="password"
                  ||
                  tempobj.type=="textarea"
                 )
                 &&
                 tempobj.value==''
               )
            )
          )
          ||
          (
          	tempobj.nodeName.toUpperCase()=='SELECT'
            &&
            tempobj.selectedIndex<=0
          )
          ||
          (
          	tempobj.nodeName.toUpperCase()=='TEXTAREA'
            &&
          	tempobj.value==''
          )
        ) 
				{
					pass = false;
					break;
				}
				else
				{
					if (tempobj.getAttribute("isdate") == "1")
					{
						if (checkdate(tempobj.value) == false)
						{
							pass = false;
							break;
						}
					}
					if (tempobj.getAttribute("isdob") == "1")
					{
						if (checkdob(tempobj.value) == false)
						{
							pass = false;
							break;
						}
					}
					if (tempobj.getAttribute("istime") == "1")
					{
						if (checktime(tempobj.value) == false)
						{
							pass = false;
							break;
						}
					}
					if (tempobj.getAttribute("iscurrency") == "1")
					{
						if (checkcurrency(tempobj.value) == false)
						{
							pass = false;
							break;
						}
					}
					if (tempobj.getAttribute("isnumber") == "1")
					{
						if (checknumber(tempobj.value) == false)
						{
							pass = false;
							break;
						}
						if (tempobj.getAttribute("lnum"))
						{
							if (parseFloat(tempobj.value) < parseFloat(tempobj.getAttribute("lnum")))
							{
								pass = false;
								break;
							}
						}
						if (tempobj.getAttribute("unum"))
						{
							if (parseFloat(tempobj.value) > parseFloat(tempobj.getAttribute("unum")))
							{
								pass = false;
								break;
							}
						}
					}
					if (tempobj.getAttribute("isemail") == "1")
					{
						if(emailCheck(tempobj.value) == false)
						{
							pass = false;
							break;
						}
					}
					if (tempobj.getAttribute("isphone") == "1")
					{
						if(checkphone(tempobj.value) == false)
						{
							pass = false;
							break;
						}
					}
					if (tempobj.getAttribute("issortcode") == "1")
					{
						if(checksortcode(tempobj.value) == false)
						{
							pass = false;
							break;
						}
					}
					if (tempobj.getAttribute("isccard") == "1")
					{
						if (tempobj.value.length >= 13)
						{
							if(checkccard(tempobj.value, tempobj.value.length) == false)
							{
								pass = false;
								break;
							}
						}
						else
						{
							pass = false;
							break;
						}
					}
					if (tempobj.getAttribute("tomatch"))
					{
						if(tempobj.value != document.getElementById(tempobj.getAttribute("tomatch")).value)
						{							
							pass = false;
            if (tempobj.getAttribute("cnm")) {
            	window.alert(tempobj.getAttribute("cnm"));
            } else {
								window.alert ("Please make sure the " + tempobj.name.toUpperCase() + " field matches the " + tempobj.getAttribute("tomatch").toUpperCase() + " field.");
            }
							tempobj.focus;
							return false;
						}
					}
				}
			}
		}
	}
	
	if (!pass) 
	{
		if(tempobj.getAttribute("ct"))
		{
			alert(tempobj.getAttribute("ct"));
		}
		else
		{
			//shortFieldName=tempobj.name.substring(8,30).toUpperCase();
			shortFieldName=tempobj.name.toUpperCase();
			alert("Please make sure the "+shortFieldName+" field was properly completed.");
		}
		
		if (tempobj.disabled != true)
			tempobj.focus();
			
		return false;
	}
	else
	{
		return true;
		//SUBMIT FORM HERE	
	}
}
//  End -->

function CheckCover(sFN, bVerbose)
{
	var bBCov=(document.forms[sFN].COVER_BUILDINGS_COVERREQ.selectedIndex==0);
	var oBAD=document.forms[sFN].COVER_BUILDINGS_ACCDAMAGE;
	var oBSum=document.forms[sFN].COVER_BUILDINGS_SUM_INSURED;
	var oBLib=document.forms[sFN].COVER_PROPERTYLI_REQ;
	var oBExc=document.forms[sFN].COVER_BUILDINGS_EXCESSINC;
	
	var bCCov=(document.forms[sFN].COVER_CONTENTS_COVERREQ.selectedIndex==0);
	var oCAD=document.forms[sFN].COVER_CONTENTS_ACCDAMAGE;
 var bCAD=(oCAD.selectedIndex==0);
	var oCSum=document.forms[sFN].COVER_CONTENTS_SUM_INSURED;
	var oCLib=document.forms[sFN].COVER_PUBLICLI_REQ;
	
	if (bBCov) {
		// Buildings cover selected
		oBAD.disabled = false;
		oBSum.disabled = false;
		oBSum.setAttribute("required", "1");
		if (oBLib) {
			oBLib.disabled=false;
		}
		if (oBExc) {
			oBExc.disabled=false;
		}
	} else	{
		// No Buildings cover selected
		oBAD.selectedIndex = 1;
		oBAD.disabled = true;
		oBSum.value = "0";
		oBSum.disabled = true;
		oBSum.setAttribute("required", "0");
		if (oBLib) {
			oBLib.selectedIndex=0;
			oBLib.disabled=true;
		}
		if (oBExc) {
			oBExc.selectedIndex=0;
			oBExc.disabled=true;
		}
		// Contents only must have AD
		if (bCCov&&(!bCAD)) {
			if (bVerbose) {
				window.alert("Contents insurance must include the accidental damage extension if taken without buildings cover.");
			}
			oCAD.selectedIndex=0;
		}
	}
	if (bCCov) {
		// Contents cover selected
		oCAD.disabled = false;
		oCSum.disabled = false;
		oCSum.setAttribute("required", "1");
		if (oCLib) {
			oCLib.disabled=false;
		}
	} else	{
		// No Contents cover selected
		oCAD.selectedIndex = 1;
		oCAD.disabled = true;
		oCSum.value = "0";
		oCSum.disabled = true;
		oCSum.setAttribute("required", "0");
		if (oCLib) {
			oCLib.selectedIndex=0;
			oCLib.disabled=true;
		}
	}
}

function addFav()
{
  var url="http://www.landlords-building-insurance.co.uk"
  var title="Landlords Buildings Insurance"
  if (document.all) {
  	window.external.AddFavorite(url,title);
		}
}

function readSelectText(oSelect)
{
	if (!oSelect) return "";
	if (oSelect.options.length<=0) return "";
	if (oSelect.selectedIndex<0) return "";
	
	return oSelect.options[oSelect.selectedIndex].text;
}


function JSTrimEx(sString) {
	// Remove whitespace &crlf from LHS
	bRun=true;
	do {
		c=sString.charCodeAt(0);
		switch (c) {
			case 32:
			case 13:
			case 10:
				sString=sString.substr(1);
				break;
			default:
				bRun=false;
				break;
		}	
	} while ((bRun)&&(sString!=""));
	
	// Remove whitespace &crlf from LHS
	bRun=true;
	do {
		iLen=sString.length;
		c=sString.charCodeAt(iLen-1);
		switch (c) {
			case 32:
			case 13:
			case 10:
				sString=sString.substr(0, iLen-1);
				break;
			default:
				bRun=false;
				break;
		}	
	} while ((bRun)&&(sString!=""));
	
	return sString;
}
function readSelectVal(oSelect) {
	if (!oSelect) return "";
	if (oSelect.options.length<=0) return "";
	if (oSelect.selectedIndex<0) return "";
	
	return JSTrimEx(oSelect.options[oSelect.selectedIndex].value);
}