/**
 * Subfooter animation functions
 * @param {Object} product_id
 */
sale_items = new Array();
featured_items = new Array();
best_seller_items = new Array();


$(document).ready(function() {
	
	// start upsell holder cycling
	if (sale_items.length) {
		setInterval("cycle_upsell_item(sale_items,'public_upsell_holder_1')",10000);
	}
	if (best_seller_items.length) {
		setInterval("cycle_upsell_item(best_seller_items,'public_upsell_holder_2')",10000);
	}
	if (featured_items.length) {
		setInterval("cycle_upsell_item(featured_items,'public_upsell_holder_3')",10001);
	}

	/* $("#featured_scroller p").animate({
		left:"-100px"
	},10000);*/

});

function load_upsell_items(name,items) {
	switch(name) {
		case "sale":
			sale_items = items;
			sale_items.index = 1;
		break;
		case "featured":
			featured_items = items;
			featured_items.index = 1;
		break;
		case "best_seller":
			best_seller_items = items;
			best_seller_items.index = 1;		
		}
}

function cycle_upsell_item(items,holder) {

	var item = items[items.index];
	
	product_holder = $("." + holder + " div.product_holder");
	
	product_holder.hide();
	
	product_holder.find("a.product_name").html(item.product_name); 
	product_holder.find("a.product_name").attr('href','?page=shop&pid=' + item.parent_id); 
	product_holder.find("img").attr('src','get_prod_img.php?id=' + item.parent_id + '&width=35'); 
	product_holder.find(".price").html(item.price);
	product_holder.find(".sale_price").html(item.sale_price);
		
	product_holder.slideDown(600);
	
	if (items.index == items.length-1	) {
		items.index = 0;
		
	} else {
		
		items.index++;
		
	}
}

function select_product(product_id)
{
	sku_code=document.add_to_cart.sku.options[document.add_to_cart.sku.selectedIndex].value;
	item_quantity=document.add_to_cart.qty.options[document.add_to_cart.qty.selectedIndex].value;

	document.location.href='?page=shop&pid='+product_id+'&action=add_item&sku='+sku_code+'&qty='+item_quantity;
}

function showHideDel(objToSwitch) {
	if (document.getElementById(objToSwitch).style.display == 'none') document.getElementById(objToSwitch).style.display = 'block';
	else document.getElementById(objToSwitch).style.display = 'none';
	
	if (document.getElementById('delivery_address').style.display == 'block') {
		document.getElementById('delivery_is_different').checked;
	}
}


function showHideDelivery() {
	if (document.getElementById("delAddress").style.display == 'none' && document.getElementById("delivery_address").style.display == 'none') {
		document.getElementById("delAddress").style.display = 'block';
	} else {
		document.getElementById("delAddress").style.display = 'none';
		document.getElementById("delivery_address").style.display = 'none';
	}
}

function checkTicks(objToCheck) {
	if (document.getElementById(objToCheck).checked == "true") {
		document.getElementById(objToCheck).style.display = 'block';
	} else {
		document.getElementById(objToCheck).style.display = 'none';
		document.getElementById(objToCheck).checked = "false";
	}
}

