/**************************************************************
 * FileName: functions.js
 * Author: ZhenAn Zhao
 * Company: ExperExchange
 * Date Created: 09/22/2006
 *
 * Description: javascript functions used in the pages
 *
 * Last Modified: 09/22/2006
 *
 * Copyright 2006 ExperExchange Inc, All rights reserved.
 **************************************************************/


function goto_index()
{
 window.location = "index.php";
}
function goto_chat()
{
 window.location = "./chat/index.php";
}
function goto_checkout()
{
 window.location = "bs.php";
}
function goto_edit_bs()
{
 window.location = "bs.php?cmd=confirm";
}
function goto_payment()
{
 window.location = "payment.php";
}
function goto_pay()
{
 window.location = "thank.php";
}
function goto_cancel_confirm()
{
 window.location = "cancel_confirm.php";
}
function goto_cart()
{
 window.location = "show_cart.php";
}
function check_checkbox2()
{
  if(!document.hidform.checkbox2.checked)
  {
    alert("Please  Check Box To Approve These Proofs");
	return false;
  }
  return true;
}



function checkform()
{
 // t = document.hidform;
 if(document.priceform.size.value=='')
   { alert("Please choose a size");
   return false;
   }
 else if(document.priceform.paper.value=='')
   { alert("Please choose a paper");
   return false;
   }
    else if(document.priceform.color.value=='')
   { alert("Please choose a color");
   return false;
   }
    else if(document.priceform.fold.value=='')
   { alert("Please choose a fold");
   return false;
   }
    else if(document.priceform.quantity.value=='')
   { alert("Please choose a quantity");
   return false;
   }
    else if(document.priceform.service_type.checked ==1)
   { 
    alert("Please choose a type of service");
    return false;
   }
   return true;
}
function check_fileform()
{
 // t = document.hidform;
 if(document.fileform.front.value.length<4)
   { alert("Please choose a file");
   return false;
   }
   return true;
}
function fill_ship()
{
	// fill the values same as billing info
	if( document.getElementById('same_b').checked )
	{
		document.getElementById('email_s').value=document.getElementById('email_b').value;
		document.getElementById('first_name_s').value=document.getElementById('first_name_b').value;
		document.getElementById('last_name_s').value=document.getElementById('last_name_b').value;
		document.getElementById('company_name_s').value=document.getElementById('company_name_b').value;
		document.getElementById('address_s').value=document.getElementById('address_b').value;
		document.getElementById('address2_s').value=document.getElementById('address2_b').value;		
		document.getElementById('city_s').value=document.getElementById('city_b').value;
		document.getElementById('state_s').selectedIndex = document.getElementById('state_b').selectedIndex;
		document.getElementById('diff_state_s').value=document.getElementById('diff_state_b').value;
		document.getElementById('country_s').selectedIndex=document.getElementById('country_b').selectedIndex;
		document.getElementById('zip_s').value=document.getElementById('zip_b').value;
		document.getElementById('phone_s').value=document.getElementById('phone_b').value;

	  }
	  else if(!document.getElementById('same_b').checked) 
	  {
	    document.getElementById('email_s').value='';
		document.getElementById('first_name_s').value='';
		document.getElementById('last_name_s').value='';
		document.getElementById('company_name_s').value='';
		document.getElementById('address_s').value='';
		document.getElementById('address2_s').value='';
		document.getElementById('city_s').value='';
		document.getElementById('state_s').selectedIndex=0;
		document.getElementById('diff_state_s').value='';
		document.getElementById('country_s').selectedIndex=0;
		document.getElementById('zip_s').value='';
		document.getElementById('phone_s').value='';

	  }

}


function check_bsform()
{
	if(document.bsform.initial.value.length==0)
	{
		 alert("Please enter initial words");
		 return false;
	}
	else if(!document.bsform.term_check.checked)
	{
		alert("Please check the term checkbox before you submit you info!");
		 return false;
	}
}

