/* It checks the input */
function check_citySearch_field(id){
	var textField = document.getElementById(id);		
	if(textField.value == '[City or Zip Code]' || textField.value.length <= 0){ 
		alert('Please Enter a City or Zip Code');
		checkClearText(document.getElementById(id));
		textField.value = "";
		textField.focus();		
		return false;	
	}else{		
		document.formQuickSearch.submit();
	}
}

/* checks for an empty text field - spaces are considered empty */
function is_white_space (strVar){
	var i = 0;
	
	if (strVar.length <= 0) { return true; }
	
	for (i=0; i<strVar.length; i++){
		if (strVar.charAt(i) != ' ') { return false; }
	}
	return true;
}

// check to see if we have a valid email address
function is_email (emailstring){
	var i;
	var atcount		=	0;
	var dotcount	=	0;
	
	if (emailstring.indexOf('@') < 1 || emailstring.indexOf('.') < 3 || emailstring.indexOf('.') > emailstring.length-2){
		return false;
	}else{
		for (i=0;i<emailstring.length;i++){
			if (emailstring.charAt(i) == "@") { atcount++; }
			if (atcount > 0 && emailstring.charAt(i) == ".") { dotcount = i; }
		}
		if (atcount > 1 || dotcount > emailstring.length-2) { return false; }
	}
	return true;
}

// removes white space
function trimString (str){
	var strLocal = str;
	
	while (strLocal.charAt(0) == ' ') { strLocal = strLocal.substring(1); }
	while (strLocal.charAt(strLocal.length - 1) == ' ') { strLocal = strLocal.substring(0, strLocal.length - 1); }
	return strLocal;
}

function getCookieValue (cookie, name) {
	var nameEQ = name + "=";
	var ca = 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 '';
}

/* Home page Address, City or Zip code search*/
function validateSearchInputs(pSearchtype){
	var cityZip, sellingAddr;
	var isValidInput = false;
	var validAddress = true;
	var validCity = false;	
	
	if (pSearchtype == "selling"){				
		sellingAddr = document.getElementById('address').value;
		validAddress = isValidAddress(sellingAddr);

	    if(validAddress){
	        validCity = isValidCityName(document.getElementById('citystatezip'));	
	    }
	    	    
		if ((validAddress) && (validCity)){ 
		    document.getElementById('formSellingSearch').submit();	
		}else {
		    if(!validAddress){
			    alert('Please enter Street Address.');			    
			    document.getElementById('address').style.color = "#000000";
			    document.getElementById('address').focus();
			    return false;
		    }		
		    else if(!validCity){
			    alert('Please enter the full City Name or a 5-digit Zip Code.');			    
			    document.getElementById('citystatezip').style.color = "#000000";
			    document.getElementById('citystatezip').focus();
			    return false;
		    }							
		}
	}else if (pSearchtype == 'address'){
		if(trimString(document.getElementById('address').value).length == 0){
			alert('Please enter a Street Name or House Number.');
			document.getElementById('address').focus();
		}else if(trimString(document.getElementById('address').value).length <= 2){
			alert('A minium of three characters are required to search Address.');
			document.getElementById('address').select();
		}else if(trimString(document.getElementById('city').value).length == 0){
			if(trimString(document.getElementById('zip').value).length == 0){
				alert('Please enter a City or Zip Code.');
				document.getElementById('city').select();
			}else if(trimString(document.getElementById('zip').value).length < 5){
				alert('A minium of five characters are required to search Zip Codes.');
				document.getElementById('zip').select();
			}else{
				document.formAddressSearch.submit();
			}
		}else if(trimString(document.getElementById('city').value).length <= 2){
			alert('A minium of three characters are required to search City and State.');
			document.getElementById('city').select();
		}else{
			document.formAddressSearch.submit();
		}
	}else if (pSearchtype == "buying"){		
		validCity = isValidCityName(document.getElementById('qsCityZip'));	
		if (document.getElementById('state')){
			//we have a state dropdown so we need to make sure the user has selected a state
			if (document.getElementById('state').options[document.getElementById('state').selectedIndex].value == ''){
				alert('You must select a state from the dropdown list');
				document.getElementById('state').focus();
				return false;
			}
		}
		
		if(!validCity){
			alert('Please enter the full City Name or a 5-digit Zip Code.');			
			document.getElementById('qsCityZip').style.color = "#000000"
			document.getElementById('qsCityZip').focus();
			return false;	
		}else {		
			document.getElementById('formBuyingSearch').submit();							
		}
	}	
}

function isValidAddress(pAddress){
	var validChars = /\w{2,}/;

	if ((pAddress != 'Enter Street Address') && (pAddress != 'Enter Address')){	
		if (pAddress.search(validChars) != -1){		
			return true;
		}else{
		    document.getElementById('address').value = "";		   		         
	        document.getElementById('address').focus();
		    return false;	
		 }		    	
	}
	
	document.getElementById('address').value = "";					
	return false;
}