function checkout_form()
{
	b_first_name=document.checkout_details.b_first_name.value;
	b_surname=document.checkout_details.b_surname.value;
	b_address_1=document.checkout_details.b_address_1.value;
	b_city_town=document.checkout_details.b_city_town.value;
	b_postcode_zip=document.checkout_details.b_postcode_zip.value;
	b_email=document.checkout_details.b_email.value;

	errors="";

	if(b_first_name=="" || b_first_name==null || b_first_name=="undefined")
	{
		errors+='\nBilling - You did not enter your first name';
	}

	if(b_surname=="" || b_surname==null || b_surname=="undefined")
	{
		errors+='\nBilling - You did not enter your surname';
	}

	if(b_address_1=="" || b_address_1==null || b_address_1=="undefined")
	{
		errors+='\nBilling - You did not enter the first line of your address';
	}

	if(b_city_town=="" || b_city_town==null || b_city_town=="undefined")
	{
		errors+='\nBilling - You did not enter your city / town';
	}

	if(b_postcode_zip=="" || b_postcode_zip==null || b_postcode_zip=="undefined")
	{
		errors+='\nBilling - You did not enter your postcode / zip';
	}

	if(b_email=="" || b_email==null || b_email=="undefined")
	{
		errors+='\nBilling - You did not enter your e-mail address';
	}

	if(document.checkout_details.delivery_is_different.checked)
	{
		if(!document.checkout_details.use_saved_delivery_address)
		{
			d_first_name=document.checkout_details.d_first_name.value;
			d_surname=document.checkout_details.d_surname.value;
			d_address_1=document.checkout_details.d_address_1.value;
			d_city_town=document.checkout_details.d_city_town.value;
			d_postcode_zip=document.checkout_details.d_postcode_zip.value;

			if(d_first_name=="" || d_first_name==null)
			{
				errors+='\nDelivery - You did not enter your first name';
			}

			if(d_surname=="" || d_surname==null)
			{
				errors+='\nDelivery - You did not enter your surname';
			}

			if(d_address_1=="" || d_address_1==null)
			{
				errors+='\nDelivery - You did not enter the first line of your address';
			}

			if(d_city_town=="" || d_city_town==null)
			{
				errors+='\nDelivery - You did not enter your city / town';
			}

			if(d_postcode_zip=="" || d_postcode_zip==null)
			{
				errors+='\nDelivery - You did not enter your postcode / zip';
			}
		}
		else
		{
			if(!document.checkout_details.use_saved_delivery_address.checked)
			{
				d_first_name=document.checkout_details.d_first_name.value;
				d_surname=document.checkout_details.d_surname.value;
				d_address_1=document.checkout_details.d_address_1.value;
				d_city_town=document.checkout_details.d_city_town.value;
				d_postcode_zip=document.checkout_details.d_postcode_zip.value;

				if(d_first_name=="" || d_first_name==null)
				{
					errors+='\nDelivery - You did not enter your first name';
				}

				if(d_surname=="" || d_surname==null)
				{
					errors+='\nDelivery - You did not enter your surname';
				}

				if(d_address_1=="" || d_address_1==null)
				{
					errors+='\nDelivery - You did not enter the first line of your address';
				}

				if(d_city_town=="" || d_city_town==null)
				{
					errors+='\nDelivery - You did not enter your city / town';
				}

				if(d_postcode_zip=="" || d_postcode_zip==null)
				{
					errors+='\nDelivery - You did not enter your postcode / zip';
				}
			}
		}
	}

	if(!document.checkout_details.terms_yes.checked)
	{
		errors+='\nTerms and Conditions - You did not agree to our Terms and Conditions';
	}

	if (errors)
	{
		alert('Leeds Met:'+errors);

		scroll(0,0);

		return false;
	}
	else
	{
		return true;
	}
}
function popup(id){
	if(document.getElementById(id).style.display == "block"){
		document.getElementById(id).style.display = "none";
	}else{
		document.getElementById(id).style.display = "block";	
	}
	
}
function doomPenguin(el){
		if(el == "|other"){
			document.getElementById("other_reason").style.display = "inline";
		}else{
			document.getElementById("other_reason").style.display = "none";
		}
}
function show_sub_menu(id){
	var catArray = [15,16,19];
	for(var cid=0; cid < catArray.length; cid++){
		if(catArray[cid]==id){
			if(document.getElementById('sub_menu_'+id).style.display=="none"){
			document.getElementById('sub_menu_'+id).style.display = "inline";
			}else{
			document.getElementById('sub_menu_'+catArray[cid]).style.display = "none";	
			}
		}else{
		document.getElementById('sub_menu_'+catArray[cid]).style.display = "none";
		}
	}
}
// ajax
var xmlHttp

