function swatchChk(swatch) {
	if($('#'+swatch).attr("checked")) {
		$('#'+swatch).attr('checked', false);
	} else {
		$('#'+swatch).attr('checked', true);
	}
}

time = 3000;
function hideDelay(obj, time) {
//	typeof(time) == 'undefined' ? time = 3000 : null;
	$.timer(time, function (timer) {
		$('#'+obj).slideUp('slow');
		timer.stop();
	});
}

function swapImage(selector, obj, imgType) {
	if (selector == "on") {
		$('#'+obj).attr("src", '/images/'+obj+'_on.'+imgType);
	} else {
		$('#'+obj).attr("src", '/images/'+obj+'.'+imgType);
	}
}

function popUp(name, strURL, strType, strTop, strLeft, strWidth, strHeight) {
	var strOptions = "";
	if (strType == "console") strOptions = "resizable,scrollbars,top="+strTop+",left="+strLeft+",height="+strHeight+",width="+strWidth;
	if (strType == "fixed") strOptions = "scrollbars=no,status,top="+strTop+",left="+strLeft+",height="+strHeight+",width="+strWidth;
	if (strType == "elastic") strOptions = "toolbar,menubar,scrollbars,resizable,location,top="+strTop+",left="+strLeft+",height="+strHeight+",width="+strWidth;
	var newWin = window.open(strURL, name, strOptions);
	newWin.focus();
}

function colorSwa(selector, swatch, fabric, color, bigImage) {
//	console.log(selector, swatch, fabric, color, bigImage);
	if (selector == "on") {
		$(swatch).css('border', '#707277 solid 1px');
		//$('#fabric').html('&nbsp;'+fabric);
		$('#color').html('&nbsp;'+color);
		$('#image_holder_image').attr('src', '/colours/big/'+bigImage);
//		$('#image_holder_image').attr('src', '/products/'+bigImage);
		/*
		$('#image_holder').fadeOut('fast',
			function(){
				$('#image_holder_image').attr('src', '/colours/'+bigImage+'_b.jpg')
			}
		).fadeIn('fast');
		*/
//		$('#image_holder').fadeOut('fast', function(){$('#image_holder').html('<img src="/colours/'+bigImage+'_b.jpg" alt="" />', function(){$('#image_holder').fadeIn('fast')})});
		//alert($('#image_holder_image').attr('src'));
	} else {
		$(swatch).css('border', '#fff solid 1px');
		//$('#fabric').html('&nbsp;');
		$('#color').html('&nbsp;');
		$('#image_holder_image').attr('src', '/products/'+bigImage)
		/*
		$('#image_holder').fadeOut('fast',
			function(){
				$('#image_holder_image').attr('src', '/products/'+bigImage)
			}
		).fadeIn('fast');
		*/
	}
}

function add_newsletter(){
	if ($('#news_fname').val() == '' || $('#news_lname').val() == '') {
		$('#mailing_list').html('<span style="color:#DF1B1B;">Please type in your full name.</span>');
	} else {
		var params = {
			fname: $('#news_fname').val(),
			lname: $('#news_lname').val(),
			email: $('#news_email').val()
		}
		
		$.ajax({
			type: "POST",
			url: "/ajax.php?act=newsletter",
			data: params,
			success: function(msg) {
				var resp = msg.getElementsByTagName("data")[0].firstChild.data;
				if (resp == 0) {
					$('#mailing_list').html('<span style="color:#DF1B1B;">NOT AN EMAIL</span>');
				} else {
					$('#content_right').height($('#content_left').height());
					$('#mailing_list_success').html(resp);
					$('#page_header').attr('src', '/images/headers/7a.jpg');
				}
			}
		});
	}
}
function remove_newsletter() {
	$.ajax({
		type: "POST",
		url: "/ajax.php?act=rem_newsletter",
		data: "email="+$('#un_news_email').val(),
		success: function(msg) {
			var resp = msg.getElementsByTagName("data")[0].firstChild.data;
			if (resp == 0) {
				$('#rem_mailing').html('<span style="color:#DF1B1B;">NOT AN EMAIL</span>');
			} else {
				$('#mailing_list_success').html(resp);
			}
		}
	});
}

function itemValid(items) {
	var comm = '';
	var goodPairs = new Array();
	
	for (var i=0; i<items; i++) {
		if ($('#quantity_' + i).val() != '0' && !isNaN($('#quantity_' + i).val()) && $('#basket_colour_' + i).val() == '0') {
			comm += '\nPlease choose a colour.';
		} else if (($('#quantity_' + i).val() == '0' || isNaN($('#quantity_' + i).val())) && $('#basket_colour_' + i).val() != '0' && $('#jsignore_'+i).val() != '1') {
			comm += '\nPlease order at least one item.';
		} else if ($('#quantity_'+i).val() != '0' && !isNaN($('#quantity_'+i).val()) && $('#basket_colour_'+i).val() != '0') {
			goodPairs[i] = 'yes';
		}  
	}

	if ($('#quantity_bale').val() != '0' && !isNaN($('#quantity_bale').val()) && $('#basket_colour_bale').val() != '0') {
		goodPairs[i] = 'yes';
	}
	
	if (comm != '') {
		alert(comm);
		return false;
	} else if(goodPairs.length >= 1) {
		return true;
	} else {
		if (comm == '') {
			alert('Please order at least one item.');
		}
		return false;
	}
}