function isValidCityName(pCity){	
	var validChars = /\w{2,}/;	
	var cityValue = pCity.value;
	
	if ((cityValue != 'Enter City or Zip') && (cityValue != 'City / Zip code')&& (cityValue != '-- Enter City, State --')){		
		if (cityValue.search(validChars) != -1){					
			return true;
		}else{
		    pCity.value = "";
		    pCity.style.color = '#000000';
		    return false;
		}		
	}else{		
		pCity.value = "";
		pCity.style.color = '#000000';
		return false;
	}
}

function checkClearText(pObj){
	if ((pObj.value=='Enter City or Zip') || (pObj.value=='Enter Street Address')){
		pObj.value='';
		pObj.style.color = '#000000';
	}
	else if (pObj.value=='Enter Email Address'){
		pObj.value='';
		pObj.style.color = '#000000';
		return;
	}else if (pObj.value=='Enter Password'){
		pObj.value='';
		pObj.style.color = '#000000';
		return;
	}else if (pObj.value=='[City or Zip Code]'){
		pObj.value='';
		pObj.style.color = '#000000';
		return;
	}
	else if (pObj.value=='City / Zip code'){
		pObj.value='';
		pObj.style.color = '#000000';
	}
	else if (pObj.value=='Enter Address'){
		pObj.value='';
		pObj.style.color = '#000000';
		return;	
	}
	else if (pObj.value=='email address'){
		pObj.value='';
		pObj.style.color = '#000000';
	}
	else if (pObj.value=='enter password'){
		pObj.value='';
		pObj.style.color = '#000000';
	}
	else if (pObj.value=='agent name'){
		pObj.value='';
		pObj.style.color = '#000000';
	}		
	else if (pObj.value=='-- Enter City, State --'){
		pObj.value='';
		pObj.style.color = '#000000';
	} 
	
}

function trimString (str) {
	while (str.charAt(0) == ' ')
		str = str.substring(1);
	while (str.charAt(str.length - 1) == ' ')
		str = str.substring(0, str.length - 1);
	return str;
}

function validlogin() {
	if (!validEmail(document.Members.username.value)) {
		alert("Invalid E-mail Address.");
		document.getElementById('username').value = "";
		document.getElementById('username').focus();
		return false;
	}
	return true;
}

function validEmail(email) {
	invalidChars="/:,;";
	atPos=email.indexOf("@",1);
	periodPos= email.indexOf(".",atPos);
	for (i=0; i<invalidChars.length;i++) {
		badChar = invalidChars.charAt(i);
		if (email.indexOf(badChar,0)>-1) {
			return false;
		}
	}
	if(atPos==-1) {
		return false;
	}
	if (email.indexOf("@",atPos+1)>-1) {
		return false;
	}
	if (periodPos==-1) {
		return false;
	}
	if (periodPos+3 > email.length) {
		return false;
	}
	return true;
}

function chkAgentSearch(strPName) {	
	if (!isValidName(document.getElementById(strPName).value)) {
		alert("Enter at least 3 Alphabet characters of an Agent's Name");		
		document.getElementById(strPName).value = "";
		document.getElementById(strPName).style.color = '#000000';
		document.getElementById(strPName).focus();
		return false;
	} else {
		document.location = 'default.asp?p=agentResults.asp&search=' + document.getElementById(strPName).value
		try{showSearchScreen()}catch(e){}
		return false;
	}
}

function isValidName(pName){	
	var validChars = /\w{3,}/;	
	var nameValue = trimString (pName);
	
	if (nameValue == 'agent name' || nameValue == ''){		
		return false;		
	}else{ // Agent Name input has a name		
	    if (nameValue.search(validChars) != -1){					
		    return true;
		}else{		   
		   return false;
		}			
	}
}
//Opens a named standardized window
//pMLSID		- MLSID of Listing
//pMLSNumber	- MLSID Number of Listing
//pAddition		- Additional QS parameters
//pSiteDir		- Site Directory for completing URL
function popupListings(pMLSID, pMLSNumber, pAddition, pSiteDir){
	var intHeight	= window.screen.height * .85;
	var intWidth	= 660;
	var strTitle	= 'listing' + pMLSID + pMLSNumber;
	var strURL		= '/' + pSiteDir + '/modules/internet/search/includes/mapsearch/listingpopup.asp';
	var strQS		= '?mlsid=' + pMLSID + '&mlsnumber=' + pMLSNumber + '&l=y' + pAddition;
	var strSpecs	= 'resizable=yes, status=yes, location=yes, scrollbars=yes, menubar=yes, width=' + intWidth + ', height=' + intHeight;
	
	window.open(strURL+strQS, strTitle, strSpecs);
}