function getStock(cid,access)
{ 
//	document.getElementById('selected_sku').value = cid;
	document.getElementById('sku').value = cid;
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	var url="includes/inc.stock_count.php"
	url=url+"?cid="+cid;
	url=url+"&sid="+Math.random();
	url=url+"&access=" + access;
		xmlHttp.onreadystatechange = function()
		{
			if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
			{
				if (access == "faculty") {
					// set new quantity and reset styling
					document.getElementById("full_quantity").innerHTML = xmlHttp.responseText
					document.getElementById('full_quantity').style.color = '#505050';
					document.getElementById("qty").value = 1;

				}
				else {
					
					response = xmlHttp.responseText;
					if(response == 0)
					{
						// show out of stock notice
						document.getElementById("stock_notification").className = "visible";
						document.getElementById("shop_add_to_cart").className = "hidden";
						document.getElementById("qty").className = "hidden";
						document.getElementById("email_child_id").value = cid;
					}
					else
					{	
						// hide notice
						document.getElementById("stock_notification").className = "hidden";
						document.getElementById("qty").className = "";
						document.getElementById("shop_add_to_cart").className = "";

					}
					
					
				}
			}
		}
		
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function stateChanged() 
{ 

}
function showOutOfStock(response)
{
	alert(response);
}


function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}

