// Ajax functions and other javascript utility scripts

function formatCurrency(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + num + '.' + cents);
}


// Check for a blank field.
function isBlank(data)
{
	// Remove all cases of "<br>" and "&nbsp;"
	data = data.replace(/\r|\n|\<br>|\&nbsp;/g, "");
	// Remove all spaces
	data = data.replace(/\s/g, "");	

	if (data == '')
		return true;
	else
		return false;	
}

function limitText(limitField, limitNum)
{
	if (limitField.value.length > limitNum)
	{
		limitField.value = limitField.value.substring(0, limitNum);
	}
	else 
	{
		document.getElementById("charsLeft").innerHTML= (limitNum - limitField.value.length);
	}
}

function getXMLHttpObject()
{
	var xmlHttp;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();		
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
		
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}

	return xmlHttp;
}

function contactRequestDetails(myForm, elementToChange, eventID, hideshow)
{
	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="/admin/ajax_php/contact_request_details.php";
	url=url+"?id="+eventID;
	url=url+"&hideshow="+hideshow;
	url=url+"&sid="+Math.random();

	xmlHttp.onreadystatechange=function()
    {
		if(xmlHttp.readyState==4)
		{
			document.getElementById(elementToChange).innerHTML=xmlHttp.responseText;
			//window.open("document.writeln('" + xmlHttp.responseText + "');", 'OFFSCREEN','scrollbars=no,resizable=no,width=1024,height=768');
		}
    }

   xmlHttp.open("POST",url,true);
   xmlHttp.send(null);
}

function eventDetails(myForm, elementToChange, eventID, hideshow)
{
	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="/admin/ajax_php/event_details.php";
	url=url+"?id="+eventID;
	url=url+"&hideshow="+hideshow;
	url=url+"&sid="+Math.random();

	xmlHttp.onreadystatechange=function()
    {
		if(xmlHttp.readyState==4)
		{
			document.getElementById(elementToChange).innerHTML=xmlHttp.responseText;
		}
    }

   xmlHttp.open("POST",url,true);
   xmlHttp.send(null);
}

function testPrint()
{
	alert('hi');
}

function showPreview(elementToChange)
{	

	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 


	var url="/admin/admin_page_preview.php";
	
//	var poststr = "billing_address=" + encodeURI( document.getElementById("billing_address").value ) +
//		  		  "&billing_city=" + encodeURI( document.getElementById("billing_city").value ) +
//		  		  "&cc_zip=" + encodeURI( document.getElementById("cc_zip").value );
	

	var poststr = "thecontent=" +  encodeURI(document.getElementById("page_content").value);

	var t;

	xmlHttp.onreadystatechange=bleh;

	function bleh()
    {	
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.status==200)
			{
					//alert(xmlHttp.responseText);
				document.getElementById(elementToChange).innerHTML=xmlHttp.responseText;
				//window.open("javascript:document.writeln(" + xmlHttp.responseText + ");", 'OFFSCREEN','scrollbars=yes,resizable=yes,width=1024,height=768');
			 }
				
		}
	
    }

   xmlHttp.open("POST",url,true);
   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", poststr.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(poststr);
 
//   orderOK();
 //  alert("t is " + xmlHttp.responseText);
}


