// FROM THE MULTIHOUSING ROOT


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


// DOCUMENT SEARCH PARAMETER HANDLER
	function setProcessors()						// HIDE SECOND AND THIRD OTHER PROCESSORS LIST
		{
		var temp;
//		temp=what.value;
		temp = document.getElementById('listsProcessor1').options[document.getElementById('listsProcessor1').selectedIndex].value;
			if(temp == "We process EVERYTHING in our own facility")
			{
			document.getElementById('divOtherprocessors').style.display="none";
			}
			else
			{
			document.getElementById('divOtherprocessors').style.display="block";
			}
		}


	function onstart()
		{
		if(location.search)
			{
			temp = location.search;
			if (temp.length < 2) { return false; }
			temp=temp.substring(1,temp.length);
			// username - password problems
			if(temp == "unpw")
				{
				alert("\n____________________________\n\nUSERNAME / PASSWORD PROBLEM\n\nPlease try again.");
				}
			// this goes last
			self.location.search="";
			}
		}


// CHECK PHONE NUMBER FORMAT
function fixPhoneNumber(what){

var inputtocheck=what;
var num=inputtocheck.value;
var inputtocheck=inputtocheck.id;

var re= /\D/;
// test for this format: (xxx)xxx-xxxx
var re2 = /^\({1}\d{3}\)\d{3}-\d{4}/; 

var newNum;
 if (num != "" && re2.test(num)!=true){
   if (num != ""){
     while (re.test(num)){
     num = num.replace(re,"");
     }
   }
  if (num.length != 10){
	alert('Please enter a 10 digit phone number  ');
	document.getElementById(inputtocheck).select();
	//return;
}
   else {
     // for format (xxx)xxx-xxxx
     newNum = '(' + num.substring(0,3) + ') ' + num.substring(3,6) + '-' + num.substring(6,10);
     // for format xxx-xxx-xxxx
     // newNum = num.substring(0,3) + '-' + num.substring(3,6) + '-' + num.substring(6,10);
//	eval(document.getElementById(inputtocheck).value=newNum);
	document.getElementById(inputtocheck).value=newNum;
     }
   }
  }



function checkEmailAddress(what)
	{
	var inputtocheck=what;
	var theaddress=inputtocheck.value;
	if(theaddress=="")
		{
		return false;
		}
	theaddress=stripSpaces(theaddress);
	if((theaddress.indexOf(".") > 2) && (theaddress.indexOf("@") > 0))
		{
		inputtocheck.value=theaddress;
		return;
		}
	else{
		alert("Please use a valid email address.");
		//inputtocheck.select();
		inputtocheck.focus();
		return;
		}
	return
	}
 

function validate(what)
	{
	var whichform;
	var i;
	var temp;

	whichform=what;

	// THIS VALIDATES THE TEXT BOXES

	for(i=1;i<fieldID.length;i++)
		{
			if(document.getElementById(fieldID[i]).value=="")
			{
			alert(fieldtitle[i] + " must not be blank.");
			document.getElementById(fieldID[i]).focus();
			document.getElementById(fieldID[i]).select();
			return false;
			}
		}
		
	if(whichform=="contact")		// RETURNS TRUE IF JUST CHECKING TEXT BOXES
		{
		return true;
		}


	// THIS VALIDATES THE DROPDOWN LIST

/*	temp=document.getElementById('selectAcctBuildingType').options.selectedIndex;
	temp=document.getElementById('selectAcctBuildingType').options[temp].value;
	if(temp == "select")
		{
		document.getElementById('selectAcctBuildingType').focus();
		alert("Please select a building type.");
		return false;
		}
	*/
	// THIS VALIDATES THE CHECKBOXES

	var checkboxes = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
	var totalchecked = 0;

	for (i in checkboxes)
		{
		temp="checkAcctPickUp" + checkboxes[i];
		totalchecked = totalchecked + document.getElementById(temp).checked;
		}
	if(totalchecked==0)
		{
		document.getElementById("checkAcctPickUpMonday").focus();
		alert("Please select at least one pickup day")
		return false;
		}
	return true;
	}	

function stripSpaces (InString) 
	{
	OutString="";
	for (Count=0; Count < InString.length; Count++)
		{
		TempChar=InString.substring (Count, Count+1);
		if (TempChar!=" ")
			OutString=OutString+TempChar;
		}
	return (OutString);
	}


