var urlprefix = "http://www.medicaresupplemental.com/";

function showPopup(currentForm) {
	st= currentForm.st.value;
	
	var siteURL ="";
	siteURL = "http://www.health-insurance-companies.com/" + "quotes_popup.php";
	siteURL = siteURL + "?st=" + st + "&source=top_window";
	window.open(siteURL,null,"fullscreen=yes, toolbars=yes, scrollbars=yes, menubar=yes, location=yes");
}

function formStValidate(f) {
	if (f.st.selectedIndex == 0) {
		alert('Please select your state and then click on Get Quotes');	
		return false;
	}
	window.location = urlprefix + "quotes-result.php?st=" + f.st.options[f.st.selectedIndex].value + "&source=bottom_window";
	//if (isValidState(f.st.options[f.st.selectedIndex].value))
		//window.open(urlprefix + "form3f.php?st=" + f.st.options[f.st.selectedIndex].value, null, "fullscreen=yes, toolbars=yes, scrollbars=yes, menubar=yes, location=yes");

	return false;
}

function showPopupFromBanner(state) {
	window.location = urlprefix + "quotes-result.php?st=" + state + "&source=bottom_window";
	//window.open(urlprefix + "form3f.php?st=" + state, null, "fullscreen=yes, toolbars=yes, scrollbars=yes, menubar=yes, location=yes");
	return false;
}

function monitorField(e, currentobject, maxsize, nextobjectfocus) {
	var unicode=e.keyCode? e.keyCode : e.charCode;
	if ((unicode != 8) && (unicode != 46)) {
		if ((currentobject.value.length) >= maxsize)
			nextobjectfocus.focus();
	}
}

function nextField(nextobjectfocus) {
	nextobjectfocus.focus();
}

function isZipCode(value) {
	exp_zipcode = /^[0-9]{5}$/;
	return exp_zipcode.test(value); 
}

function isNumber(value) {
	objRegExp = /(^\d\d*$)/;
	return objRegExp.test(value);
}