function validateCustomerForm(form)
{			
	var errorString='';

	if (isBlank(form.fname.value))
	{		
		errorString = errorString + "- Billing First Name\n";
	}
	if (isBlank(form.lname.value))
	{		
		errorString = errorString + "- Billing Last Name\n";
	}
	if (isBlank(form.phone.value))
	{		
		errorString = errorString + "- Billing Phone\n";
	}
	if (isBlank(form.cc_addr.value))
	{		
		errorString = errorString + "- Billing Address\n";
	}
	if (isBlank(form.cc_city.value))
	{		
		errorString = errorString + "- Billing City\n";
	}
	if (isBlank(form.cc_state.value))
	{		
		errorString = errorString + "- Billing State\n";
	}
	if (isBlank(form.cc_zip.value))
	{		
		errorString = errorString + "- Billing Zip Code\n";
	}
	if (isBlank(form.cc_country.value))
	{		
		errorString = errorString + "- Billing Country\n";
	}
	if (isBlank(form.email.value))
	{		
		errorString = errorString + "- Email Address\n";
	}
	if (isBlank(form.x_ship_to_first_name.value))
	{		
		errorString = errorString + "- Shipping First Name\n";
	}
	if (isBlank(form.x_ship_to_last_name.value))
	{		
		errorString = errorString + "- Shipping Last Name\n";
	}
	if (isBlank(form.x_ship_to_address.value))
	{		
		errorString = errorString + "- Shipping Address\n";
	}
	if (isBlank(form.x_ship_to_city.value))
	{		
		errorString = errorString + "- Shipping City\n";
	}
	if (isBlank(form.x_ship_to_state.value))
	{		
		errorString = errorString + "- Shipping State\n";
	}
	if (isBlank(form.x_ship_to_zip.value))
	{		
		errorString = errorString + "- Shipping Zip\n";
	}
	if (isBlank(form.x_ship_to_country.value))
	{		
		errorString = errorString + "- Shipping Country\n";
	}
	if (isBlank(form.cc_num.value))
	{		
		errorString = errorString + "- Credit Card Number\n";
	}
	if (isBlank(form.cc_cid.value))
	{		
		errorString = errorString + "- Credit Card CVV Code\n";
	}
/*	if (isBlank(form.cc_exp.value))
	{		
		errorString = errorString + "- Credit Card Expiration Date\n";
	}
*/

	if (errorString!='')
	{
		alert("The following information is required:\n" + errorString);
		return false;
	}
	else
	{
		return true;
	}
}

function copyBillingInfoToShippingInfo(form)
{
	if (form.shippingSame.checked)
	{		
		form.x_ship_to_first_name.value=form.fname.value;
		form.x_ship_to_middle_name.value=form.mname.value;
		form.x_ship_to_last_name.value=form.lname.value;
		form.x_ship_to_address.value=form.cc_addr.value;
		form.x_ship_to_city.value=form.cc_city.value;
		form.x_ship_to_state.selectedIndex=form.cc_state.selectedIndex;
		form.x_ship_to_zip.value=form.cc_zip.value;
		form.x_ship_to_country.value=form.cc_country.value;
		form.x_ship_to_country.selectedIndex=form.cc_country.selectedIndex;
		//form.x_ship_to_company.value=form.cc_company.value;

		//changeShippingRates(form);
		calculateFedExShipping('shippingDisplayPrice', form.cc_zip.value, form.cc_country.value, form.cc_state.selectedIndex, '0');
	}
}


function calculateFedExShipping(elementToChange, zip, country, state, price)
{	
	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 


	var url="/admin/ajax_php/calculateFedExShipping.php";
	
	var poststr = "zip=" + encodeURI( zip ) +
		  		  "&country=" + encodeURI(country ) +
		  		  "&state=" + encodeURI( state );
	
	
	var t;

	xmlHttp.onreadystatechange=bleh;

	function bleh()
    {	
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.status==200)
			{
				//var total_price2 = document.getElementById('price').value - document.getElementById('shipping').value;
				
				var data = xmlHttp.responseText;
					//alert(xmlHttp.responseText);
				data = data.replace(/\s/g, "");
				document.getElementById(elementToChange).innerHTML='$'+data;
				document.getElementById('shipping').value=formatCurrency(data);
				
				
				//alert(document.getElementById('shipping').value + " " + document.getElementById('subtotal').value);


				var total_price = (data * 1) + (document.getElementById('subtotal').value * 1);
				//var total_price = (data * 1) + (price * 1);
				document.getElementById("totalPrice").innerHTML='$'+formatCurrency(total_price);
				document.getElementById('price').value=formatCurrency(total_price);
	

				getPromoDiscount('promoNameDisplay', 'discountDisplay', document.getElementById('promotion_code').value, document.getElementById('shipping').value, document.getElementById('price').value, document.getElementById('subtotal').value);
				//document.getElementById('shipping').value=data;
				//window.open("javascript:document.writeln(" + xmlHttp.responseText + ");", 'OFFSCREEN','scrollbars=yes,resizable=yes,width=1024,height=768');
			 }
				
		}
	
    }

   xmlHttp.open("POST",url,true);
   xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp.setRequestHeader("Content-length", poststr.length);
   xmlHttp.setRequestHeader("Connection", "close");
   xmlHttp.send(poststr);
 