//Ajax Function
function Ajax()
{
	var xmlhttp;
	try
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");
	} catch (e)
	{
		try
		{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e)
		{
			try 
			{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) 
			{
				xmlhttp = false;
			}
		}
	}
	//for firefox
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{
		xmlhttp = new XMLHttpRequest();
	}

	this.xmlhttp=xmlhttp;

  function responseIsSuccess() 
  {
		return xmlhttp.status == undefined 
	    || xmlhttp.status == 0 
    	|| (xmlhttp.status >= 200 && xmlhttp.status < 300); 
  }
		
	this.getURL=function(url,asynchronous,callback) 
	{
		xmlhttp.open('GET', url, asynchronous); 
		if (asynchronous) xmlhttp.onreadystatechange = function()
		{
			if(xmlhttp.readyState == 4)
			{
				if(responseIsSuccess()) callback(xmlhttp.responseText);
			}
		}
		xmlhttp.send(null); 
		if (asynchronous==false) 
		{
			if(responseIsSuccess()) callback(xmlhttp.responseText);
		}
	}
}

//Insert Script 
function InsertScript(URL)
{
	var script;
	script=document.createElement("script");
	script.type="text/javascript";
	script.src=URL;
	document.getElementsByTagName("head")[0].appendChild(script);
}

//XML Response Process
function ProcessXML(XML)
{
	//alert(XML);
	eval(XML);	
}

//Execute JS use AJAX or DOM
function executeJSFile(url){
	if(executeJSFile.arguments.length>1&&executeJSFile.arguments[1]=='DOM') InsertScript(url);
	else {
		var ajax=new Ajax();
		if(ajax.xmlhttp!=null) ajax.getURL(url,true,ProcessXML);	//Use AJAX
		else InsertScript(url);										//Use DOM
	}
}


// get quantity list
function get_quantities()
{
	 if(document.getElementById('size').value != "")
	  { 
	    var url = "ajax.php?paper="+document.getElementById('paper').value+"&product_id="
		           +document.getElementById('product_id').value+"&size="+document.getElementById('size').value;
		//alert(url);
		document.getElementById('quantity').value = "";
	    executeJSFile(url);
		return;
	  }
	 else
	 {
	   document.getElementById('paper').value = "";
	   alert("Please select a size first");
	   return;
	 }
     
}

// get the pirce of chosen options
function get_price()
{   var url = "ajax.php?product_id="+document.getElementById('product_id').value;
     // size
    if(document.getElementById('size').value != "")
	  { 
	    url += "&size="+document.getElementById('size').value;
	  }
	 else
	 {
	  
	  //alert("Please select a size");
	  return;
	 }
	 //paper
	 if(document.getElementById('paper').value != "")
	  { 
	    url += "&paper="+document.getElementById('paper').value;
	  }
	 else
	 {
	  
	  //alert("Please select a paper");
	  return;
	 }
	 //color
	 if(document.getElementById('color').value != "")
	  { 
	    url += "&color="+document.getElementById('color').value;
	  }
	 else
	 {
	  
	 // alert("Please select a color");
	  return;
	 }
	 // fold
	 if(document.getElementById('fold').value != "")
	  { 
	    url += "&fold="+document.getElementById('fold').value;
	  }
	 else
	 {
	 // alert("Please select a fold");
	  return;
	 }
	 //quantity
	 if(document.getElementById('quantity').value != "")
	  { 
	    url += "&quantity="+document.getElementById('quantity').value;
	  }
	 else
	 {
	 // alert("Please select quantity");
	  return;
	 }
	// alert(url);
	 executeJSFile(url);
}


function check_designform()
{
	if(document.designform.front_desc.value.length < 100)
	{
		 alert("Please enter description of front side");
		 return false;
	}
	else if(document.designform.back_desc.value.length<100)
	{
		 alert("Please enter description of back side");
		 return false;
	}
	else return true;
}

function get_shipping()
{
 alert('Please wait, Shipping calculate may take a few seconds');
  var url = 'get_ship.php?country_s=';
  url += document.getElementById('country_s').value;
  
  if(document.getElementById('zip_s').value!= "" )
   {
	   url += '&zip_s=';
	   url += document.getElementById('zip_s').value;
   }
 
  if(document.shipform.shipping_option.selectedIndex==0)
  {
    alert("Please  select a shippping option");
	return false;
  }
   url += '&shipping_option=';
   url += document.getElementById('shipping_option').value;
  //alert(url);
  
   url += "&state_s="+document.getElementById('state_s').value;
   
  executeJSFile(url);
}
