// Ajax functions
// 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 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);
}


function changeShippingRates(form,boxChecked)
{
		if (form.x_ship_to_country.value == "US")
		{	
			if (boxChecked == "usps")
			{
				form.overnight.checked = false;
				form.twoDaySelect.checked = false;			
				form.threeDaySelect.checked = false;
			}			
			else if (boxChecked == "threeDaySelect")
			{
				form.usps.checked = false;
				form.overnight.checked = false;
				form.twoDaySelect.checked = false;
			}
			else if(boxChecked == "twoDaySelect")
			{
				form.usps.checked = false;
				form.threeDaySelect.checked = false;
				form.overnight.checked = false;
			}
			else if(boxChecked == "overnight")
			{
				form.usps.checked = false;
				form.threeDaySelect.checked = false;
				form.twoDaySelect.checked = false;
			}
			else if(boxChecked == "uspsHIAK23")
			{
				form.upsHIAK2.checked = false;
			}
			else if(boxChecked == "upsHIAK2")
			{
				form.uspsHIAK23.checked = false;
			}


			document.getElementById("insuranceBox").innerHTML="";


			if (form.x_ship_to_state.value == "AK" || form.x_ship_to_state.value == "HI")
			{
				if (document.getElementById("uspsHIAK23Box").innerHTML=="")
				{
					document.getElementById("threeDaySelectBox").innerHTML="";
					document.getElementById("twoDaySelectBox").innerHTML="";
					document.getElementById("overnightBox").innerHTML="";
					document.getElementById("uspsBox").innerHTML="";
					document.getElementById("uspsHIAK23Box").innerHTML="<div id=\"uspsHIAK23Box\"><input type=\"checkbox\" name=\"uspsHIAK23\" onclick=\"changeShippingRates(shippingForm,'uspsHIAK23');\"> USPS FREE First Class for orders over $100, $12.95 orders under $100 <b>(Domestic U.S. Accounts ONLY)</b></div>";
					document.getElementById("upsHIAK2Box").innerHTML="<div id=\"upsHIAK2Box\"><input type=\"checkbox\" name=\"upsHIAK2\" onclick=\"changeShippingRates(shippingForm,'upsHIAK2');\"> UPS 2 Day Air Rate of $23.95 <b>(Domestic U.S. Accounts ONLY)</b></div>";
					form.uspsHIAK23.checked = true;
				}
				
				if(form.upsHIAK2.checked) 
				{
					form.shipping.value = (23.95);
					form.upsHIAK2.checked = true;
				}
				else
				{
					form.uspsHIAK23.checked = true;
					if (form.price.value > 100.00)
					{
						form.shipping.value = (0.00);
					}
					else
					{
						form.shipping.value = (12.95);
					}
				}																
			}
			else if (document.getElementById("twoDaySelectBox").innerHTML=="")
			{			
				document.getElementById("threeDaySelectBox").innerHTML="<div id=\"threeDaySelectBox\"><input type=\"checkbox\" name=\"threeDaySelect\" onclick=\"changeShippingRates(shippingForm,'threeDaySelect');\">  UPS Expedited 3 Business Day  - Add $9.00 to the cost of UPS Ground <b>(Domestic U.S. Accounts ONLY)</b></div>";
				document.getElementById("twoDaySelectBox").innerHTML="<div id=\"twoDaySelectBox\"><input type=\"checkbox\" name=\"twoDaySelect\" onclick=\"changeShippingRates(shippingForm,'twoDaySelect');\">  UPS Expedited 2 Business Day - Add $12.00 to the cost of UPS Ground <b>(Domestic U.S. Accounts ONLY)</b></div>";
				document.getElementById("overnightBox").innerHTML="<div id=\"overnightBox\"><input type=\"checkbox\" name=\"overnight\" onclick=\"changeShippingRates(shippingForm,'overnight');\"> UPS Overnight 1 Business Day - Add $15.00 to the cost of UPS Ground <b>(Domestic U.S. Accounts ONLY)</b></div>";
				document.getElementById("uspsBox").innerHTML="<div id=\"uspsBox\"><input type=\"checkbox\" name=\"usps\" onclick=\"changeShippingRates(shippingForm,'usps');\"> USPS FREE First Class for orders over $100, $9.00 orders under $100 <b>(Domestic U.S. Accounts ONLY)</b></div>";
				document.getElementById("uspsHIAK23Box").innerHTML="";
				document.getElementById("upsHIAK2Box").innerHTML="";
				//form.shipping.value = (9.50 + ((form.totalItems.value - 1) * 7.25));
				if (form.price.value > 100.00)
				{
					form.shipping.value = (0.00);
				}
				else
				{
					form.shipping.value = (9.00);
				}

				form.usps.checked = true;
			}			
			else if(form.overnight.checked) 
			{
				//form.shipping.value = (30.00 + ((form.totalItems.value - 1) * 12.50));
				form.shipping.value = (15.00 + 6.00);
			}
			else if(form.twoDaySelect.checked) 
			{
				form.shipping.value = (12.00 + 6.00);
			}
			else if(form.threeDaySelect.checked) 
			{
				form.shipping.value = (9.00 + 6.00);
			}
			else if(form.usps.checked) 
			{
				if (form.price.value > 100.00)
				{
					form.shipping.value = (0.00);
				}
				else
				{
					form.shipping.value = (9.00);
				}
			}
			else
			{
				if (form.price.value > 100.00)
				{
					form.shipping.value = (0.00);
				}
				else
				{
					form.shipping.value = (9.00);
				}
				form.usps.checked = true;
				//form.shipping.value = (9.50 + ((form.totalItems.value - 1) * 7.25));
			}
		}
		else
		{
			if (document.getElementById("twoDaySelectBox").innerHTML!="" || document.getElementById("insuranceBox").innerHTML!="" || document.getElementById("overnightBox").innerHTML=="")
			{			

				//document.getElementById("overnightBox").innerHTML="<div id=\"overnightBox\"><input type=\"checkbox\" name=\"overnight\" onclick=\"changeShippingRates(shippingForm,'overnight');\"> Overnight shipping - $35.00 first item, $14.00 for each additional item <b>(Canada Accounts ONLY)</b></div>";
				//document.getElementById("threeDaySelectBox").innerHTML="<div id=\"threeDaySelectBox\"><br><input type=\"checkbox\" name=\"threeDaySelect\" onclick=\"changeShippingRates(shippingForm,'threeDaySelect');\">  3 day select - $22.00 first item, $10.00 for each additional item <b>(Canada Accounts ONLY)</b></div>";
				//document.getElementById("threeDaySelectBox").innerHTML="<div id=\"threeDaySelectBox\"></div>";
				document.getElementById("uspsHIAK23Box").innerHTML="";
				document.getElementById("upsHIAK2Box").innerHTML="";
				document.getElementById("uspsBox").innerHTML="<div id=\"uspsBox\"><input type=\"checkbox\" name=\"usps\" onclick=\"changeShippingRates(shippingForm,'usps');\"> USPS FREE First Class for orders over $100, $29.00 on orders under $100</div>";
				if (form.price.value > 100.00)
				{
					form.shipping.value = (0.00);
				}
				else
				{
					form.shipping.value = (29.00);
				}
				form.usps.checked = true;
			}			
			
			
			if (form.price.value > 100.00)
			{
				form.shipping.value = (0.00);
			}
			else
			{
				form.shipping.value = (29.00);
			}

			document.getElementById("insuranceBox").innerHTML="";
			document.getElementById("twoDaySelectBox").innerHTML="";
			document.getElementById("threeDaySelectBox").innerHTML="";
			document.getElementById("overnightBox").innerHTML="";
		}

		var newTotal = (form.price.value * 1)+ (form.shipping.value * 1) - form.discount.value;

		document.getElementById("shippingDisplayPrice").innerHTML = "<b>$"+formatCurrency(form.shipping.value)+"</b>";
		document.getElementById("totalPrice").innerHTML = "<b>$"+formatCurrency(newTotal)+"</b>";
}

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);	
	}
}


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 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, adminID, 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+"&admin_id="+adminID;
	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 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);
}