//   orderOK();
 //  alert("t is " + xmlHttp.responseText);
}

function validateSponsorAJarForm(form)
{			
	var errorString='';

	if (isBlank(form.person_celebrated.value))
	{		
		errorString = errorString + "- Name of person celebrated\n";
	}

	if (isBlank(form.special_occasion.value))
	{		
		errorString = errorString + "- Special Occasion\n";
	}
	
	if (errorString!='')
	{
		alert("The following information is required:\n" + errorString);
		return false;
	}
	else
	{
		return true;
	}
}


function validateChooseAJarForm(form)
{			
	var errorString='';

	if (isBlank(form.person_celebrated.value))
	{		
		errorString = errorString + "- Name of person celebrated\n";
	}

	if (errorString!='')
	{
		alert("The following information is required:\n" + errorString);
		return false;
	}
	else
	{
		return true;
	}
}



function memoryDetails(myForm, elementToChange, eventID, hideshow)
{
	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="/admin/ajax_php/memoryDetails.php";
	url=url+"?id="+eventID;
	url=url+"&hideshow="+hideshow;
	url=url+"&sid="+Math.random();

	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			document.getElementById(elementToChange).innerHTML=xmlHttp.responseText;
		}
	}

	xmlHttp.open("POST",url,true);
	xmlHttp.send(null);
}

function getPromoDiscount(promoNameDisplay, discountDisplay, promoCode, shipping, price, subtotal)
{	
	xmlHttp2 = getXMLHttpObject();
	if (xmlHttp2==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 


	var url2="/admin/ajax_php/getPromoDiscount.php";
	
	var poststr2 =	"promoCode=" + encodeURI(promoCode) +
					"&shipping=" + encodeURI(shipping) +
					"&price=" + encodeURI(price) +
					"&subtotal=" + encodeURI(subtotal) +
					"&sid="+Math.random();
	

	var t;

	xmlHttp2.onreadystatechange=bleh2;

	function bleh2()
    {	
		if(xmlHttp2.readyState==4)
		{
			if(xmlHttp2.status==200)
			{
				var data = xmlHttp2.responseText;
					//alert(xmlHttp2.responseText);
				data = data.replace(/\s/g, "");
				document.getElementById(discountDisplay).innerHTML='-$'+data;
				var finalPrice = 1 * price - 1 * data;

				document.getElementById("totalPrice").innerHTML='$'+ formatCurrency(finalPrice);


				//var total_price = (data * 1) + (price * 1);
				//document.getElementById("totalPrice").innerHTML='$'+formatCurrency(total_price);

				//document.getElementById('shipping').value=data;
				//window.open("javascript:document.writeln(" + xmlHttp2.responseText + ");", 'OFFSCREEN','scrollbars=yes,resizable=yes,width=1024,height=768');
			 }
				
		}
	
    }

   xmlHttp2.open("POST",url2,true);
   xmlHttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp2.setRequestHeader("Content-length", poststr2.length);
   xmlHttp2.setRequestHeader("Connection", "close");
   xmlHttp2.send(poststr2);
 
//   orderOK();
 //  alert("t is " + xmlHttp2.responseText);
}
