// JavaScript Document
var time_variable;
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 mask(f) {
//tel='';	
		  var val=f.value.split('');

		  if (val.length==1){
			  if (val[0]=='(') {
					val[0]='';
				 }
			  tel='(';
		  } else {
			  tel='';
		  }
		  for(var i=0; i<val.lenth; i++){
			  	//if (val[i]=='('){
					//val[i]='';
				//}
				//if(val[i]==')'){
				  //val[i]='';
				//}
				//if (val[i]=='-'){
					//val[i]='';
			  	//}
			  	if (val[i]==' '){
				  	val[i]='';
			  	}
		  }
for(var i=0; i<val.length; i++){
	//alert(i+" "+val[i]);
//if(i==0){ tel='('; }
				  
if(i==4 && val[i]!=')'){ val[i]=') '+val[i]; }
if(i==9 && val[i]!="-"){ val[i]='-'+val[i]; }
if(i==14 && val[i]!="x"){ val[i]='x'+val[i]; }
var final;
	tel=tel+val[i];
	final=tel;
}
document.getElementById("phone").value=tel;
}

function saddFunction(sku,qty,idname) {

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

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

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

function printSelection(node){

  var content=node.innerHTML
  var pwin=window.open('','print_content','width=100,height=100');
  pwin.document.open();
  pwin.document.write('<html><body onload="window.print()">'+content+'</body></html>');
  pwin.document.close();
  setTimeout(function(){pwin.close();},1000);
  
  var sendstr;
			sendstr="finishcheckout.php";
			xmlhttp.open("get",sendstr,true);
		 	xmlhttp.onreadystatechange  = handleResonseServer;
			xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			xmlhttp.send(null); //Posting txtname to PHP File
}

function handleResonseServer() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
	   document.getElementById("order_response").innerHTML=xmlhttp.responseText; //Update the HTML Form element 
	   }
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}

