function validateZipCode() {

	var zip_code = document.getElementById('searchZip').value;
	var url = "/js/ajax/zipCodeExists.aspx?zip="; // The server-side script

	window.status = 'Validating zip code...';
	document.getElementById('searchZip').disabled = true;
	document.getElementById('searchZipSubmit').disabled = true;
	
	http.open("GET", url + escape(zip_code), true);

	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			document.getElementById('searchZip').disabled = false;
			document.getElementById('searchZipSubmit').disabled = false;
			window.status = '';
			isValid = http.responseText;
			if (isValid == 'True') {
				location.href = '/leads/nearby.aspx?zip=' + escape(document.getElementById('searchZip').value);
			} else {
				alert('Sorry, we were unable to find the zip code \'' + zip_code + '\'. Please try again.');
				document.getElementById('searchZip').focus();
			}
		}
	}
	http.send(null);
	return false;
}

function submitSigninForm() {
	if (document.getElementById('SignInPasswordAndSubmitWrapper').style.display == 'none') {
		emailPassword();
	} else {
		signin();
	}
	return false;
}

function revealEmailPasswordButton() {
	document.getElementById('SignInPasswordAndSubmitWrapper').style.display = 'none';
	document.getElementById('EmailMyPasswordButtonWrapper').style.display = 'block';
	document.getElementById('signinEmailAddress').focus();
	document.getElementById('signinSubmit').disabled = true;
	return false;
}

function emailPassword() {
	var emailAddress = document.getElementById('signinEmailAddress').value;
	var url = "/js/ajax/emailPasswordReminder.aspx?email=" + escape(emailAddress);

	window.status = 'Sending reminder...';
	document.getElementById('EmailPasswordSubmit').value = 'Please wait...';
	document.getElementById('EmailPasswordSubmit').disabled = true;
	
	http.open("GET", url, true);

	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			document.getElementById('signinSubmit').disabled = false;
			document.getElementById('EmailPasswordSubmit').disabled = false;
			document.getElementById('EmailPasswordSubmit').value = 'Email Me My Password';
			document.getElementById('SignInPasswordAndSubmitWrapper').style.display = 'block';
			document.getElementById('EmailMyPasswordButtonWrapper').style.display = 'none';
			document.getElementById('signinPassword').focus();
			
			window.status = '';
			isValid = http.responseText;
			if (isValid == 'true') {
				alert('Your password has been emailed to ' + emailAddress + '.');
			} else if (isValid == 'error') {
				alert('We\'re sorry, there was a problem sending your password reminder email. Please contact your account representative for assistance.');
			} else {
				alert('We\'re sorry, we were unable to find account matching that email address. Please try again, or contact your account representative for assistance.');
			}
		}
	}
	http.send(null);
	return false;
}

function getNewsItem() {
	var id = document.getElementById('TopNewsItemId').value;
	var url = "/js/ajax/getNewsItem.aspx?id=" + escape(id); // The server-side script

	window.status = 'Dismissing message...';
	
	http.open("GET", url, true);
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			window.status = '';
			results = http.responseText.split("|");
			document.getElementById('TopNewsItemId').value = results[0];
			document.getElementById('TopNewsItemTitle').innerHTML = results[1];
			document.getElementById('TopNewsItemMessage').innerHTML = results[2];
			document.getElementById('TopNewsItemTitle').setAttribute('style', results[3]);
			document.getElementById('TopNewsItemMessage').setAttribute('style', results[4]);
			if (results[5] == 'False') document.getElementById('TopNewsItemDismiss').style.display = 'none';
		}
	}
	http.send(null);
	return false;
}

function getAccountData() {
	var id = document.getElementById('LocalOffices')[document.getElementById('LocalOffices').selectedIndex].value;
	var url = "/js/ajax/accountAtAGlance.aspx?id=" + escape(id); // The server-side script
	window.status = 'Retrieving account data...';
	
	http.open("GET", url, true);
	http.onreadystatechange = function() {
		if (http.readyState == 4) {
			window.status = '';
			document.getElementById('AccountAtAGlanceData').innerHTML = http.responseText;
			blinkOffice(7)
		}
	}
	http.send(null);
	return false;
}

function blinkOffice(blinks) {
	if(blinks > 0) {
		var e = document.getElementById('LocalOffices');

		var visibility = 'visible';
		if ((blinks/2) == parseInt(blinks/2))
			visibility = 'hidden';
		e.style.visibility = visibility;

		blinks = blinks - 1
		setTimeout('blinkOffice(' + blinks + ')', 50)
	}
}

function displayRecordingWindow() {
	window.open('/popup/samplerecording/', 'sample', 'width=600,height=435,toolbar=no,menubar=0,scrollbars=no,resizable=yes,location=no,status=0');
	return false;
	
}

function displayMoreResults() {
	document.getElementById('MoreResults').className = '';
	document.getElementById('MoreResultsLink').style.display = 'none';
}

function stripeProjectList() {
	stripe('RfpList');
}

function ReadCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return "";
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function SetCookie(cookieName,cookieValue,nDays) {
 var today = new Date();
 var expire = new Date();
 if (nDays==null || nDays==0) nDays=1;
 expire.setTime(today.getTime() + 3600000*24*nDays);
 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}

function cookiesEnabled() {
	var testValue = Math.floor(1000*Math.random());
	SetCookie('AreCookiesEnabled', testValue);
	if (testValue == ReadCookie('AreCookiesEnabled'))
		return true
	else
		return false;
}

function initpage() {
	updateTimeRemaining();
	stripeProjectList();
	handleWindowResize();

	if(document.getElementById('SignInFormWrapper')) {
		if(!http) {
			document.getElementById('NoAjax').style.display = 'block';
		} else if (!cookiesEnabled()) {
			document.getElementById('NoCookies').style.display = 'block';
		} else {
			document.getElementById('SignInFormWrapper').style.display = 'block'
		}
	}
}

window.onload = initpage;