function getUrlParam(strParamName){
	var strHref = window.location.href;
	if (strHref.indexOf("?") > -1) {
		var strQueryString = strHref.substr(strHref.indexOf("?"));
		var aQueryString = strQueryString.split("&");
		for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
			if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
				var aParam = aQueryString[iParam].split("=");
				return unescape(aParam[1]);
			}
		}
	}
	return "";
} 

voipAvailabilityFormJs = new Object();
voipAvailabilityFormJs.postToUrl = "http://torrent-beta.bandwidth.com/BWC.BigMachines.AvailabilityCheck.Website/AvailabilityCheck.aspx?checktype=ELS";
voipAvailabilityFormJs.submit=function(form, returnToUrl) {
	if (!form.address.value || form.address.value.length == 0 || form.address.value.split(' ').length <= 1) {
		alert("The Street Address must contain at least one space character");
		return;
	}
	var href = "&street="+escape(form.address.value);
	href += "&city="+escape(form.city.value);
	href += "&state="+escape(form.state.value);
	href += "&zip="+escape(form.zip.value);
	location.href = this.postToUrl+href+"&redirectURL="+escape(returnToUrl+href+"&availability-check-complete=true");
}

voipAvailabilityFormJs.init=function(form, resulttag) {
	//Push the results back onto the page if we have been redirected back here.
	if (location.href.indexOf("availability-check-complete=true") < 0) {
		return;
	}
	form.address.value = unescape(getUrlParam("street"));
	form.city.value = unescape(getUrlParam("city"));
	form.zip.value = unescape(getUrlParam("zip"));
	
	var state = unescape(getUrlParam("state")).toUpperCase();
	for(i=0; i< form.state.options.length - 1; i++) {
		if( form.state.options[i].value == state) {
			form.state.options[i].selected = true;
			break;
		}
	}

	var resp = getUrlParam("_resp").replace( "%7c", "|");
	if (!resp) {
		return;
	}
	var msg = resp.split('|');
	var resphtml;
	if (msg[0] && msg[0] == "true") {
		resphtml = '<div class="available">Available</div>';
	} else {
		if (msg[1]) {
			if(msg[1].indexOf("01") > -1 || msg[1].indexOf("02") > -1 ) {
				resphtml = '<div class="error">The address provided is invalid.</div>';
			} else {
				resphtml = '<div class="error">' + unescape(msg[1]) + '</div>';
			}
		}
	}
	resulttag.innerHTML = resphtml;
}
	

