// For Consignment System
function setDiscontinued(a, pID) {
	var href = a.href;
	var td = a.parentNode;
	td.innerHTML = '<img src="/images/loading_small.gif" />';
	setTimeout('setDiscontinued_private("'+td.id+'", "'+href+'")',100);
	// setTimeout('setDiscontinued_private("#'+td.id+'", "'+href+'")',50);
}
function setDiscontinued_private(td_id, href) {
	$.ajax({
		async: true,
		type: "GET",
		url: href,
		success: function(msg){
			var td = document.getElementById(td_id);
			td.innerHTML = msg;
			if(msg.match(/_on.gif/)) {
				td.parentNode.className += ' discontinued';
			}else if(msg.match(/_off.gif/)) {
				td.parentNode.className = td.parentNode.className.replace(/\s*discontinued/, '');
			}
		}
	});
}

function newSetDiscontinuedOLD(inp) {
	$.ajax({
		async: true,
		type: "POST",
		url: inp.form.action,
		data: { 'item[flag_discontinued]': (inp.checked ? '1' : '0') },
		success: function(data) { /* alert('Data Loaded: ' + data); */ }
	});
}

function createXMLHttp() {
	var aVersions = [ "MSXML2.XMLHttp.5.0",
		"MSXML2.XMLHttp.4.0","MSXML2.XMLHttp.3.0",
		"MSXML2.XMLHttp","Microsoft.XMLHttp"
	];

	for (var i = 0; i < aVersions.length; i++) {
		try {
			var oXmlHttp = new ActiveXObject(aVersions[i]);
			return oXmlHttp;
		} catch (oError) {
			//Do nothing
		}
	}
	try {
		var oXmlHttp = new XMLHttpRequest();
		return oXmlHttp;
	} catch (oError) {
		//Do nothing
	}
	return false;
}
function newSetDiscontinued(inp) {
	var href = inp.value+'&item[flag_discontinued]='+(inp.checked ? '1' : '0');
	// alert(href);
	inp.blur();
	var oXmlHttp = createXMLHttp();
	oXmlHttp.open("get", href, true);
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			// alert("Got response.");
		}
	};
	oXmlHttp.send(null);
}
function newSetDoOrder(inp) {
	var href = inp.value+'&do_order='+(inp.checked ? '1' : '0');
	inp.blur();
	var oXmlHttp = createXMLHttp();
	oXmlHttp.open("get", href, true);
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			// alert("Got response.");
		}
	};
	oXmlHttp.send(null);
}

function newSetDiscontinued2(a) {
	a.innerHTML = '...';
	var oXmlHttp = createXMLHttp();
	oXmlHttp.open("get", a.href+'&_xhr=1', true);
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4 && oXmlHttp.status == 200) {
			// alert("Got response: " + oXmlHttp.responseText);
			if(oXmlHttp.responseText.match(/true/)){
				a.href = a.href.replace(/=1$/, '=0');
				a.innerHTML = '<span style="color:red;">X</span>';
			} else if(oXmlHttp.responseText.match(/false/)){
				a.href = a.href.replace(/=0$/, '=1');
				a.innerHTML = '<img src="/images/tickbox_off.gif" alt="[ ]" />';
			}
		}
	};
	oXmlHttp.send(null);
}
function newSetDoOrder2(a) {
	a.innerHTML = '...';
	var oXmlHttp = createXMLHttp();
	oXmlHttp.open("get", a.href+'&_xhr=1', true);
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4 && oXmlHttp.status == 200) {
			// alert("Got response: " + oXmlHttp.responseText);
			if(oXmlHttp.responseText.match(/true/)){
				a.href = a.href.replace(/=1$/, '=0');
				a.innerHTML = '<img alt="Y" src="/images/tick2.gif"/>';
			} else if(oXmlHttp.responseText.match(/false/)){
				a.href = a.href.replace(/=0$/, '=1');
				a.innerHTML = '<img src="/images/tickbox_off.gif" alt="[ ]" />';
			}
		}
	};
	oXmlHttp.send(null);
}

if(typeof(setLoading)!='function') {
	function setLoading(a) { }
	function hideLoading() { }
}


