/* Copyright Kerve Creative */

/* Globals */
var stock_array = new Array();


/* SHOPPING CART FUNCTIONS */
function openWindow(address,winurl,winwidth,winheight) {
	var new_window = window.open(address,winurl,'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=' + winwidth + ',height=' + winheight);
}

function setDefaults() {
	document.product.size.selectedIndex=0;
	document.product.quantity.selectedIndex=0;
	

}

/* change product image */
function alterProductImage(image_name,image_file,stock0,stock1,stock2,stock3,stock4,stock5,index_col,size0,size1,size2,size3) {

	//doThis();
	document.getElementById('colour').value = image_name;
	
	//var sel = document.product.size.rb1;
	
	//document.shop.submit();
	setDefaults();
	
	getData('includes/site_content_modules.php?action=store_temp_session&sess_name=colour&sess_value='+image_name,'')

	// TEST changin text upon selection
	
	
	var active_string = "";
	if(index_col==0) {
		active_string = stock0; // (in,out..)
	} else if(index_col==1) {
		active_string = stock1;
	} else if(index_col==2) {
		active_string = stock2;
	} else if(index_col==3) {
		active_string = stock3;
	} else if(index_col==4) {
		active_string = stock4;
	} else if(index_col==5) {
		active_string = stock5;
	}
	
	// now with active string we wish to change the combo box
	
	// putting active string into array
	active_string = active_string.substring(1,active_string.length-1);
	//alert(active_string);
	var temp = new Array();
	temp = active_string.split(',');

	
	
	
	setStockArray(temp);
	
	

	doThis();
	
		
}

function stockIt(stock_init) {

	
	var active_string = "";
	active_string = stock_init;
	
	// now with active string we wish to change the combo box
	
	// putting active string into array
	active_string = active_string.substring(1,active_string.length-1);
	//alert(active_string);
	
	stock_array = active_string.split(',');

}

function setStockArray(tempArray) {

	stock_array = tempArray;

}

function removeAllOptions(selectbox)
{
var i;
for(i=selectbox.options.length-1;i>=1;i--)
{
selectbox.remove(i);
}
}

function addOption(selectbox,text,value )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}

function saveTempSession(sess_name,sess_value) {
	getData('includes/site_content_modules.php?action=store_temp_session&sess_name='+sess_name+'&sess_value='+sess_value,'')
	getData('includes/site_content_modules.php?action=store_temp_session&sess_name='+sess_name+'&sess_value='+sess_value,'')
}

function doThis() {

	// look for select box chosen item and change depending on its outcome
	var chosen_ix = document.product.size.selectedIndex-1;

	if(stock_array[chosen_ix]=="out") {
		document.getElementById("stock").innerHTML='<FONT style="font-size:15pt;font-weight:bold;color:black;"><br /><br />OUT OF STOCK</FONT>';
		document.product.quantity.disabled = true;

	} else {
		document.getElementById("stock").innerHTML='<FONT style="font-size:15pt;font-weight:bold;"></FONT>';
		document.product.quantity.disabled = false;
	}
	
}

function testIt() {

	alert('TEST');
}


function reloadPage(params,pid) {
	document.location = '/shop_details.php?'+params;
	
}

/* AJAX Connection strings */
//HTTPRequest string for main data
function getData(datasource,divID) {
	//Initial a HTTPXML Connection string
	var XMLHttpRequestObject = null;
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        try {
            XMLHttpRequestObject = new XMLHttpRequest( );
        } catch(e) {
            XMLHttpRequestObject = null;
        }
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        try {
            XMLHttpRequestObject = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
               XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
               XMLHttpRequestObject = null;
            }
        }
    }
    // preserve reference to request object for later
    this.request = XMLHttpRequestObject;
	
	if(XMLHttpRequestObject) {
		XMLHttpRequestObject.open("GET", datasource);
		XMLHttpRequestObject.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		XMLHttpRequestObject.onreadystatechange = function() {
			if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
				/* Debug ALL data being returned from the AJAX request */
				//alert(XMLHttpRequestObject.responseText);
				/* return the data parsed by the AJAX'd php file to the supplied div in 'divID' */
				if(divID != "") {
					document.getElementById(divID).innerHTML = XMLHttpRequestObject.responseText;
				}
			}
		}
		XMLHttpRequestObject.send(null);
	}
}

function findValue(li) {
	if( li == null ) return alert("No match!");

	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];

	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;

	alert("The value you selected was: " + sValue);
}
function selectItem(li) {
	findValue(li);
}
function formatItem(row) {
	return row[0] + " (id: " + row[1] + ")";
}
function lookupLocal(){
	var oSuggest = $("#personName")[0].autocompleter;

	oSuggest.findValue();

	return false;
}


/* FIND button */
function findEntry() {
	var errormessage = 'Please ensure that you have completed the following:\n';
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var checkother_result;
	
	if (
		(document.personalisation_form.personName.value != '') &&
		(document.personalisation_form.personName.value.length >= 2)
	) {
	
		document.personalisation_form.submit();
	} else {
		if((document.personalisation_form.personName.value == '') || (document.personalisation_form.personName.value.length <= 2)) {
			errormessage += "  > Person's name\n";
		}
		
		alert(errormessage);
	}
}

