var filedir = '/wp-content/themes/joenterprises/';

//function checkPaymentTypeXX(frm) {
//
//	if(document.getElementById('pp_paypal').innerHTML=='x') {
//		frm.action = "https://www.sandbox.paypal.com/cgi-bin/webscr";
//	} else {
//		frm.action = "/checkout-check";
//	}
//	return true;
//}

function toggleDisplay(el) {
	var c = el.getElementsByTagName('*');
	for (i in c) if (c[i].className == 'toggle') c[i].style.display = (c[i].style.display == 'block') ? 'none' : 'block';
}
  
function checkPaymentType(frm) {
	if(frm.custom[0].value == 'paypal' && frm.custom[0].checked) {
		// frm.action = "https://www.sandbox.paypal.com/cgi-bin/webscr";
		frm.action = "https://www.paypal.com/cgi-bin/webscr";
	} else {
		frm.action = "/checkout-check";
	}
	return true;
}

function checkForm() {
	var form_good = true;
	var results = true;
	var req = ['email','first_name','last_name','address1','city','state','zip','phone'];
	
	for( i in req ) { 
		results = doAjaxFormRequest(req[i], filedir + 'validateform.php?field=' + req[i], 'formprocess');
		if(!results) { 
			form_good = false;
			if(req[i] == 'phone') {
			  if(document.getElementById('night_phone_a')) document.getElementById('night_phone_a').style.backgroundColor = '#ffeded'; // display result
			  if(document.getElementById('night_phone_b')) document.getElementById('night_phone_b').style.backgroundColor = '#ffeded'; // display result
			  if(document.getElementById('night_phone_c')) document.getElementById('night_phone_c').style.backgroundColor = '#ffeded'; // display result
			} else {
			  if(document.getElementById(req[i])) document.getElementById(req[i]).style.backgroundColor = '#ffeded'; // display result
			}
		}
	}
	// addPaypalOrder();
	// AjaxRequest('',filedir + 'completeorder.php');
	return form_good;
}


function doAjaxFormRequest(target_div, file, process_div) {

	var msg_div = 'val_' + target_div;
	var results = true;
	
	var MyHttpRequest = false;
	var MyHttpLoading = '<img src="/images/loading.gif" border="0" />';
	var ErrorMSG = 'Sorry - No XMLHTTP support in your browser';
			
	if(window.XMLHttpRequest) // client use Firefox, Opera etc - Non Microsoft product
		{ try { MyHttpRequest = new XMLHttpRequest(); }
		catch(e) { MyHttpRequest = false; }	}
	else if(window.ActiveXObject) // client use Internet Explorer
		{ try { MyHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch(e) { try { MyHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { MyHttpRequest = false; } } }
	else { MyHttpRequest = false; }


	if(MyHttpRequest) { // browser supports httprequest
		
		if(document.getElementById(process_div)) document.getElementById(process_div).innerHTML = MyHttpLoading; // working
		MyHttpRequest.open("get", file, false); // <-- run the httprequest using GET
		MyHttpRequest.send(null);
		if(document.getElementById(msg_div)) document.getElementById(msg_div).innerHTML = MyHttpRequest.responseText; // display result
		if(MyHttpRequest.responseText != '') results = false;
		if(document.getElementById(process_div)) document.getElementById(process_div).innerHTML = ''; // hide progress gif
	} else { 
		if(document.getElementById(process_div)) document.getElementById(process_div).innerHTML = ErrorMSG; } // the browser was unable to create a httprequest
		
	return results;
}

function AjaxRequest(target_div,file,process_div) {

	var havediv = true;
	var MyHttpRequest = false;
	var MyHttpLoading = '<img src="/images/loading.gif" border="0" />';
	var ErrorMSG = 'Sorry - No XMLHTTP support in your browser';
	
	if(!target_div) havediv = false;

	if(window.XMLHttpRequest) // client use Firefox, Opera etc - Non Microsoft product
		{ try { MyHttpRequest = new XMLHttpRequest(); }
		catch(e) { MyHttpRequest = false; }	}
	else if(window.ActiveXObject) // client use Internet Explorer
		{ try { MyHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch(e) { try { MyHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { MyHttpRequest = false; } } }
	else { MyHttpRequest = false; }


	if(MyHttpRequest) { // browser supports httprequest
		var random = Math.random() * Date.parse(new Date()); // make a random string to prevent caching
		
		var file_array = file.split('.'); // prepare to check if we have a query string or a html/htm file
		if(file_array[1] == 'php') // no query string, just calling a php file
			{ var query_string = '?ajax=true&rand=' + random; }
		else // we have presumable a php file with a query string attached
			{ var query_string = '&ajax=true&rand=' + random; }
		
		
		MyHttpRequest.open("get", file + query_string, true); // <-- run the httprequest using GET
	
		// handle the httprequest
		MyHttpRequest.onreadystatechange = function () {
			if(MyHttpRequest.readyState == 4) { // done and responded
				if(process_div) document.getElementById(process_div).innerHTML = ''; // hide progress gif
				if(havediv) document.getElementById(target_div).innerHTML = MyHttpRequest.responseText; // display result
			} else {
				if(process_div)document.getElementById(process_div).innerHTML = MyHttpLoading; } // still working
		}
	
		MyHttpRequest.send(null);
	
	} else { 
		if(havediv) document.getElementById(process_div).innerHTML = ErrorMSG; } // the browser was unable to create a httprequest
}


function MyAjaxRequest(target_div,file,process_div) {

	var havediv = true;
	var MyHttpRequest = false;
	var MyHttpLoading = '<img src="/images/loading.gif" border="0" />';
	var ErrorMSG = 'Sorry - No XMLHTTP support in your browser';
	
	if(!process_div) var process_div = target_div;
	if(!target_div) havediv = false;

	if(window.XMLHttpRequest) // client use Firefox, Opera etc - Non Microsoft product
		{ try { MyHttpRequest = new XMLHttpRequest(); }
		catch(e) { MyHttpRequest = false; }	}
	else if(window.ActiveXObject) // client use Internet Explorer
		{ try { MyHttpRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
		catch(e) { try { MyHttpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { MyHttpRequest = false; } } }
	else { MyHttpRequest = false; }


	if(MyHttpRequest) { // browser supports httprequest
		var random = Math.random() * Date.parse(new Date()); // make a random string to prevent caching
		
		var file_array = file.split('.'); // prepare to check if we have a query string or a html/htm file
		if(file_array[1] == 'php') // no query string, just calling a php file
			{ var query_string = '?ajaxrequest=true&rand=' + random; }
		else // we have presumable a php file with a query string attached
			{ var query_string = '&ajaxrequest=true&rand=' + random; }
		
		
		MyHttpRequest.open("get", file + query_string, true); // <-- run the httprequest using GET
	
		// handle the httprequest
		MyHttpRequest.onreadystatechange = function () {
			if(MyHttpRequest.readyState == 4) { // done and responded
				if(havediv) document.getElementById(process_div).innerHTML = ''; // hide progress gif
				if(havediv) document.getElementById(target_div).innerHTML = MyHttpRequest.responseText; // display result
			} else {
				if(havediv) document.getElementById(process_div).innerHTML = MyHttpLoading; } // still working
		}
	
		MyHttpRequest.send(null);
	
	} else { 
		if(havediv) document.getElementById(process_div).innerHTML = ErrorMSG; } // the browser was unable to create a httprequest
}