// Non-ajax functions - used for two lists manipulations (one is a main list, and the other is the new list)
function uncheckAll(obj) {
	for (var loop=0; loop < obj.options.length; loop++) {
	obj.options[loop].selected = false;
	}
}
function selectAll(listID)
{
	var newList = document.getElementById(listID);

	for (i = newList.length - 1; i>=0; i--)
	{
		newList.options[i].selected = true;
	}
}
function addSelectedClientsToList(mList, nList)
{
	var elSel = document.getElementById(mList);
	var newList = document.getElementById(nList);

	var i;
	for (i = elSel.length - 1; i>=0; i--)
	{
		if (elSel.options[i].selected)
		{
			//alert(elSel.options[i].text + ' ' + elSel.options[i].value);
			addClientsToList(elSel.options[i].value, elSel.options[i].text, newList);
		}
	}

	uncheckAll(document.getElementById(mList));
}

function addClientsToList(optionValue, optionText, newList)
{
	var found = 0;

	for (i = newList.length - 1; i>=0; i--) {
		if (newList.options[i].value ==optionValue) {
		 found = 1;
		}
	}

	if (found == 0)
	{
		var elOptNew = document.createElement('option');
		elOptNew.text = optionText;
		elOptNew.value = optionValue;

		try {
		newList.add(elOptNew, null); // standards compliant; doesn't work in IE
		}
		catch(ex) {
		newList.add(elOptNew); // IE only
		}
	}

}