function checkfunction(nmefield,addfield,cityfield,statefield,zipfield,phonefield,emailfield,cfield) {
	document.getElementById("order_response").innerHTML=""
	var response = document.getElementById("order_response");
	if (nmefield == "") {
		document.getElementById('name').style.background="#993300";
		document.getElementById('name').style.color="#FFFFFF";
		var pTag = document.createElement("p");
		pTag.setAttribute("align","center", "strong");
		pTag.innerHTML = "Please Enter Your Name!";
		response.appendChild(pTag);
		var nmerr=true;
	} else {
		document.getElementById('name').style.background="#FFFFFF";
		document.getElementById('name').style.color="#000000";
		var nmerr=false;
		var nme="?name="+nmefield;
	}
	
	if (addfield == "") {
		document.getElementById("address").style.background="#993300";
		document.getElementById("address").style.color="#FFFFFF";
		var pTag = document.createElement("p");
		pTag.setAttribute("align","center", "strong");
		pTag.innerHTML = "Please Enter Your Address!";
		response.appendChild(pTag);
		var aderr=true;
	} else {
		document.getElementById("address").style.background="#FFFFFF";
		document.getElementById("address").style.color="#000000";
		var aderr=false;
		var add="&address="+addfield;
	}
	
	if (cityfield == "") {
		document.getElementById("city").style.background="#993300";
		document.getElementById("city").style.color="#FFFFFF";
		var pTag = document.createElement("p");
		pTag.setAttribute("align","center", "strong");
		pTag.innerHTML = "Please Enter Your City!";
		response.appendChild(pTag);
		var cterr=true;
	} else {
		document.getElementById("city").style.background="#FFFFFF";
		document.getElementById("city").style.color="#000000";
		var cterr=false;
		var cty="&city="+cityfield;
	}
	
	if (statefield == "") {
		document.getElementById("state").style.background="#993300";
		document.getElementById("state").style.color="#FFFFFF";
		var pTag = document.createElement("p");
		pTag.setAttribute("align","center", "strong");
		pTag.innerHTML = "Please Enter Your state!";
		response.appendChild(pTag);
		var sterr=true;
	} else {
		document.getElementById("state").style.background="#FFFFFF";
		document.getElementById("state").style.color="#000000";
		var sterr=false;
		var ste="&state="+statefield;
	}
	
	if (zipfield == "") {
		document.getElementById("zip").style.background="#993300";
		document.getElementById("zip").style.color="#FFFFFF";
		var pTag = document.createElement("p");
		pTag.setAttribute("align","center", "strong");
		pTag.innerHTML = "Please Enter Your zip!";
		response.appendChild(pTag);
		var zperr=true;
	}  else if (zipfield.length < 5) {
		document.getElementById("zip").style.background="#993300";
		document.getElementById("zip").style.color="#FFFFFF";
		var pTag = document.createElement("p");
		pTag.setAttribute("align","center", "strong");
		pTag.innerHTML = "Please Enter a Valid Zip Code!";
		response.appendChild(pTag);
		var zperr=true;
	} else {
		document.getElementById("zip").style.background="#FFFFFF";
		document.getElementById("zip").style.color="#000000";
		var zperr=false;
		var zp="&zip="+zipfield;
	}
	
	if (phonefield == "") {
		document.getElementById("phone").style.background="#993300";
		document.getElementById("phone").style.color="#FFFFFF";
		var pTag = document.createElement("p");
		pTag.setAttribute("align","center", "strong");
		pTag.innerHTML = "Please Enter Your phone!";
		response.appendChild(pTag);
		var pherr=true;
	} else {
		document.getElementById("phone").style.background="#FFFFFF";
		document.getElementById("phone").style.color="#000000";
		var pherr=false;
		var ph="&phone="+phonefield;
	}
	
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (emailfield.search(filter)==-1) {
		document.getElementById("email").style.background="#993300";
		document.getElementById("email").style.color="#FFFFFF";
		var pTag = document.createElement("p");
		pTag.setAttribute("align","center", "strong");
		pTag.innerHTML = "Please Enter a valid email address!";
		response.appendChild(pTag);
		var emerr=true;
	} else {
		document.getElementById("email").style.background="#FFFFFF";
		document.getElementById("email").style.color="#000000";
		var emerr=false;
		var eml="&email="+emailfield;
	}
	
		//if (cfield=="cc") {
		
			//var ccnmefield=document.getElementById("ccname").value;
			//var ccnumfield=document.getElementById("ccnum").value;
			//var ccexpmonfield=document.getElementById("cc_mon").value;
			//var ccexpyearfield=document.getElementById("cc_year").value;
				
		//if (ccnmefield=="") {
			//document.getElementById("ccname").style.background="#993300";
			//document.getElementById("ccname").style.color="#FFFFFF";
			//var pTag = document.createElement("p");
			//pTag.setAttribute("align","center", "bold");
			//pTag.innerHTML = "Please Enter The Name On Your Credit Card!";
			//response.appendChild(pTag);
			//var ccnmerr=true;	
		//} else {
			//document.getElementById("ccname").style.background="#FFFFFF";
			//document.getElementById("ccname").style.color="#000000";
			//var ccnmerr=false;
		//}
		
		//if (ccnumfield=="") {
			//document.getElementById("ccnum").style.background="#993300";
			//document.getElementById("ccnum").style.color="#FFFFFF";
			//var pTag = document.createElement("p");
			//pTag.setAttribute("align","center", "bold");
			//pTag.innerHTML = "Please Enter Your Credit Card Number!";
			//response.appendChild(pTag);
			//var ccnumerr=true;	
		//} else {
			//document.getElementById("ccnum").style.background="#FFFFFF";
			//document.getElementById("ccnum").style.color="#000000";
			//var ccnumerr=false;
		//}	
	//}
	
	if (nmerr==true || aderr==true || cterr==true || sterr==true || zperr==true || pherr==true || emerr==true) {
	alert("Errors were Found!");
	} else {
	var err = false;
	}
	
	if (err==false && cfield=="check"){
		if(xmlhttp) {
		var sendstr;
			sendstr="checkpay.php"+nme+add+cty+ste+zp+ph+eml+"&ptype=check";	
		xmlhttp.open("get",sendstr,true);
		xmlhttp.onreadystatechange  = handleServerResponse;
    	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	xmlhttp.send(null); //Posting txtname to PHP File
		}
	} 

	if (err==false && cfield=="cc") {
		
		if(xmlhttp) {
		var sendstr;
			sendstr="checkpay.php"+nme+add+cty+ste+zp+ph+eml+"&ptype=credit";
		//window.location=sendstr;
		xmlhttp.open("get",sendstr,true);
		xmlhttp.onreadystatechange  = handleServerResponse;
    	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	xmlhttp.send(null); //Posting txtname to PHP File
		}
	} 
}

	

function handleServerResponse() {
   if (xmlhttp.readyState == 4) {
     if(xmlhttp.status == 200) {
	   document.getElementById("enter_info").style.display="none";
	   document.getElementById("check_out").style.display="block";
	   document.getElementById("check_out").innerHTML=xmlhttp.responseText; //Update the HTML Form element 
	   }
     else {
        alert("Error during AJAX call. Please try again");
     }
   }
}