function shipp_add() {
	if ($('#shipbill').attr('checked') == true) {
		$('#s_fname').val($('#b_fname').val());
		$('#s_lname').val($('#b_lname').val());
		$('#s_address').val($('#b_address').val());
		$('#s_address2').val($('#b_address2').val());
		$('#s_city').val($('#b_city').val());
		$('#s_country').val($('#b_country').val());
		$('#s_postcode').val($('#b_postcode').val());
		$('#s_phone').val($('#b_phone').val());
		$('#s_mobile').val($('#b_mobile').val());
		$('#s_email').val($('#b_email').val());
		$('#s_email2').val($('#b_email2').val());
	} else {
		$('#s_fname').val('');
		$('#s_lname').val('');
		$('#s_address').val('');
		$('#s_address2').val('');
		$('#s_city').val('');
		$('#s_country').val('');
		$('#s_postcode').val('');
		$('#s_phone').val('');
		$('#s_mobile').val('');
		$('#s_email').val('');
		$('#s_email2').val('');
	}
}

function basketValid() {
	var comm = '';
	if ($('#sel_del').val() == '') {
		comm = 'Please choose delivery type.';
	}
	
	if (comm != '') {
		alert(comm);
		return false;
	}
	return true;
}

function checkoutValid() {
	var comm = '';
	var comm2 = '';
	
	//billing
	if ($('#b_fname').val() == '') {
		comm += '\nFirst name';
	}
	if ($('#b_lname').val() == '') {
		comm += '\nLast name';
	}
	if ($('#b_address').val() == '') {
		comm += '\nYour address';
	}
	if ($('#b_country').val() == '') {
		comm += '\nCountry';
	}
	if ($('#b_postcode').val() == '') {
		comm += '\nPost code/ZIP';
	}
	if ($('#b_phone').val() == '') {
		comm += '\nPhone';
	}
	/*
	if ($('#b_mobile').val() == '') {
		comm += '\nMobile';
	}
	*/
	if ($('#b_email').val() == '' || $('#b_email').val() != $('#b_email2').val()) {
		comm += '\nCorrect email';
	}
	if (comm != '') {
		comm = '\n\nBILLING'+comm;
	}
	
	if ($('#shipbill').attr('checked') != true) {
		//shippping
		if ($('#s_fname').val() == '') {
			comm2 += '\nFirst name';
		}
		if ($('#s_lname').val() == '') {
			comm2 += '\nLast name';
		}
		if ($('#s_address').val() == '') {
			comm2 += '\nYour address';
		}
		if ($('#s_country').val() == '') {
			comm2 += '\nCountry';
		}
		if ($('#s_postcode').val() == '') {
			comm2 += '\nPost code/ZIP';
		}
		if ($('#s_email').val() == '' || $('#s_email').val() != $('#s_email2').val()) {
			comm2 += '\nCorrect email';
		}
		if (comm2 != '') {
			comm2 = '\n\nSHIPPING'+comm2;
		}
	}
	
	if (comm != '' || comm2 != '') {
		alert('Please fill in following fields:'+comm+comm2);
		return false;
	}
	return true;
}

function menu_anim(menu_id) {
	if ($('#'+menu_id).css('display') == 'none') {
		$('#'+menu_id).show('slow');
	} else {
		$('#'+menu_id).hide('slow');
	}
}

function cardValid() {
	var comm = '';
	if ($('#cardType').val() == '') {
		comm += '\nPlease specify your credit card type.';
	}
	if ($('#cardName').val() == '') {
		comm += '\nPlease enter your name as it appears on your card.';
	}
	if ($('#cardNr').val() == '') {
		comm += '\nPlease enter your credit card number.';
	}
	if ($('#expM').val() == '' || $('#expY').val() == '' || $('#expM').val() == 'MM' || $('#expY').val() == 'YY') {
		comm += '\nPlease specify your credit card expiration date.';
	}
	if (($('#staM').val() == '' || $('#staY').val() == '' || $('#staM').val() == 'MM' || $('#staY').val() == 'YY') && $('#issue').val() == '') {
		comm += '\nPlease specify your credit card start date or issue number.';
	}
	if ($('#cv2').val() == '') {
		comm += '\nPlease specify your credit card verification number (CV2)';
	}
	
	if (comm != '') {
		alert(comm);
		return false;
	}
	return true;
}

function cardtype() {
	if ($('#cardType').val() == 'AMEX') {
		$('#secCode_1').slideUp('slow');
		$('#secCode_2').slideDown('slow');
	} else if ($('#cardType').val() == '') {
		$('#secCode_1').slideUp('slow');
		$('#secCode_2').slideUp('slow');
	} else {
		$('#secCode_1').slideDown('slow');
		$('#secCode_2').slideUp('slow');
	}
}

/*
function addHandler() {
	if ($('#category').val() == 'new') {
		$('#newCategory').css('display', 'block');
	} else {
		$('#newCategory').css('display','none');
	}
	if ($('#category').val() == 0) {
		$('#submit').attr('disabled', 'true');
	} else {
		$('#submit').removeAttr('disabled');
	}
}
*/