function removeOptionSelected(listID)
{
  var elSel = document.getElementById(listID);
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
	if (elSel.options[i].selected) {
	  elSel.remove(i);
	}
  }
}

function changeJammerzOutfit(pID, elementToChange)
{
//	alert(countryID);

	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="/admin/ajax_php/changeJammerzOutfit.php";
	url=url+"?id="+pID;
	url=url+"&sid="+Math.random();

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

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

function changeJammerzDropDown(pID, elementToChange)
{
	//alert(pID);

	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="/admin/ajax_php/changeJammerzDropDown.php";
	url=url+"?id="+pID;
	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 processResponse(form, responseID, elementToChange, adminID)
{
	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 

	var url="/admin/ajax_php/processResponse.php";
	url=url+"?cr_id="+responseID;
	url=url+"&admin_id="+adminID;
	url=url+"&cr_response_"+responseID+"="+encodeURI(document.getElementById("cr_response_"+responseID).value);
	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 getLotSizeBoxes(elementToChange, pid, lotType)
{
	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 
	var url="/admin/ajax_php/getLotSizeBoxes.php";
	url=url+"?pid="+pid;
	url=url+"&lotType="+lotType;
	url=url+"&sid="+Math.random();

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

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


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

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

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


function ajax_change_shipping_rates(elementToChange, shippingCountry, shippingState, value, totalItems2, totalPrice2)
{	

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


	var url="/admin/ajax_php/calculateShipping.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 poststr2 = "boxChecked=" +  encodeURI(value) +
		  		  "&totalItems=" + encodeURI(totalItems2) +
		  		  "&totalPrice=" + encodeURI(totalPrice2) +
		  		  "&shippingCountry=" + encodeURI(shippingCountry) + 
		  		  "&shippingState=" + encodeURI(shippingState);

	var t;

	xmlHttp2.onreadystatechange=blah;

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

   xmlHttp2.open("POST",url,true);
   xmlHttp2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp2.setRequestHeader("Content-length", poststr2.length);
   xmlHttp2.setRequestHeader("Connection", "close");
   xmlHttp2.send(poststr2);
}


function ajax_change_shipping_price(elementToChange, shippingCountry, shippingState, totalItems, shippingType, totalPrice)
{	
	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 


	var url="/admin/ajax_php/calculateShippingPrice.php";
	
	var poststr = "totalItems=" + encodeURI(totalItems) +
		  		  "&shippingType=" + encodeURI(shippingType) + 
		  		  "&shippingCountry=" + encodeURI(shippingCountry) + 
		  		  "&shippingState=" + encodeURI(shippingState) + 
		  		  "&totalPrice=" + encodeURI(totalPrice);

	var t;

	xmlHttp.onreadystatechange=bleh;

	function bleh()
    {	
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.status==200)
			{		
				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);
}

function ajax_change_total_price(elementToChange, shippingCountry, shippingState, shippingType, totalItems, totalPrice)
{	
	xmlHttp3 = getXMLHttpObject();
	if (xmlHttp3==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 


	var url="/admin/ajax_php/calculateTotalPrice.php";
	
	var poststr3 = "totalItems=" + encodeURI(totalItems) +
		  		  "&shippingType=" + encodeURI(shippingType)+ 
				  "&totalPrice=" + encodeURI(totalPrice) +
				  "&shippingCountry=" + encodeURI(shippingCountry) + 
		  		  "&shippingState=" + encodeURI(shippingState);

	var t;

	xmlHttp3.onreadystatechange=bleh;

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

   xmlHttp3.open("POST",url,true);
   xmlHttp3.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp3.setRequestHeader("Content-length", poststr3.length);
   xmlHttp3.setRequestHeader("Connection", "close");
   xmlHttp3.send(poststr3);
}


function ajax_change_shipping_type(elementToChange, shippingCountry, shippingState, boxChecked, totalItems, totalPrice)
{	
	xmlHttp4 = getXMLHttpObject();
	if (xmlHttp4==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 


	var url="/admin/ajax_php/calculateShippingType.php";
	
	var poststr4 = "totalItems=" + encodeURI(totalItems) +
				  "&totalPrice=" + encodeURI(totalPrice) +
				  "&shippingCountry=" + encodeURI(shippingCountry) + 
		  		  "&shippingState=" + encodeURI(shippingState) + 
		  		  "&boxChecked=" + encodeURI(boxChecked);

	var t;

	xmlHttp4.onreadystatechange=bleh;

	function bleh()
    {	
		if(xmlHttp4.readyState==4)
		{
			if(xmlHttp4.status==200)
			{		
				document.getElementById(elementToChange).value = xmlHttp4.responseText;
			 }
				
		}
	
    }

   xmlHttp4.open("POST",url,true);
   xmlHttp4.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp4.setRequestHeader("Content-length", poststr4.length);
   xmlHttp4.setRequestHeader("Connection", "close");
   xmlHttp4.send(poststr4);
}


function ajax_change_all_shipping_info(ratesElement, priceElement, totalPriceElement, shippingCountry, shippingState, shippingType, boxChecked, totalItems, totalPrice)
{
	/* First check to see if any shipping options are already selected */ 

	/* Do all shipping changes with the following three AJAX functions */
	ajax_change_shipping_rates(ratesElement, shippingCountry, shippingState, boxChecked, totalItems, totalPrice);
	ajax_change_shipping_price(priceElement, shippingCountry, shippingState, totalItems, shippingType, totalPrice);
	ajax_change_total_price(totalPriceElement, shippingCountry, shippingState, shippingType, totalItems, totalPrice);
	ajax_change_shipping_type("shippingType", shippingCountry, shippingState, boxChecked, totalItems, totalPrice);

//	alert(document.getElementById('tests').value);
}

function ajax_lots_change_total_price(elementToChange, shippingCountry, totalPrice, domesticShipping, internationalShipping)
{	
	xmlHttp3 = getXMLHttpObject();
	if (xmlHttp3==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 


	var url="/admin/ajax_php/calculateLotsTotalPrice.php";
	
	var poststr3 = "shippingCountry=" + encodeURI(shippingCountry) +
				  "&totalPrice=" + encodeURI(totalPrice) + 
				  "&domesticShipping=" + encodeURI(domesticShipping) + 
				  "&internationalShipping=" + encodeURI(internationalShipping);

	var t;

	xmlHttp3.onreadystatechange=bleh;

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

   xmlHttp3.open("POST",url,true);
   xmlHttp3.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp3.setRequestHeader("Content-length", poststr3.length);
   xmlHttp3.setRequestHeader("Connection", "close");
   xmlHttp3.send(poststr3);
}

function ajax_lots_change_shipping_price(elementToChange, shippingCountry, domesticShipping, internationalShipping)
{	
	xmlHttp = getXMLHttpObject();
	if (xmlHttp==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 


	var url="/admin/ajax_php/calculateLotsShippingPrice.php";
	
	var poststr = "shippingCountry=" + encodeURI(shippingCountry) +
		  		  "&domesticShipping=" + encodeURI(domesticShipping) +
		  		  "&internationalShipping=" + encodeURI(internationalShipping);

	var t;

	xmlHttp.onreadystatechange=bleh;

	function bleh()
    {	
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.status==200)
			{		
				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);
}


function get_quantity_drop_down(elementToChange, priceID)
{	
	xmlHttp3 = getXMLHttpObject();
	if (xmlHttp3==null)
	{
		alert ("Your browser does not support AJAX!");
		return;
	} 


	var url="/admin/ajax_php/getQuantityDropDown.php";
	
	var poststr3 = "priceID=" + priceID +
				  "&sid="+Math.random();
	var t;

	xmlHttp3.onreadystatechange=bleh;

	function bleh()
    {	
		if(xmlHttp3.readyState==4)
		{
			if(xmlHttp3.status==200)
			{		
				document.getElementById(elementToChange).innerHTML=xmlHttp3.responseText;
			}				
		}
	
    }

   xmlHttp3.open("POST",url,true);
   xmlHttp3.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   xmlHttp3.setRequestHeader("Content-length", poststr3.length);
   xmlHttp3.setRequestHeader("Connection", "close");
   xmlHttp3.send(poststr3);
}


function validateLotsCustomerForm(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 (errorString!='')
	{
		alert("The following information is required:\n" + errorString);
		return false;
	}
	else
	{
		return true;
	}
}
// Non-ajax list management functions - used for two lists manipulations (one is a main list, and the other is the new list)
function uncheckAll(obj) {
	for (var loop=0; loop < obj.options.length; loop++) {
	obj.options[loop].selected = false;
	}
}
function selectAll(listID)
{
	var newList = document.getElementById(listID);

	for (i = newList.length - 1; i>=0; i--)
	{
		newList.options[i].selected = true;
	}
}
function addSelectedClientsToList(mList, nList)
{
	var elSel = document.getElementById(mList);
	var newList = document.getElementById(nList);

	var i;
	for (i = elSel.length - 1; i>=0; i--)
	{
		if (elSel.options[i].selected)
		{
			addClientsToList(elSel.options[i].value, elSel.options[i].text, newList);
		}
	}

	uncheckAll(document.getElementById(mList));
}

function addClientsToList(optionValue, optionText, newList)
{
	var found = 0;

	for (i = newList.length - 1; i>=0; i--) {
		if (newList.options[i].value ==optionValue) {
		 found = 1;
		}
	}

	if (found == 0)
	{
		var elOptNew = document.createElement('option');
		elOptNew.text = optionText;
		elOptNew.value = optionValue;

		try {
		newList.add(elOptNew, null); // standards compliant; doesn't work in IE
		}
		catch(ex) {
		newList.add(elOptNew); // IE only
		}
	}

}

function removeOptionSelected(listID)
{
  var elSel = document.getElementById(listID);
  var i;
  for (i = elSel.length - 1; i>=0; i--) {
	if (elSel.options[i].selected) {
	  elSel.remove(i);
	}
  }
}