function isValidGender(currentField) {
	value = currentField.val(); 
	if (value == "0") {
		return false;
	}
	return true;
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function isValidFirstName(value) {
	value = trim(value);
	exp1_text = /^([a-z])+([a-z ])*$/i;
	exp2_text = /^test$/i;
	if (exp1_text.test(value) && (!exp2_text.test(value)))
		return true;
	return false;
}

function isValidLastName(value) {
	value = trim(value);
	exp1_text = /^([a-z])+([a-z ])*$/i;
	exp2_text = /^test$/i;
	if (exp1_text.test(value) && (!exp2_text.test(value)))
		return true;
	return false;
}

function isValidEmail(strEmail){
	validRegExp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i;
    if (strEmail.search(validRegExp) == -1) 
    {
      return false;
    } 
    return true; 
}

function isValidAddress(value) {
	exp1_text = /^[-a-z0-9,. ]{4}([-a-z0-9,. ])*$/i;
	return exp1_text.test(value); 
}

function isValidCity(value) {
	exp1_text = /^[a-z ]{2}([a-z,. ])*$/i;
	return exp1_text.test(value);
}

function isValidAreaPhoneNumber(value) {
	exp_text = /^[0-9]{3}$/;
	return exp_text.test(value);
}

function isValidStatePhoneNumber(value) {
	exp_text = /^[0-9]{3}$/;
	return exp_text.test(value);
}

function isValidPhoneNumber(value) {
	exp_text = /^[0-9]{4}$/;
	return exp_text.test(value);
}

function isValidABEligible(element) {
	if ((element[0].checked == false) && (element[1].checked == false) && (element[2].checked == false))
		return false;
	return true;
}

function isValidState(value) {
	switch (value) {
		case 'AK':
		case 'DC':
		case 'HI':
		case 'MA':
		case 'ND':
		case 'RI':
		case 'FL':
		case 'NY':
			return false;
			break;
		default:
			return true;
			break;
	}
}

function isValidFields(currentForm)
{
	var stop = false;
	var colorRequired = '#FF0000';
	var colorNormal = '#B3B3B3';
	var textColor = '#000000';
	
	// validate date of birth
	if ((stop == false) && (currentForm.month.selectedIndex == 0)) {
		alert ("To proceed please choose your \"Month\" of birthday.");
		currentForm.month.focus();
		paintBorderColor(currentForm.month, colorRequired);
		stop = true;
	}  else
		paintBorderColor(currentForm.month, colorNormal);
	if ((stop == false) && (currentForm.day.selectedIndex == 0)) {
		alert ("To proceed please choose your \"Day\" of birthday.");
		currentForm.day.focus();
		paintBorderColor(currentForm.day, colorRequired);
		stop = true;
	} else
		paintBorderColor(currentForm.day, colorNormal);
	if ((stop == false) && (currentForm.year.selectedIndex == 0)) {
		alert ("To proceed please choose your \"Year\" of birthday.");
		currentForm.year.focus();
		paintBorderColor(currentForm.year, colorRequired);
		stop = true;
	} else
		paintBorderColor(currentForm.year, colorNormal);
	
	// validate currently medicare eligible
	/*var abeligiblecontainer = document.getElementById('eligible');
	if ((stop == false) && (!isValidABEligible(currentForm.abeligible))) {
		alert ("To proceed please choose a \"Currently Medicare Part A/B Eligible\".");
		paintTextColor(abeligiblecontainer, colorRequired);
		stop = true;
	} else
		paintTextColor(abeligiblecontainer, textColor);*/
	
	// validate first name
	if ((stop == false) && (!isValidFirstName(currentForm.firstname.value))) {
		alert ("To proceed please type a valid \"First Name\".");
		currentForm.firstname.focus();
		paintBorderColor(currentForm.firstname, colorRequired);
		stop = true;
	} else
		paintBorderColor(currentForm.firstname, colorNormal);
	
	// validate last name
	if ((stop == false) && (!isValidLastName(currentForm.lastname.value))) {
		alert ("To proceed please type a valid \"Last Name\".");
		currentForm.lastname.focus();
		paintBorderColor(currentForm.lastname, colorRequired);
		stop = true;
	} else
		paintBorderColor(currentForm.lastname, colorNormal);
	
	// validate address
	if ((stop == false) && (!isValidAddress(currentForm.address.value))) {
		alert ("To proceed please type your \"Address\".");
		currentForm.address.focus();
		paintBorderColor(currentForm.address, colorRequired);
		stop = true;
	} else
		paintBorderColor(currentForm.address, colorNormal);

	// validate city
	if ((stop == false) && (!isValidCity(currentForm.city.value))) {
		alert ("To proceed please type your \"City\".");
		currentForm.city.focus();
		paintBorderColor(currentForm.city, colorRequired);
		stop = true;
	} else
		paintBorderColor(currentForm.city, colorNormal);

	// validate state
	if ((stop == false) && (currentForm.state.selectedIndex == 0)) {
		alert ("To proceed please choose your \"State\".");
		currentForm.state.focus();
		paintBorderColor(currentForm.state, colorRequired);
		stop = true;
	} else
		paintBorderColor(currentForm.state, colorNormal);

	// validate zip code
	if ((stop == false) && (!isZipCode(currentForm.zipcode.value))) {
		alert ("To proceed please type a valid \"ZIP Code\".");
		currentForm.zipcode.focus();
		paintBorderColor(currentForm.zipcode, colorRequired);
		stop = true;
	} else
		paintBorderColor(currentForm.zipcode, colorNormal);

	// validate phone number 1
	if ((stop == false) && (!isValidAreaPhoneNumber(currentForm.phone1.value))) {
		alert ("To proceed please type a valid \"Phone Area Code\".");
		currentForm.phone1.focus();
		paintBorderColor(currentForm.phone1, colorRequired);
		stop = true;
	} else
		paintBorderColor(currentForm.phone1, colorNormal);
	
	// validate phone number 2
	if ((stop == false) && (!isValidStatePhoneNumber(currentForm.phone2.value))) {
		alert ("To proceed please type a valid \"Phone State Code\".");
		currentForm.phone2.focus();
		paintBorderColor(currentForm.phone2, colorRequired);
		stop = true;
	} else
		paintBorderColor(currentForm.phone2, colorNormal);
	
	// validate phone number 3
	if ((stop == false) && (!isValidPhoneNumber(currentForm.phone3.value))) {
		alert ("To proceed please type a valid \"Phone Number\".");
		currentForm.phone3.focus();
		paintBorderColor(currentForm.phone3, colorRequired);
		stop = true;
	} else
		paintBorderColor(currentForm.phone3, colorNormal);
	
	// validate email
	if ((stop == false) && (!isValidEmail(currentForm.email.value))) { //no 
		alert ("To proceed please type a valid \"Email\".");
		currentForm.email.focus();
		paintBorderColor(currentForm.email, colorRequired);
		stop = true;
	} else
		paintBorderColor(currentForm.email, colorNormal);
	
	return (!stop);
}

function validateForm() {
	currentForm = document.frmMSQ;
	if (isValidFields(currentForm)) {
		currentForm.submit();
	}
}

function paintTextColor(obj, color) {
	obj.style.color = color;
}

function paintBorderColor(obj, color) {
	obj.style.borderColor = color;
}


function validateQuoteProvidersForm(form)
{
    if (!isZipCode(form.zipcode.value)) {
        alert("To proceed please enter a valid ZIP Code.");
        form.zipcode.focus();
        return false;
    }
	window.location = urlprefix + "quotes-results.php?zip=" + form.zipcode.value + "&source=bottom_window";
	return false;
}

function validateInsuranceQuotesForm(form)
{
    if (!isZipCode(form.zipcode.value)) {
        alert("To proceed please enter a valid ZIP Code.");
        form.zipcode.focus();
        return false;
    }
	var Url = location.href;
	Url = Url.replace(/.*\?(.*?)/,"$1");

	var url = document.URL;
    	url = url.replace(/&/g,'|');
    	
	window.location = urlprefix + "quotes-result.php?zip=" + form.zipcode.value + "&source=bottom_window" + "&utm_source=" + form.utm_source.value + "&bw_type="  + form.bw_type.value + "&bw_keyword="+ form.bw_keyword.value + '&'+Url;
	
	//if (!isValidStateForLead(form.zipcode.value)){
	         var args  = 'width='+screen.availWidth;
	         args += ',height='+screen.availHeight;
	         args += ',top=0,left=0';
	         args += ',scrollbars=yes';
	         args += ',channelmod=1';
	         args += ',fullscreen=yes';        	 
        	 window.open(urlprefix + "form3i.php?zip=" + form.zipcode.value +"&utm_source="+ form.utm_source.value + "&SRC="+ form.SRC.value + "&bw_type="+ form.bw_type.value + "&bw_keyword="+ form.bw_keyword.value + "&Pub_ID="+ form.Pub_ID.value + "&Sub_ID="+ form.Sub_ID.value + "&CID="+ form.CID.value  +"&url_params="+url, null, args);
        	 
	//}
		
	return false;
}

function validateStateQuoteForm(state){
	var form = document.getElementsByName('search');
	form = form[0];
    var url = document.URL;
	url = url.replace(/&/g,'|');
	var Urlparams = location.href;
	Urlparams = Urlparams.replace(/.*\?(.*?)/,"$1");
	
	var utm = document.getElementById('utm_source').value;	
	var bw_type = document.getElementById('bw_type').value;
	var CID = document.getElementById('CID').value;
	//if (isValidState(state)){
		 var args  = 'width='+screen.availWidth;
         args += ',height='+screen.availHeight;
         args += ',top=0,left=0';
         args += ',scrollbars=yes';
         args += ',channelmod=1';
         args += ',fullscreen=yes';
		window.open(urlprefix + "form3i.php?st=" + state +"&utm_source="+ form.utm_source.value + "&SRC="+ form.SRC.value + "&bw_type="+ form.bw_type.value + "&bw_keyword="+ form.bw_keyword.value + "&Pub_ID="+ form.utm_source.value + "&Sub_ID="+ form.Sub_ID.value + "&CID="+ CID  +"&url_params="+url, null, args);
	//}
	window.location = urlprefix + "quotes-result.php?st=" + state + "&source=bottom_window"+ "&utm_source=" + utm + "&bw_type=" + form.bw_type.value + "&bw_keyword="+ form.bw_keyword.value +'&'+Urlparams;	
	
	return false;
	
}

function validateQuoteProvidersFormMirror(form)
{
    if (!isZipCode(form.zipcode.value)) {
        alert("To proceed please enter a valid ZIP Code.");
        form.zipcode.focus();
        return false;
    }
    window.location = urlprefix + "quotes.php?zip=" + form.zipcode.value + "&source=bottom_window";
	window.open (urlprefix + "longevity-alliance/?zipcode=" + form.zipcode.value + "&source=bottom_window", null, "fullscreen=yes, toolbars=yes, scrollbars=yes, menubar=yes, location=yes");
	return false;
}

function ajaxObject(){
    var xmlhttp=false;
    try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
            try {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                    xmlhttp = false;
            }
    }

    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
            xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}

function isValidStateForLead(zipcode){    
	var result;
	ajax = ajaxObject();
    ajax.open("GET", urlprefix + "state_lead.php?action=echo&form=3f&zipcode="+zipcode, false);
    ajax.onreadystatechange = function() {
            if (ajax.readyState == 4) {
            	if (ajax.status == 200)
            	result = ajax.responseText
            }
    }
    ajax.send(null);
    if (ajax.responseText == "true")
    	return true;
    else
    	return false;
}

function zip_change(event, object) {
	
	kcode = event.keyCode? event.keyCode : event.charCode;
	if ((kcode != 8) && (kcode != 46) && (kcode != 13)&&(kcode != 9)) { // if is different from backspace and delete	    
	    if (((kcode > 47) && (kcode < 58))){//only numbers
			dob_length = $(object).val().length;
			if ((dob_length == 4)) {		    
			    $('.inputBg').addClass('inputBgFull');
			    $('#bntQuotes').focus();
			    $('#arrowimg').attr('src','images/arrow2.gif');
			} 
	    }else{
			return false;
	    }
	}else{		
	    if((kcode != 9)){
	    	$('.inputBg').removeClass('inputBgFull');
	    }
	}
	return true;
}