//cart refresh
function cartRefresh(){
var divid = "cart";
var url = "cart.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(divid).innerHTML=xmlHttp.responseText;
setTimeout('cartRefresh()',1*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

// Start the refreshing process

window.onload = function startrefresh(){
setTimeout('cartRefresh()',1*1000);
}

//edit cart refresh
function editCartRefresh(){
var divid = "ecart";
var url = "edit_cart.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(divid).innerHTML=xmlHttp.responseText;
setTimeout('editCartRefresh()',10*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

// Start the refreshing process

window.onload = function startrefresh(){
setTimeout('editCartRefresh()',10*1000);
}


function zoompic(pic) {
document.getElementById(pic).style.display='block'
document.getElementById(pic).style.float='none'
}
function closepic(pic) {
document.getElementById(pic).style.display='none'
}

//load shipping info form
function shipFunction(){
var divid = "ship_info";
var url = "ship_form.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(divid).innerHTML=xmlHttp.responseText;
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

function ccardchange(ccheck) {
if (ccheck == "cc") {
	document.getElementById('ccard').style.display='block';
} else if (ccheck == "check") {
	document.getElementById('ccard').style.display='none';
}
}

function makechanges() {
	document.getElementById("enter_info").style.display="block";
	document.getElementById("check_out").style.display="none";
}

function changecolor(row,rid){
	alert("HERE "+row+" - "+rid);
	if (row==1) {
	document.getElementById(rid).style.background="#FFFFFF";
	}
	
	if (row==2) {
	document.getElementById(rid).style.background="#CCCCCC";
	}
}

//edit cart refresh
function editCartRefresh(){
var divid = "ecart";
var url = "edit_cart.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(divid).innerHTML=xmlHttp.responseText;
setTimeout('editCartRefresh()',10*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

// Start the refreshing process

window.onload = function startrefresh(){
setTimeout('editCartRefresh()',10*1000);
}


function zoompic(pic) {
document.getElementById(pic).style.display='block'
document.getElementById(pic).style.float='none'
}
function closepic(pic) {
document.getElementById(pic).style.display='none'
}

//load shipping info form
function prevRefresh(){
var divid = "check_out";
var url = "checkpay.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(divid).innerHTML=xmlHttp.responseText;
setTimeout('prevRefresh()',2*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

// Start the refreshing process

window.onload = function startrefresh(){
setTimeout('prevRefresh()',2*1000);
}

//load Random Product
function randProdt(){
var divid = "rand_prod";
var url = "random.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(divid).innerHTML=xmlHttp.responseText;
setTimeout('randProdt()',60*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

// Start the refreshing process

window.onload = function startrefresh(){
setTimeout('randProdt()',60*1000);
}

//load Hit Counter
function hitRefresh(){
var divid = "hits";
var url = "counter.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(divid).innerHTML=xmlHttp.responseText;
setTimeout('hitRefresh()',1*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

// Start the refreshing process

window.onload = function startrefresh(){
setTimeout('hitRefresh()',1*1000);
}

//load stats
function statRefresh(){
var divid = "stats";
var url = "stat_refresh.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(divid).innerHTML=xmlHttp.responseText;
setTimeout('statRefresh()',1*1000);
}
}
xmlHttp.open("GET",nocacheurl,true);
xmlHttp.send(null);
}

// Start the refreshing process

window.onload = function startrefresh(){
setTimeout('statRefresh()',1*1000);
}

function dateMask(d) {
//tel='';	
		  var val=d.value.split('');

		  if (val.length==1){
			  //if (val[0]=='0') {
					//val[0]='';
				 //}
			  //tel='0';
		  } else {
			  tel='';
		  }
		  for(var i=0; i<val.lenth; i++){
			  	//if (val[i]=='('){
					//val[i]='';
				//}
				//if(val[i]==')'){
				  //val[i]='';
				//}
				//if (val[i]=='-'){
					//val[i]='';
			  	//}
			  	if (val[i]==' '){
				  	val[i]='';
			  	}
		  }
for(var i=0; i<val.length; i++){
	//alert(i+" "+val[i]);
//if(i==0){ tel='('; }
				  
if(i==2 && val[i]!='/'){ val[i]='/'+val[i]; }
if(i==5 && val[i]!="/"){ val[i]='/'+val[i]; }
//if(i==14 && val[i]!="x"){ val[i]='x'+val[i]; }
var final;
	tel=tel+val[i];
	final=tel;
}
document.getElementById("inv_date").value=tel;
}