if (document.images) 
{
	img1on = new Image();
	img1on.src = "images/buysellrenton.gif";  
	img1off = new Image();
	img1off.src = "images/buysellrentoff.gif"; 
	img2on = new Image();
	img2on.src = "images/financeon.gif";  
	img2off = new Image();
	img2off.src = "images/financeoff.gif"; 
	img3on = new Image();
	img3on.src = "images/remodelingon.gif";  
	img3off = new Image();
	img3off.src = "images/remodelingoff.gif"; 
	img4on = new Image();
	img4on.src = "images/homeserviceson.gif";  
	img4off = new Image();
	img4off.src = "images/homeservicesoff.gif"; 
}

function imgOn(imgName) 
{
	if (document.images) 
	{
		document[imgName].src = eval(imgName + "on.src");
    }
}
function imgOff(imgName) 
{
	if (document.images) 
	{
    	document[imgName].src = eval(imgName + "off.src");        
    }
}

function getPositionAndDimension(element) 
{
  var left = 0;
  var top = 0;
  var width = element.offsetWidth;
  var height = element.offsetHeight;
  while (element.offsetParent != null) 
  {
    left += element.offsetLeft;
    top += element.offsetTop;
    element = element.offsetParent;
  }
	
  var results = new Array(4);
  results['left'] = left;
  results['top'] = top;
  results['width'] = width;
  results['height'] = height;
  return results;
}

function LeadFormCheck(optionformat)
{
	var errors = "";
	if (document.thisform.fname.value=="") 
	{
    	errors = errors + "- First Name must be entered\n";
    }
	if (document.thisform.lname.value=="") 
	{
    	errors = errors + "- Last Name must be entered\n";
    }
	if (document.thisform.address1.value=="") 
	{
    	errors = errors + "- Address must be entered\n";
    }
	if (document.thisform.city.value=="") 
	{
    	errors = errors + "- City must be entered\n";
    }
	if (document.thisform.state.value=="") 
	{
    	errors = errors + "- State must be entered\n";
    }
	if (document.thisform.zip.value=="") 
	{
    	errors = errors + "- Zip code must be entered\n";
    }
    else
    {
		if (validateZIP(document.thisform.zip.value)==false)
		{
			errors = errors + "- Invalid zip code\n";
		}
    }
	if (document.thisform.phone.value=="") 
	{
    	errors = errors + "- Phone must be entered\n";
    }
    else
    {
		if (ValidateUSNumber(document.thisform.phone.value)==false)
		{
			errors = errors + "- Invalid Phone Number\n";
		}
    }
	if (document.thisform.email.value=="") 
	{
    	errors = errors + "- Email Address must be entered\n";
    }
    else
    {
		if (email_check(document.thisform.email.value)==false)
		{
			errors = errors + "- Invalid Email Address\n";
		}
		else
		{
			if (document.thisform.confirmemail.value!=document.thisform.email.value) 
			{
				errors = errors + "- Email and Confirm Email must match\n";
			}
		}
    }
    
    //if (optionformat == "site")
    //{
	//	if(checkEmpty(checkradiovalue(document.thisform.optin))) 
	//	{
	//		errors = errors + "- You must verify that your information is accurate\n";
	//	}
	//}	

	qerrors = "";
	for (var i=1; i <= 20; i++) 
	{
		var questerror = SYS_trackObject("quest"+i+"error");
		if (questerror)
		{
			errorfound = "no";
			var item = SYS_trackObject("quest"+i);
			if (item.length>0 && item.value == undefined) 
			{
				if(checkEmpty(checkradiovalue(item))) 
				{
					var qerrors = qerrors + "- " + questerror.value + "\n";
					errorfound = "yes";
				}
			}
			else 
			{ 
				if(checkEmpty(item.value)) 
				{ 
					var qerrors = qerrors + "- " + questerror.value + "\n";
					errorfound = "yes";
				}
			}
			if (errorfound=="no" && item.type=="checkbox")	
			{
				if(checkEmpty(checkradiovalue(item))) 
				{
					var qerrors = qerrors + "- " + questerror.value + "\n";
				}
			}
		}	
	}
	if (qerrors != "")
	{
		errors = errors + qerrors;
	}

	if (errors!="")
	{
		alert (errors);
		return false;
	}
	return true;
}