function setLineColour(order_id, colour, statusTxt) {
	var tr = $('#tr_'+order_id);
	if(tr && tr.tagName=='TR' && tr.cells) {
		var tr2 = tr;
		var rowspan = tr.cells[0].getAttribute('rowspan');
		while(tr2 && rowspan > 0) {
			// alert(tr2.className + ' : ' + tr2.innerHTML);
			if(tr2.style) {
				tr2.style.background = colour;
				rowspan--;
			}
			tr2 = tr2.nextSibling;
		}
		tr.cells[tr.cells.length - 1].innerHTML = statusTxt;
	}
}

/* Following is for the Cart */
var promocode_checklast = new Array();
function pick_promo_sizecolour(c) {
	if(c.checked){
		var sel = $(c.parentNode).find('select:first');
		if(sel && sel[0]) {
			tb_show('Please Select a Size / Colour', '#TB_inline?height=50&width=400&inlineId=promocode_details_'+c.value, false);
		}
	}
}
function promocode_check(c,max) {
	// Handle Uncheck first.
	if(!c.checked){
		for(i=0;i<promocode_checklast.length;i++) {
			if(promocode_checklast[i]==c){
				promocode_checklast.splice(i,1);
			}
		}
		return 1;
	}

	for(i=promocode_checklast.length+1;i>0;i--){
		promocode_checklast[i] = promocode_checklast[i-1];
	}
	promocode_checklast[0] = c;
	if(promocode_checklast.length > max) promocode_checklast.length = max;

	var frm = c.form;
	var promocode_values = '';
	for(i=0;i<frm.length;i++) {
		if(frm[i].nodeName == 'INPUT' && frm[i].name == 'promocodes[]') {
			if(frm[i].checked){
				var check = false;
				for(k=0;k<promocode_checklast.length;k++){
					if(frm[i] == promocode_checklast[k]) check = true;
				}
				frm[i].checked = check;
				if(check) {
					promocode_values += frm[i].value+',';
				}
			}
		}
	}

	Set_Cookie('ws_promocodes', promocode_values, 1, '/');
	get_all_promo_titles(frm);

	return 1;
}
function get_all_promo_titles(frm) {
	$('#promocode_selected').html('');
	$("input[name='promocodes[]']:checked").each(function() {
		var inp = this;
		var td = $(inp).parents('td:first');
		var a = $(td).find('a:first').clone(true);
		a.html($(td).find('div:first').html());
		var div = $('#promocode_selected').append('<div>1 x </div>');
		$('#promocode_selected div:last').append(a);
		var size = $('#promocode_details_sel_'+inp.value+' option:selected');
		if(size && size.html() && size.html()!='Select ....') {
			$('#promocode_selected div:last').append(' (' + size.html() + ')');
		}
	});
	if($('#promocode_selected').html()=='') {
		$('#promocode_selected').hide();
	} else {
		$('#promocode_selected').show();
	}
	return;
}

function cart_init_cart_bonus() {
	// $('form#cart_form div#cart_bonus input[type=checkbox]').attr('checked', true);
	var num_free_gifts = parseInt($('form#cart_form div#cart_bonus').attr('rel'));
	if(num_free_gifts && num_free_gifts!='' && parseInt(num_free_gifts)>0) {
		num_free_gifts = parseInt(num_free_gifts);
		$('form#cart_form div#cart_bonus input[type=checkbox]').click(function() { pick_promo_sizecolour(this); promocode_check(this, num_free_gifts); });
		var promocode_values = '';
		$('form#cart_form div#cart_bonus input[type=checkbox]:checked:gt('+(num_free_gifts-1)+')').attr('checked', false);
		$('form#cart_form div#cart_bonus input[type=checkbox]:checked').each(function() {
			var num_sizes = $('#promocode_details_sel_'+this.value+' option').length;
			if(num_sizes==0) {
				promocode_values += this.value + ',';
				promocode_checklast[promocode_checklast.length] = this;
			}
		});
		Set_Cookie('ws_promocodes', promocode_values, 1, '/');
		get_all_promo_titles($('form#cart_form div#cart_bonus')[0]);
	}
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) { expires = expires * 1000 * 60 * 60 * 24; }
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
			( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
			( ( path ) ? ";path=" + path : "" ) +
			( ( domain ) ? ";domain=" + domain : "" ) +
			( ( secure ) ? ";secure" : "" );
}
function Get_Cookie( name ) {

	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) { return ''; }
	if ( start == -1 ) return '';
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

