﻿////////////////////////////////////////////////////////////
// Constant Contact
function ajaxConstantContact(){
	document.getElementById('cc_loader').style.display = 'block';
	document.getElementById('cc_form').style.display = 'none';
	var url = 'server-side/add_cc.php';
	var data = 'Full_Name='+document.getElementById('Full_Name').value+'&Email='+document.getElementById('Email').value+'&CC_List='+document.getElementById('CC_List').value;
	var aj = new Ajax.Request(url, {
			method:'POST',
			parameters: data,
			onComplete: getResponseConstantContact
		}
	);
	function getResponseConstantContact(oReq){
		document.getElementById('cc_loader').style.display = 'none';
		var info = oReq.responseText;
		document.getElementById('cc_response').innerHTML = info;
	}
}
////////////////////////////////////////////////////////////
// FORMAT PHONE NUMBER
function ajaxRequestPhone(elementid,data,emessage){
	var url = 'server-side/format-phone-number.php';
	var aj = new Ajax.Request(url, {
			method:'POST',
			parameters: data,
			onComplete: getResponsePhone
		}
	);
	function getResponsePhone(oReq){
		var info = oReq.responseText;
		if(info == 0){
			$(elementid+'_error').innerHTML = emessage;
			$(elementid+'_error').style.display = "inline";
			setTimeout("$('pp_error').style.display = 'none'", 6000);
		}else{
			document.getElementById(elementid).value = info;
		}
	}
}

////////////////////////////////////////////////////////////
// CHECK EMAIL VALIDITY
function ajaxRequestEmail(elementid,data,emessage) {
	var url = 'server-side/check-for-valid-email-address.php';
	var aj = new Ajax.Request(url, {
			method:'get',
			parameters: data,
			onComplete: getResponseEmail
		}
	);
	function getResponseEmail(oReq) {
		if (oReq.responseText!="") {
			$(elementid+'_error').innerHTML = emessage;
			$(elementid+'_error').style.display = "inline";
			setTimeout("document.getElementById('email_address_error').style.display = 'none';",6000);
			document.getElementById(elementid).style.border = '1px solid #ffbebe';
			document.getElementById(elementid).style.background = '#fff0f0';
		}else{
			document.getElementById(elementid).style.border = 'solid 1px #0d4e7e';
			document.getElementById(elementid).style.background = '#F3F3f3';
			document.getElementById(elementid).value = info;
		}
	}
}

///////////////////////////////////////////////////////////
// FORMAT NAME
function ajaxFormatName(elementid,data){
	var url = 'server-side/format_name.php';
	var aj = new Ajax.Request(url, {
			method:'POST',
			parameters: data,
			onComplete: getResponseFormatName
		}
	)
	function getResponseFormatName(oReq) {
		var info = oReq.responseText;
		if(info != 0){
			document.getElementById(elementid).value = info;
		}
	}
}

////////////////////////////////////////////////////////////
// ZIP CODE LOOKUP

// <![CDATA[

function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function sndReq(zip) {
    http.open('get', 'server-side/zipcode.php?zip='+zip);
    http.onreadystatechange = handleResponse;
    http.send(null);
	function handleResponse() {
    	if(http.readyState == 4){
	        var response = http.responseText;
    	    var update = new Array();
	        if(response.indexOf('|' != -1)) {
    	        update = response.split('|');
    	        if(update[1] == 'NF'){
    	        	document.getElementById("city").style.border = '1px solid #ffbebe';
					document.getElementById("city").style.background = '#fff0f0';
					document.getElementById("state").style.border = '1px solid #ffbebe';
					document.getElementById("state").style.background = '#fff0f0';
					document.getElementById("city").value = "";
					document.getElementById("state").value = "";
					$('pp_error').innerHTML = "City and State could not be found";
					$('pp_error').style.display = "block";
					setTimeout("document.getElementById('pp_error').style.display = 'none';",6000);
				}else{
					document.getElementById("city").value = update[0];
					document.getElementById("state").value = update[1];
    	        }
    	    }
    	}
 	}   
}



    
function sndReqAccount(zip) { // FOR ACCOUNT INFO ONLY
    http.open('get', 'server-side/zipcode.php?zip='+zip);
    http.onreadystatechange = handleResponseAccount;
    http.send(null);

}
function sndReqPartner(zip) { // FOR ACCOUNT INFO ONLY
    http.open('get', 'server-side/zipcode.php?zip='+zip);
    http.onreadystatechange = handleResponsePartner;
    http.send(null);

}

function handleResponseAccount() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1)) {
            update = response.split('|');
            document.getElementById("credit_card_city").value = update[0];
			document.getElementById("credit_card_state").value = update[1];
        }
    }
}

function handleResponsePartner() {
    if(http.readyState == 4){
        var response = http.responseText;
        var update = new Array();

        if(response.indexOf('|' != -1)) {
            update = response.split('|');
            document.getElementById("partner_city").value = update[0];
			document.getElementById("partner_state").value = update[1];
        }
    }
}

// ]]>

////////////////////////////////////////////////////////////
// FORMAT RATE
function ajaxRequestRate(url,data) {
	var aj = new Ajax.Request(url, {
		method:'get',
		parameters: data,
		onComplete: getResponseRate
	}
	);
}
function getResponseRate(oReq) {
	document.getElementById('amount').value = oReq.responseText;
}