function SYS_trackObject(n, d) {
  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=SYS_trackObject(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;}

function MM_openBrWindow(theURL,winName,features) 
{
	window.open(theURL,winName,features);
}

function openWindow(theURL,winName,features) 
{
	NewWindow=window.open(theURL,winName,features);
	NewWindow.focus()
}

function validateZIP(zipcode) 
{
	var valid = "0123456789";
	if (zipcode.length!=5) 
	{
		return false;
	}
	for (var i=0; i < zipcode.length; i++) 
	{
		temp = "" + zipcode.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") 
		{
			return false;
		}
	}
	return true;
}

function zipFormCheck()
{
	var errors = "";
	if (document.thisform.zip.value=="") 
	{
    	errors = errors + "zip code must be entered\n";
    }
    else
    {
		if (validateZIP(document.thisform.zip.value)==false)
		{
			errors = errors + "invalid zip code\n";
		}
    }
	if (errors!="")
	{
		alert (errors);
		return false;
	}
	return true;
}

function FormatPhone(phone)
{
	phonetemp=phone.value
	if(phonetemp.length==3)
	{
		pphonetemp=phonetemp;
	    digit1=phonetemp.indexOf('(');
	    digit4=phonetemp.indexOf(')');
	    if(digit1==-1)pphonetemp="("+pphonetemp;
	    if(digit1!=0 && digit4==-1)pphonetemp=pphonetemp+")";
	    phone.value="";
	    phone.value=pphonetemp;
	}

	if(phonetemp.length>3)
	{
		digit1=phonetemp.indexOf('(')
	    digit2=phonetemp.indexOf(')')
	    if (digit2==-1)
	    {
    		templength=phonetemp.length;
	        temp1=phonetemp.substring(0,4);
	        temp1=temp1+")"
	        temp2=phonetemp.substring(4,templength);
	        pphonetemp=temp1+temp2;
	        phone.value="";
	        phone.value=pphonetemp;
		}
	}
	if(phonetemp.length>5)
	{
		temp1=phonetemp.substring(digit1+1,digit2);
	    if(temp1.length>3)
	    {
	    	temp2=temp1;
	        templength1=temp2.length;
	        templength2=phonetemp.length
	        areacode=temp1.substring(0,3);
	        exchange=temp1.substring(3,templength1);
	        number=phonetemp.substring(digit2+1,templength2);
	        phone.value="";
	        pphonetemp="("+areacode+")"+exchange+number;
	        phone.value=pphonetemp;
		}
	    templength3=phonetemp.length;
	    temp6=phonetemp.substring(digit2+1,templength3);
	    templength4=temp6.length;
	    if(templength4>3&&temp6.indexOf('-')==-1)
	    {
	    	exchange=phonetemp.substring(digit2+1,digit2+4);
	        number=phonetemp.substring(digit2+4,templength3);
	        areacode=phonetemp.substring(0,digit2+1);
	        pphonetemp=areacode+exchange+"-"+number;
	        phone.value="";
	        phone.value=pphonetemp;
	    }    
	}
}

function ValidateUSNumber(phonenumber) { 
	var CleanedString=""; var index = 0; var LimitCheck; 
 	LimitCheck = phonenumber.length;
 	while (index != LimitCheck) { 
  		if (isNaN(parseInt(phonenumber.charAt(index)))) { } 
	  	else { CleanedString = CleanedString + phonenumber.charAt(index);} 
	  	index = index + 1; }
	if (CleanedString.length == 10) { 
	 	phonenumber = "(" + CleanedString.substring(0,3) + ") " + CleanedString.substring(3,6) + "-" + CleanedString.substring(6,10); }
	else { CleanedString = phonenumber; return false; } } 

function email_check(string){
	var addressPattern = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	return addressPattern.test(string);}

function checkradiovalue(f)
{
	if (f.length>0){
		for (var i=0; i < f.length; i++){
			if (f[i].checked){
				return rad_val = f[i].value;}}}
	else { if (f.checked){
			return rad_val = f.value;}}    
	return null;
}
function checkEmpty(strng) 
{
	if (strng == null || strng == "") {
		return true;	}
	for (i=0;i<strng.length;i++) {
		if (strng.charAt(i) != ' ') return false;}
	return true;
}

function limitlength(item, element, size)
{
  	returnvalue=true;
    if (!element.which && item.value.length>=size)
    returnvalue=false;
    else if (element.which && element.which!=8 && item.value.length>=size)
    returnvalue=false;
    return returnvalue
}

function cm_response(option) 
{
	if (option == "logout")
	{
		if (confirm('Sure to Log Out from Content Manager?'))
		{
			window.location="production/index.php?o=logout";
		}
	}	
	return false;
}