/* Add item to cart */
function cartAdd(checkother) {
	var errormessage = 'Please ensure that you have checked the following:\n';
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var checkother_result;
	var chosen_ix2 = document.product.size.selectedIndex-1;
	
	if(checkother == "1") {
		if (document.product.customisation.value == '') {
			checkother_result = '0';
		} else {
			checkother_result = '1';
		}
	}
	
	if (
		(document.product.quantity.value != '') &&
		(document.product.size.value != '') &&
		(stock_array[chosen_ix2]!="out") &&
		(checkother_result != '0')
	) {
		document.product.submit();
	} else {
		if(document.product.quantity.value == '') {
			errormessage += "  > product quanity\n";
		}
		if(document.product.size.value == '') {
			errormessage += "  > product size\n";
		}
		if(stock_array[chosen_ix2]=="out") {
			errormessage += "  > product chosen is out of stock!\n";
		}
		if(checkother_result == '0') {
			errormessage += "  > your custom message\n";
		}
		alert(errormessage);
	}
}


/* Remove Item */
function cartRemove(item) {
	if(confirm('Are you sure you want to remove this item?')) {
		var unit_item = 'remove_' + item;
		
		document.getElementById(unit_item).submit();
	}
}


/* Update item in cart */
function cartUpdate(item) {
	var filter  = /^([a-zA-Z0\.\-\+\=\_\|\?\/\>\<\,\!\"\£\$\%\^\&\*\(\)])+$/;
	var filter2  = /^(\-[1-9])+$/;
	var filter3  = /^([0-9]+\.[0-9]+)+$/;
	var filter4  = /^(\-[0-9]+\.[0-9]+)+$/;
	var maxquantityitems = 999;
	
	var unit_item = 'quant_' + item;
	
	if(
	   (!filter.test(document.getElementById(unit_item).quantity.value)) && 
	   (!filter2.test(document.getElementById(unit_item).quantity.value)) && 
	   (!filter3.test(document.getElementById(unit_item).quantity.value)) && 
	   (!filter4.test(document.getElementById(unit_item).quantity.value)) && 
	   (document.getElementById(unit_item).quantity.value < maxquantityitems) && 
	   (document.getElementById(unit_item).quantity.value != '')
	) {
		var quant = eval("document." + unit_item + ".quantity.value");
		document.getElementById(unit_item).submit();
		//document.location = '/includes/cart_functions.php?query_string='+query_string+'&action=update&id='+document.getElementById(unit_item).item_session.value+"&quantity="+quant;
	} else {
		if(document.getElementById(unit_item).quantity.value == '0') {
			cartRemove(item);
		} else {
			alert('Invalid entry');
		}
	}
}

/* Confirm order, then send user to ProTX credit card pages */
function validateDelivery() {
	var errormessage = 'Please ensure that you have completed the following:\n';
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	
	if (
		(document.cart_delivery.first_name.value != '') &&
		(document.cart_delivery.email.value != '') &&
		(filter.test(document.cart_delivery.email.value)) &&
		(document.cart_delivery.confirm_email.value != '') &&
		(filter.test(document.cart_delivery.confirm_email.value)) &&
		(document.cart_delivery.address1.value != '') &&
		(document.cart_delivery.postcode.value != '') &&
		(document.cart_delivery.telephone.value != '') &&
		(document.cart_delivery.tandc.checked == true) &&
		(document.cart_delivery.country.value != '') && 
		(document.cart_delivery.email.value == document.cart_delivery.confirm_email.value)
	) {
		document.cart_delivery.submit();
	} else {
		if(document.cart_delivery.email.value == '') {
			errormessage += "  > Email address\n";
		} else {
			if((filter.test(document.cart_delivery.email.value)) == false) {
				errormessage += "  > You must enter a valid email address\n";
			}
		}
		if(document.cart_delivery.confirm_email.value == '') {
			errormessage += "  > Email confirmation address\n";
		} else {
			if((filter.test(document.cart_delivery.confirm_email.value)) == false) {
				errormessage += "  > You must enter a valid email confirmation address\n";
			}
		}
		if(document.cart_delivery.email.value != document.cart_delivery.confirm_email.value) {
			errormessage += "  > Email confirmation must match your primary email address\n";
		}
		if(document.cart_delivery.first_name.value == '') {
			errormessage += "  > Your name\n";
		}
		if(document.cart_delivery.address1.value == '') {
			errormessage += "  > Your address\n";
		}
		if(document.cart_delivery.postcode.value == '') {
			errormessage += "  > Your postcode\n";
		}
		if(document.cart_delivery.country.value == '') {
			errormessage += "  > Your country\n";
		}
		if(document.cart_delivery.telephone.value == '') {
			errormessage += "  > Your telephone number\n";
		}
		if(document.cart_delivery.tandc.checked == false) {
			errormessage += "  > Read and understood the terms and conditions\n";
		}
		alert(errormessage);
	}
}


/* Place order */
function processCart() {
	document.cart_confirm.submit();
}
