// JavaScript Document
function getXMLObject()  //XML OBJECT
{
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
     }
     catch (e2) {
       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;  // Mandatory Statement returning the ajax object created
}
 
var xmlhttp = new getXMLObject();	//xmlhttp holds the ajax object

function addItem(id) {
		fcartFunction();
	var qty = document.getElementById('qty_'+id).value;
	var getdate = new Date();  //Used to prevent caching during ajax call
  if(xmlhttp) { 
  	var sendstr;
		sendstr="cartadd.php?id="+id+"&qty="+qty;
    xmlhttp.open("GET",sendstr,true); //calling testing.php using POST method
    xmlhttp.onreadystatechange  = handleCartAdd;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(null); //Posting txtname to PHP File
	
  }
}

function ecartAdd(id) {
		fcartFunction();
	var qty = document.getElementById('qty_'+id).value;
	var getdate = new Date();  //Used to prevent caching during ajax call
  if(xmlhttp) { 
  	var sendstr;
		sendstr="ecart.php?id="+id+"&action=add";
    xmlhttp.open("GET",sendstr,true); //calling testing.php using POST method
    xmlhttp.onreadystatechange  = handleEadd;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(null); //Posting txtname to PHP File
	
  }
}

function ecartSub(id) {
		fcartFunction();
	var qty = document.getElementById('qty_'+id).value;
	var getdate = new Date();  //Used to prevent caching during ajax call
  if(xmlhttp) { 
  	var sendstr;
		sendstr="ecart.php?id="+id+"&action=sub";
    xmlhttp.open("GET",sendstr,true); //calling testing.php using POST method
    xmlhttp.onreadystatechange  = handleEadd;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(null); //Posting txtname to PHP File
	
  }
}

function cartCheck(id) {
	var getdate = new Date();  //Used to prevent caching during ajax call
  if(xmlhttp) { 
  	var sendstr;
		sendstr="cartcheck.php?id="+id;
		//alert (sendstr);
    xmlhttp.open("GET",sendstr,true); //calling testing.php using POST method
	//alert (sendstr);
    xmlhttp.onreadystatechange  = handleCartCheck;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(null); //Posting txtname to PHP File
	
  }
  

}

function handleEadd() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
		
     } 
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}

function handleCartAdd() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
		var id=xmlhttp.responseText;
		document.getElementById('qty_'+id).value=1;
		cartCheck(id);
     } 
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
   fcartFunction();
}

function handleCartCheck() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
		var tc=xmlhttp.responseText;
				var info_array=tc.split("|");
		if (!(info_array[1]=="none")) {
		document.getElementById('prod_'+info_array[0]).innerHTML="<font size='2'><center><b>" + info_array[1] + "<br></font><font size='1'>Currently in<br>Your Cart</b></center></font>";
		document.getElementById('added_'+info_array[0]).style.display="block";
		}
     } 
     else {
        //alert("Error during AJAX call. Please try again");
     }
   }
}

//TIME REFRESH
function fcartFunction(){

var divid = "scrat_qty";
var url = "scrat.php";

// The XMLHttpRequest object

var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}

// Timestamp for preventing IE caching the GET request

fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}

var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp;

// The code...

xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
	if (xmlHttp.responseText!="NO CART") {
	document.getElementById("floatdiv").style.display="block";
	document.getElementById("floatdiv").innerHTML=xmlHttp.responseText;
	setTimeout('fcartFunction()',1*1000);
	}
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

//TIME REFRESH
function ecartQtyRefresh(id){

var divid = "qty_" + id;
var url = "qtycart.php";

// The XMLHttpRequest object

var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}

// Timestamp for preventing IE caching the GET request

fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}

var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp+"&id="+id;

// The code...

xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
	if (xmlHttp.responseText!="NO CART") {
	document.getElementById("qty_"+id).innerHTML=xmlHttp.responseText;
		if (xmlHttp.responseText==1) {
			document.getElementById("esub_"+id).style.display="none";	
		} else {
			document.getElementById("esub_"+id).style.display="block";	
		}
	setTimeout('ecartQtyRefresh('+id+')',1*1000);
	}
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

//TIME REFRESH
function lineTot(id){

var divid = "ltot_" + id;
var url = "linetotal.php";

// The XMLHttpRequest object

var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}

// Timestamp for preventing IE caching the GET request

fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}

var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp+"&id="+id;

// The code...

xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
	if (xmlHttp.responseText!="NO CART") {
	document.getElementById("ltot_"+id).innerHTML=xmlHttp.responseText;
	setTimeout('lineTot('+id+')',1*1000);
	}
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

//TIME REFRESH
function stotRefresh(){

var divid = "sub_total";
var url = "subtotal.php";

// The XMLHttpRequest object

var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}

// Timestamp for preventing IE caching the GET request

fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}

var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp;

// The code...

xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
	if (xmlHttp.responseText!="NO CART") {
	document.getElementById("subtotal").innerHTML=xmlHttp.responseText;
	setTimeout('stotRefresh()',1*1000);
	}
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

//TIME REFRESH
function shipRefresh(){

var divid = "ship";
var url = "shipping.php";

// The XMLHttpRequest object

var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}

// Timestamp for preventing IE caching the GET request

fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}

var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp;

// The code...

xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
	if (xmlHttp.responseText!="NO CART") {
	document.getElementById("ship").innerHTML=xmlHttp.responseText;
	setTimeout('shipRefresh()',1*1000);
	}
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

//TOTAL REFRESH
function totRefresh(state){

var divid = "total";
var url = "total.php";

// The XMLHttpRequest object

var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}

// Timestamp for preventing IE caching the GET request

fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}

var timestamp = fetch_unix_timestamp();
if (state=="none") {
var nocacheurl = url+"?t="+timestamp;
} else {
var nocacheurl = url+"?t="+timestamp+"&state="+state;	
}

// The code...

xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
	if (xmlHttp.responseText!="NO CART") {
	document.getElementById("total").style.display="block";
	document.getElementById("total").innerHTML=xmlHttp.responseText;
	setTimeout('totRefresh()',1*1000);
	}
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

function salesTax() {
	totRefresh(document.getElementById('state').value);	
}

function taxRefresh(){
	
var divid = "tax";
var url = "tax.php";

// The XMLHttpRequest object

var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}

// Timestamp for preventing IE caching the GET request

fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}

var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp;

// The code...

xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4){
	document.getElementById("tax").style.display="block";
	document.getElementById("tax").innerHTML=xmlHttp.responseText;
	setTimeout('taxRefresh()',1*1000);
	}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

function getXMLObject()  //XML OBJECT
{
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
     }
     catch (e2) {
       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;  // Mandatory Statement returning the ajax object created
}
 
var xmlhttp = new getXMLObject();	//xmlhttp holds the ajax object

function payPal() {
	var name = document.getElementById('name').value;
	var email = document.getElementById('email').value;
	var phone = document.getElementById('phone').value;
	var shipto = document.getElementById('shipto').value;
	var address = document.getElementById('address').value;
	var city = document.getElementById('city').value;
	var state = document.getElementById('state').value;
	var zip = document.getElementById('zip').value;
	if(xmlhttp) { 
  	var sendstr;
		sendstr="shippadd.php?name="+name+"&email="+email+"&phone="+phone+"&shipto="+shipto+"&address="+address+"&city="+city+"&state="+state+"&zip="+zip;
    xmlhttp.open("GET",sendstr,true); //calling testing.php using POST method
    xmlhttp.onreadystatechange  = handleShipAdd;
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(null); //Posting txtname to PHP File
	
  }
}

function handleShipAdd() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
		if (xmlhttp.responseText=="SUCCESS!") {
		payPalGo();
		} else {
		document.getElementById('shipperr').innerHTML=xmlhttp.responseText;
		}
     } 
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}

function payPalGo() {
	var numlines = document.getElementById("lines").value;
	var x=0;
	var itm ="";
	do {
		if (itm!=null) {
			itm=itm+"&";
		}
		x++;
		itm = itm+"item_name_"+x+"="+document.getElementById('itm_name_'+numlines).value+"&amount_"+x+"="+document.getElementById('itm_cost_'+numlines).value+"&quantity_"+x+"="+document.getElementById('itm_qty_'+numlines).value;
		
		numlines--;
	} while (numlines>0);
		//x=x+1;
		//itm=itm+"&item_name_"+x+"=Shipping&amount_"+x+"="+document.getElementById('ship').innerHTML;
		//x=x+1;
		//itm=itm+"&item_name_"+x+"=Handling&amount_"+x+"=2.00";
		//x=x+1;
		//itm=itm+"&item_name_"+x+"=Tax&amount_"+x+"="+document.getElementById('tax').innerHTML;
	//alert(itm);
	//alert("&tax="+document.getElementById('tax').innerHTML);
	itm=itm+"&tax_1="+document.getElementById('tax').innerHTML;
	itm=itm+"&shipping_1="+document.getElementById('ship').innerHTML;
	itm=itm+"&handling_1=2.00";
	itm=itm+"&return=http://www.mainstreetpiqua.com/thanks.php&rm=0";
	
	window.location="https://www.paypal.com/cgi-bin/webscr/index.php?cmd=_cart&upload=1&business=mainstreetpiqua@woh.rr.com"+itm+"&tax="+document.getElementById('tax').innerHTML;
	//window.location="https://www.paypal.com/cgi-bin/webscr/index.php?cmd=_cart&upload=1&business=mainstreetpiqua@woh.rr.com&item_name_1=Aggregated items&amount_1="+document.getElementById('amount').value;
	
}