function applyAll(apply)
{
	var y = 0;
	var elCount = 0;
	
	elements = document.getElementsByTagName("INPUT");
	
	elCount = elements.length;
	
	for(x=0 ; x<elCount ; x++)
	{
		el = elements[x];
		
		if(el.name.indexOf("reason") != -1)
		{
			if(y==0)
			{
				first_reason = el.value;
			}
			else
			{
				if(apply == true)
				{
					el.value = first_reason;				
				}
				else
				{
					el.value = "";
				}
			}
			y++	
		}
	}	
}
function checkStock(el, cartSku, cartQty)
{
	var want = el *1;
	var have = document.getElementById('child_stock').value * 1;
	var sku = document.add_to_cart.sku.value;
	var cartItmQty = 0;
	
	if(typeof cartSku=='undefined' || cartSku==""){
		var subTotal = want;	
	}else{
		// define arrays
		var skuArray = new Array(); 
		skuArray = cartSku.split(",");
		var cartArray = new Array();
		cartArray = cartQty.split(",");
		// get array position, return quantity
		for(i=0; i<skuArray.length; i++){
			if(skuArray[i]==sku){
				cartItmQty = cartArray[i] * 1;
			} 			
		}
		var subTotal = (cartItmQty+want);
	}
		var total = (have-subTotal);
	
	if(total<0)
	{
		//alert('Insufficient avaliable stock');
		document.getElementById('full_quantity').style.color = '#ff0000';
		//document.getElementById('only').style.display = 'inline';
		document.getElementById('shop_add_to_cart').disabled = true;
	}
	else
	{
		document.getElementById('full_quantity').style.color = '#505050';
		//document.getElementById('only').style.display = 'none';
		document.getElementById('shop_add_to_cart').disabled = false;
	}
}
function bodyCheckStock(skuO, qtyO){
	var have = document.getElementById('child_stock').value * 1;
	var want = qtyO * 1;
	var sku = document.add_to_cart.sku.value;
	if(sku == skuO && want<=have){
	return true;
	}else{
	return false;	
	}
}
function change_image_click(image_id, state)
{
//	alert('image id = '+image_id);
//detect browser type
	if(state=="extra")
	{
		document.getElementById('main_image').innerHTML = '<a href="get_prod_imgs.php?id=' + image_id + '&width=600&type=main" class="lightbox"><img width="166"   src="get_prod_imgs.php?id=' + image_id + '&amp;width=166"></a>';
	}
	else
	{
		document.getElementById('main_image').innerHTML = '<a href="get_prod_img.php?id=' + image_id + '&width=600&type=main" class="lightbox"><img  width="166"   src="get_prod_img.php?id=' + image_id + '&amp;width=166&amp;type=main"></a>';	
	}
	
	$('a.lightbox').lightBox();
}
function sendEnquiry()
{
	// get field values
	e_name=document.enquiry.name.value;
	e_department=document.enquiry.department.value;
	e_tel=document.enquiry.tel.value;
	e_email=document.enquiry.email.value;
	e_items=document.enquiry.items.value;
	e_qty=document.enquiry.qty.value;
	e_date=document.enquiry.date.value;
	errors="";
	// validate fields
	if(e_name=="" || e_name==null)
	{
		errors+='\n* - You did not enter your first name';
	}
	if(e_department=="" || e_department==null)
	{
		errors+='\n* - You did not enter your department';
	}
	if(e_tel=="" || e_tel==null)
	{
		errors+='\n* - You did not enter your telephone number';
	}
	if(e_email=="" || e_email==null)
	{
		errors+='\n* - You did not enter your email address';
	}
	if(e_items=="" || e_items==null)
	{
		errors+='\n* - You did not enter your item requirements';
	}
	if(e_qty=="" || e_qty==null)
	{
		errors+='\n* - You did not enter your item quantity';
	}
	if(e_date=="" || e_date==null)
	{
		errors+='\n* - You did not enter your required date';
	}
	// check for errors and alert
	if (errors)
	{
		alert('Leeds Met:'+errors);

		scroll(0,0);

		return false;
	}
	else
	{
		return true;
	}
}
function checkAddStatus(){	
		if(document.getElementById('shop_add_to_cart').disabled == true){
			return false;
		}else{
			return true;
			//document.add_to_cart.submit();	
		}
}
function updateProject(element)
{
	elName = element.name.split("_");
	sku = elName[2];
	project_id = element.value;

	document.location = "index.php?page=shop&option=summary&action=update_project&sku=" + sku + "=sku&project=" + project_id;
}
function check_summary_form()
{
	var errors = "";
	// get all selects
	inputs = document.getElementsByTagName("SELECT");
	
	for(x=0;x<inputs.length;x++)
	{
		current = inputs[x];
		if (current.name.indexOf("project_code") != -1) {
			if (current.value == '-- Select --' || current.value <= 0) {

				split_name = current.name.split("_")
				errors += "Please select a project code for item " + split_name[2] + "\n";

			}
		}
	}
	if(errors.length > 0)
	{
		alert(errors);
		return false;
	}
	else
	{
		return true;
	}
	return false;
}
function getBrowser(domain_url){
	var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();

//document.write('<p class="accent">You\'re using ' + BrowserDetect.browser + ' ' + BrowserDetect.version + ' on ' + BrowserDetect.OS + '!</p>');
document.write('<link href="css/search' + BrowserDetect.browser + '.css" rel="stylesheet" type="text/css"/>');
	if(BrowserDetect.browser=="Firefox"){
		if(BrowserDetect.version == 3)
		{
			document.write('<link rel="stylesheet" href="includes/javascript/gmenuFF3.css" />');
			document.write('<link href="css/search' + BrowserDetect.browser + BrowserDetect.version + '.css" rel="stylesheet" type="text/css"/>');
		}
		else
		{
			document.write('<link rel="stylesheet" href="includes/javascript/gmenuFF.css" />');
		}
	}else{
	document.write('<link rel="stylesheet" href="includes/javascript/gmenu.css" />');	
	}
}
function check_enquiry_form() {
	
    var errors = "";

    form = document.getElementById("enquiry_form");
    
    labels = form.getElementsByTagName("LABEL");
    
	valid = true;
	
    for (x = 0; x < form.elements.length; x++) {
    
        el = form.elements[x];
        
        if (el.className == "required") {
			
            if (el.value.length == 0) {
				
                for (y = 0; y < labels.length; y++) {
                
                    if (labels[y].htmlFor == el.name) {
						
                        label = labels[y].innerHTML;
                        break;
                    }
                    else {
                        label = "";
                    }
                }
				
                errors = errors + label + " is a required field\n";
				valid = false;
            }
        }
    }
	
    if (valid) {
        return true;
    }
    else {
		alert(errors);
        return false;
    }
}
