//* Stock Checking *//
function checkstock(){if(typeof stockData!="undefined"){var optionObjCount=0;var optionObjSelecetedCount=0;if(document.le_form.color){optionObjCount++;thisselection=document.le_form.color.selectedIndex;if(document.le_form.color[thisselection].value!=""){selectedcolor=document.le_form.color[thisselection].value;optionObjSelecetedCount++}}if(document.le_form.specialoptions){optionObjCount++;thisselection=document.le_form.specialoptions.selectedIndex;if(document.le_form.specialoptions[thisselection].value!=""){selectedoptionTemp=document.le_form.specialoptions[thisselection].value;selectedoptionArray=selectedoptionTemp.split("|");selectedoption=selectedoptionArray[0];optionObjSelecetedCount++}}if(document.le_form.size){optionObjCount++;thisselection=document.le_form.size.selectedIndex;if(document.le_form.size[thisselection].value!=""){selectedsizeTemp=document.le_form.size[thisselection].value;selectedsizeArray=selectedsizeTemp.split("|");selectedsize=selectedsizeArray[0];optionObjSelecetedCount++}}if(optionObjCount==optionObjSelecetedCount){for(sd=0;sd<stockData.length;sd++){if(document.le_form&&document.le_form.addtocart){document.le_form.addtocart.style.display=""}if(document.getElementById("stock-warning")){document.getElementById("stock-warning").style.display="none"}if(typeof selectedsize=="undefined"||stockData[sd].size==selectedsize){if(typeof selectedoption=="undefined"||stockData[sd].specialoptions==selectedoption){if(typeof selectedcolor=="undefined"||stockData[sd].color==selectedcolor){if(stockData[sd].stocklevel==0){if(document.le_form&&document.le_form.addtocart){document.le_form.addtocart.style.display="none"}if(document.getElementById("stock-warning")){document.getElementById("stock-warning").style.display=""}break}}}}}}}};

function setCookie(c_name,value,expiredays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";path=/;expires="+exdate.toUTCString());
}

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 "";
}

function checkCookie() {
    isInitial=getCookie('initial');
    if (isInitial!=null && isInitial!="") { return false; }
    else {
      setCookie('initial','false',90);
      var promoBox = $('<div style="display:hidden"></div>').appendTo('body');
      promoBox.load('http://www.twoseasons.co.uk/layouts/signupoffer.htm',
	    function (responseText,textStatus,XMLHttpRequest) {
		promoBox.dialog({width: 600, height: 600, title: "WIN a Signed Element Deck", modal: true,
		    buttons: {
			'Send me Offers' : function() {
			    var trigger = false;
			    $('#mailerform input[type="text"]').each(function() {
				var currEl = $(this).val();
				if (currEl == null || currEl == '') {trigger = true;}
			    })
			    if (trigger == true) { alert('Please fill in all fields'); return false; }
			    var mail = $('#mailerEmail').val();
			    if (mail.indexOf('.') >2 && mail.indexOf('@')>0) {
				$('#mailerform').submit();
			    } else { alert('Please check your email address') }
			},
			Cancel: function() {
			    $(this).dialog('close');
			}
		    }
		});
	    }
	);
    }
}

var eMsg = "";

function isValidEmail() {
    var email = $("#review-email-input").val();
    if (email.indexOf('@') >= 0) {return true}
    else {
	eMsg = eMsg + "<" + "li>Please check your email address<" + "/li>";
	return false
    }
};
function isValidReview() {
    var iChars = "!@#$%^&*()+=-[]\\;,./{}|:<>?";
    if ($("#review-comments-input").val().length != 0) {
	for (var i = 0; i < $("#review-comments-input").val().length; i++) {
	    if (iChars.indexOf($("#review-comments-input").val().charAt(i)) != -1) {
		eMsg = eMsg + "<" + "li>Review (No e-mail/web addresses, or html tags are permitted)<" + "/li>";
		return false;
	    } else {
		return true;
	    };
	}
    } else {
	eMsg = eMsg + "<" + "li>Please enter a review<" + "/li>";
	return false;
    }
};
function isValidName() {
    if ($("#review-name-input").val().length > 1) {return true}
    else {
	eMsg = eMsg + "<" + "li>Please enter your name<" + "/li>";
	return false;
    }
};

function buyFormSubmitGoogleAnalytics(obj) {
    if (typeof(__utmLinkPost) != "undefined")
	{ __utmLinkPost(obj); }
    else if (typeof(pageTracker._link) != "undefined")
	{ pageTracker._linkByPost(obj); }
    return true;
};

function jsonResponse(response) {
    var s = "";
    s = s + response.tipName +": "+ response.tipContent;
    var newEl = document.createElement('div');
    $(newEl).text(response.tipContent);
    newEl.setAttribute('id',response.tipId);
    $(newEl).appendTo('body').dialog({title: response.tipName, modal: true, zIndex: 10510});
};

function dgExt(thisURL,dgTitle,dgHeight,dgWidth) {
    if (thisURL) {
	$.get(thisURL,function(data) {
	    var newEl = document.createElement('div')
	    $(newEl).css('display','none')
	    $('body').append(newEl);
	    $(newEl).html(data).dialog({height: dgHeight, width: dgWidth, modal: true, title: dgTitle});
	});
    };
};

$(document).ready(function(){
    if (typeof(stockData) != 'undefined') {
	$("#product_sizes_dropdown option").each(function() {
	    var frmSize = $(this).attr("value");
	    for (x in stockData) {
		if (stockData[x].size == frmSize) {
		    var stkMsg = "";		    
		    if (parseFloat(stockData[x].stocklevel) < 5) {
			if (stockData[x].stocklevel <= 0) {stkMsg = " (sold out)"}
			else {stkMsg = " (low stock)"};
		    };
		    $(this).replaceWith("<option value='"+frmSize+"'>"+frmSize+stkMsg+"</option>");
		}
	    }
	});
    };
    $('#prodMainImgA').click(function () {$('#prodMainImgA').colorbox({open: 'true'});});
    
    $('#sizeGuide').click(function () {
	$('#sizeGuide').colorbox({'width':'970px','height':'80%','iframe':'true'});
    }).button({
    icons: { primary: "ui-icon-extlink" },
    text: "Size Guide"
    });
    
    $('#prodReviews').tabs({fx: {opacity: 'toggle'}, selected: 1 });    
    
    $('button[name="addtocart"]').empty().text("Add to Basket").button({
	icons: { primary: "ui-icon-cart" },
	text: "Add to Basket"
    }).click(function(event) {
	var sizeEl = $("#product_sizes_dropdown");
	var optEl = $("select[name='specialoptions']");
	
	if (sizeEl.length > 0 && $(sizeEl).val() == "") {
	    event.preventDefault();
	    alert("Please select a size");
	};
	if (optEl.length > 0 && $(optEl).val() == "") {
	    event.preventDefault();
	    alert("Please choose an option");
	};
    });
    
    $('.videoLink').colorbox({iframe:true,innerWidth:425,innerHeight:344});
    
    $('.featureListItem').click(function() {
	var currTip = $(this).attr('tip');
	if (document.getElementById(currTip)) {
	    $('#'+currTip).dialog("open");
	} else {
	    var srcUri = 'http://blog.twoseasons.co.uk/tooltips/tip.php?q='+currTip;
	    var toAppend = document.createElement('script');
	    toAppend.setAttribute('src',srcUri);
	    $('head').append(toAppend);
	};
    })
});

$(window).load(function() {
    if(location.hostname.indexOf("twoseasons.co.uk") && ($(window).width() >= 1100)) {
	$('#socialBar').show('slide');
    };
});
