// JavaScript Document

/**************************************************** 
* Author: Erik Schweigert
* Module: ShoppingCartHelper.js
* Date: August 14, 2007
*
* Purpose:
*      Shopping Cart Dynamic Functions
****************************************************/


/********************************************
* Get the Locality of the customer from his/her IP address
*********************************************/
function setLocalityFromMaxMind(){
	// Availble function from Max Mind
	// http://j.maxmind.com/app/country.js
	// function geoip_country_code() { return 'CA'; } 
	// function geoip_country_name() { return 'Canada'; }
	
	var value = geoip_country_name();
	alert("Max");
	document.cookie = "localeFromMaxMind=" + value + ";" + "path=/;" + "expires=" + getExpiry(1) + ";";
}


/********************************************
* Grab a cookie based on its name.
*********************************************/
function getCookie(c_name)
{
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=")
		if (c_start!=-1){ 
	    	c_start=c_start + c_name.length+1 
    		c_end=document.cookie.indexOf(";",c_start)
    		if (c_end==-1) c_end=document.cookie.length
    		return unescape(document.cookie.substring(c_start,c_end))
    	} 
  	}
	return "";
}


/****************************************************
* This returns the value for cookie expiration.
* By supplying the number of days.
****************************************************/
function getExpiry(expiredays){
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	return exdate.toGMTString();
}


/****************************************************
* On the working cart page the user can specify quantity
* by 'KeyPress', we check if its alphabetical, if so
* we dont accept it.  Otherwise take the numeric
* and pass it to 'changeQuantity'
*****************************************************/
function noAlpha(object,e)
{
	var keynum, keychar, numcheck;
	if(window.event){ // IE
		keynum = e.keyCode;
	}else if(e.which){ // Netscape/Firefox/Opera
		keynum = e.which;
	}
	
	if(deletedobjectid == null){
			
		if(object.value == "" && keynum == 13){
			if(object.value == ""){
				deletedobjectid = object.id;
				var answer = confirm("Would you like to remove this item from the cart?");
			}
			
			if(answer)
				changeQuantity(0,object.name);
			else
				window.location = "cart.php";
		}
		
	}
	
	// Ignore 'enter'
	if(keynum == 13){
		return false;
	}
	
	keychar = String.fromCharCode(keynum);

	if(!isNaN(keychar)){
		changeQuantity(keychar,object.name);
	}else{
		numcheck = /[a-z]/;
		return !numcheck.test(keychar);
	}
	return "";
}


deletedobjectid = null;
function noInput(object)
{
	var keynum, keychar, numcheck;
	
	if(deletedobjectid == null){
	
		if(object.value == ""){
			deletedobjectid = object.id;
			var answer = confirm("Would you like to remove this item from the cart?");	
		}
		
		if(answer)
			changeQuantity(0,object.name);
		else
			window.location = "cart.php";
			
	}
	return "";
}


/************************************************
* This is used to allow the user to specify only a max
* quantity in the shopping cart.  This allows the
* working cart to be automagically updated once
* they change the quantity value.
************************************************/
function changeQuantity(key,name){
	// Grab values from the document object
	var new_amount = key;
	var name = name;
	
	var new_string = "product[" + name + "]=" + key;
		
	// Set the new cookie
	document.cookie = new_string + ";path=/;expires=" + getExpiry(1) + ";";
	window.location = "cart.php";
	
}

/************************************************
* Sets the location cookie, used for the header of
* each page.
************************************************/
function setLocale(value){
	document.cookie = "locale=" + value + ";" + "path=/;" + "expires=" + getExpiry(1) + ";";
	//document.cookie = "locale" + "=" + value + ";" + "path=/;";
	
	var url = document.location.href;
	// Refresh current page.
	document.location.href = url;
}


/************************************************
* Internet Example.
************************************************/
function toggleDetail(id,number) {
	for (i=1;i<=number;i++) {
		var hold = document.getElementById(id+i);
		if (hold) {
			if (hold.style.display == 'none') {
				// To make tr tags disappear
				// we set display to none, as usual
				// to make them appear again
				// we set style to block for IE
				// but for firefox we use table-row
				try {
				  hold.style.display='table-row';
				} catch(e) {
				  hold.style.display = 'block';
				}
			}else {
				hold.style.display = 'none';
			}
		}
	}
}


/************************************************
* Duplicate user information 
************************************************/
var $flip = 1;
function duplicateData(){
	if($flip){
		document.getElementById('firstnameshipping').value = document.getElementById('firstname').value;
		document.getElementById('lastnameshipping').value = document.getElementById('lastname').value;
		document.getElementById('addressshipping').value = document.getElementById('address').value;
		document.getElementById('cityshipping').value = document.getElementById('city').value;
		document.getElementById('stateshipping').value = document.getElementById('state').value;
		document.getElementById('countryshipping').value = document.getElementById('country_from_db').value;
		document.getElementById('postalcodeshipping').value = document.getElementById('postalcode').value;
		document.getElementById('phoneshipping').value = document.getElementById('phone').value;
		$flip = 0;
	}else{
		document.getElementById('firstnameshipping').value = "";
		document.getElementById('lastnameshipping').value = "";
		document.getElementById('addressshipping').value = "";
		document.getElementById('cityshipping').value = "";
		document.getElementById('stateshipping').value = "";
		document.getElementById('countryshipping').value = "";
		document.getElementById('postalcodeshipping').value = "";
		document.getElementById('phoneshipping').value = "";
		$flip = 1;		
	}
}

function disableElements(){
	var bool;
	if(document.transaction.paymentType.value == 'CC' || document.transaction.paymentType.value == ''){
		bool = false;
	}else{
		bool = true;
	}
	
	document.transaction.creditcard.disabled=bool;
	document.transaction.expirationmonth.disabled=bool;
	document.transaction.expirationyear.disabled=bool;
	document.transaction.cvv2.disabled=bool;
	document.transaction.visa.disabled=bool;
	document.transaction.mastercard.disabled=bool;
}

function isFieldFilledIn(id, message){
	if(document.getElementById(id).value == ""){
		alert(message);
		return false;
	}else
		return true;
}