// To be used on the Cart - Set up from on ready handler at top.
function cart_open_li() {
	var sel = $('div#cart_paidby ul li :radio:checked');
	if(sel) {
		sel.parents('div#cart_paidby').attr('class', sel.parents('li:first').attr('id'));
		sel.blur();
	}
}
function cart_open_li2() {
	var sel = $('div#cart_paidby ul li :radio:checked');
	if(sel && sel[0] && $('#order_card_name').val()!='' && $('#order_card_number').val()!='' && $('#order_card_id').val()!='') {
		if(!confirm("You have already selected the 'Payment by Credit Card' option by providing your Credit Card details.\n\nIf you now select a different payment option, your Credit Cart details will be removed.")) {
			$('#order_paid_by_id_1').attr('checked',true);
			$('#order_card_name').focus().blur();
			return false;
		}
	}
	$('#order_card_type_id').val('').parents('div.fieldWithErrors').toggleClass('fieldWithErrors');
	$('#order_card_name').val('').parents('div.fieldWithErrors').toggleClass('fieldWithErrors');
	$('#order_card_number').val('').parents('div.fieldWithErrors').toggleClass('fieldWithErrors');
	$('#order_cc_exp_mm').each(function() { this.selectedIndex=0; }).parents('div.fieldWithErrors:first').toggleClass('fieldWithErrors').css({ display: 'inline' });
	$('#order_cc_exp_yy').each(function() { this.selectedIndex=0; });
	$('#order_card_id').val('').parents('div.fieldWithErrors').toggleClass('fieldWithErrors');
	cart_open_li();
}

function confirm_adult(a) {
	if(confirm('Due to Google Adwords Editorial Policy, you are currently viewing the restricted version of our site which does not include adult images.\n\nWould you like to view the full version of our website?')) {
		a.href += (a.href.match(/\?/) ? '&' : '?') + 'set_hide_images=0';
		return true;
	} else {
		return false;
	}
}

function setLargeImgDims(id) {
	var img1 = (id ? $('#large_img_'+id) : $('img.large_img:first'));
	if(img1 && img1[0]) {
		var img2 = new Image();
		$(img2).load(function() {
			var w = this.width; var h = this.height;
			if(w && h) {
				if(w>50 && h>5 && (w!=300 || h!=450)) {
					try {
						img1.css({ 'width': w+'px', 'height': h+'px' });
						/* alert(img1.attr('style')); */
					} catch(e) { /* alert(e); */ }
				}
			}
		});
		var src = img1.attr('src');
		if(src.match('fill.gif')) {
			src = img1.css('backgroundImage');
			src = src.replace('url(','').replace(')','')
		}
		if(src && !src.match('fill.gif')) img2.src = src;
	}
}


function charcode(e) {
	var keynum;
	if(window.event) {  // IE
		keynum = e.keyCode;
	}
	else if(e.which) { // Netscape/Firefox/Opera
		keynum = e.which;
	}
	return keynum;
}

function prod_qty_keypress(inp, code) {
	var a = $(inp).parents('td:first').find('a.add_to_cart');
	if(code==10 || code==13) {
		inp.blur();
		a.click();
		return false;
	} else {
		var product_id = a.attr('rel').replace(/^bp_/,'');
		blinkCartBtn[product_id] = true;
		blinkAddToCart(product_id, 1);
		return true;
	}
}

function blinkDgSubmitButton(toggle) {
	$('a.dg_submit_button').css({ visibility: 'visible' });
	if(toggle && toggle==1) {
		$('a.dg_submit_button').addClass('hover');
		setTimeout('blinkDgSubmitButton(0)', 500);
	} else {
		$('a.dg_submit_button').removeClass('hover');
		setTimeout('blinkDgSubmitButton(1)', 500);
	}
}

