// JavaScript Document



function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}


var addEvent = function(elem, type, eventHandle) { 
    if (elem == null || elem == undefined) return; 
    if ( elem.addEventListener ) { 
        elem.addEventListener( type, eventHandle, false ); 
    } else if ( elem.attachEvent ) { 
        elem.attachEvent( "on" + type, eventHandle ); 
    } 
};

addEvent(window, "resize", function() { set_backgrounds()} ); 


function set_backgrounds(){ 
	var windowHeight; 
	
	if (typeof window.innerWidth != 'undefined'){ 
		windowHeight = window.innerHeight; 
	}  else if (typeof document.documentElement != 'undefined'   && typeof document.documentElement.clientWidth != 'undefined'  && document.documentElement.clientWidth != 0)  { 
		windowHeight = document.documentElement.clientHeight; 
	} else  { 
		windowHeight = document.getElementsByTagName('body')[0].clientHeight; 
	} 
	
	tHeight = document.getElementById('outside').clientHeight 
	//alert(tHeight + " " + windowHeight)
	newHeight = (windowHeight - document.getElementById("bottom_bg").offsetTop  )
	if(newHeight < tHeight){
		newHeight = tHeight
	}
	document.getElementById("bottom_bg").style.height = newHeight + "px"; 

 
}

function adjust_category_bg(){
	outsideHeight = document.getElementById("outside").offsetHeight;
	brandsHeight = document.getElementById("brands_icons_wrapper").offsetHeight;
	newHeight = document.getElementById("thumbs_wrapper").offsetHeight;
	if(brandsHeight > newHeight){
		newHeight = brandsHeight
	} 
	newHeight = newHeight + 16;
	document.getElementById("category_thumbs_bg").style.height = newHeight + "px"; 
	 
	 document.getElementById("footer_positioner").style.top = (newHeight + 390) + "px"; 
	 document.getElementById("bottom_bg").style.height = (newHeight +  outsideHeight - 290)+ "px"; 
	 set_backgrounds()
}

function adjust_brand_bg(){
	outsideHeight = document.getElementById("outside").offsetHeight;
	newHeight = document.getElementById("thumbs_wrapper").offsetHeight;
	textHeight = document.getElementById("brand_text").offsetHeight;
	 if(textHeight > newHeight){
		newHeight = textHeight
	}
	newHeight = newHeight + 16;
	document.getElementById("brand_thumbs_bg").style.height = newHeight + "px"; 
	 
	 document.getElementById("footer_positioner").style.top = (newHeight + 390) + "px"; 
	 document.getElementById("bottom_bg").style.height = (newHeight +  outsideHeight - 290)+ "px"; 
	  set_backgrounds()
}

function adjust_product_bg(){
	outsideHeight = document.getElementById("outside").offsetHeight;
	 
	newHeight = document.getElementById("thumb_wrapper").offsetHeight;
	textHeight = document.getElementById("product_text").offsetHeight;
	 if(textHeight > newHeight){
		newHeight = textHeight
	} 
	document.getElementById("brand_thumbs_bg").style.height = newHeight + "px"; 
	 
	 document.getElementById("footer_positioner").style.top = (newHeight + 180) + "px"; 
	 document.getElementById("bottom_bg").style.height = (newHeight +  outsideHeight - 490)+ "px"; 
	  set_backgrounds()
}


function adjust_whats_hot_bg(){
	outsideHeight = document.getElementById("outside").offsetHeight;
	newHeight = document.getElementById("thumbs_wrapper").offsetHeight;
 
	newHeight = newHeight + 16;
	document.getElementById("whats_hot_thumbs_bg").style.height = newHeight + "px"; 
	 
	 document.getElementById("footer_positioner").style.top = (newHeight + 180) + "px"; 
	 document.getElementById("bottom_bg").style.height = (newHeight +  outsideHeight - 290)+ "px"; 
	  set_backgrounds()
}



function adjust_content_bg(){
	outsideHeight = document.getElementById("outside").offsetHeight;
	newHeight = document.getElementById("content_wrapper").offsetHeight;
 
	newHeight = newHeight + 16;
	document.getElementById("content_bg").style.height = newHeight + "px"; 
	 
	 document.getElementById("footer_positioner").style.top = (newHeight + 180) + "px"; 
	 document.getElementById("bottom_bg").style.height = (newHeight +  outsideHeight - 290)+ "px"; 
	  set_backgrounds()
}

function check_email(aVal){
	var tValidatedOK = true
	if(aVal != 0){
		var error = false
		email = aVal
		var at="@"
		var dot="."
		var lat=email.indexOf(at)
		var lstr=email.length
		var ldot=email.indexOf(dot)
		if (email.indexOf(at)==-1){
		   error = true;
		}

		if (email.indexOf(at)==-1 || email.indexOf(at)==0 || email.indexOf(at)==lstr){
		   error = true;
		}

		if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 || email.indexOf(dot)==lstr){
		   error = true;
		}

		 if (email.indexOf(at,(lat+1))!=-1){
		   error = true;
		 }

		 if (email.substring(lat-1,lat)==dot || email.substring(lat+1,lat+2)==dot){
		   error = true;
		 }

		 if (email.indexOf(dot,(lat+2))==-1){
		   error = true;
		 }
		
		 if (email.indexOf(" ")!=-1){
		   error = true;
		 }

		if(error){
			tValidatedOK = false;
		} 
	}
	
	return tValidatedOK;
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}


function validate_subscribe(){
	var tValidatedOK = true
	 
	
	tForm = document.getElementById("frmSubscribe")
 
	if (check_email(tForm.email.value) == false || trim(tForm.email.value) == ""){
		alert("Please enter a valid Email Address")
		tValidatedOK = false
	} else {
		tForm.submit()
	}
	
}



function validate_enquiry(){
	var tValidatedOK = true
	var tMessage = "The following field(s) are required:\n"
	
	tForm = document.getElementById("frmEnquire")

	 
	if (tForm.fullname.value  == ""){
		tMessage += "          You must enter your Name\n"
		tValidatedOK = false
	} 
	
	if (tForm.email.value  == ""){
		tMessage += "          You must enter your Email Address\n"
		tValidatedOK = false
	}
			
	if(tValidatedOK == false){
		alert(tMessage)
	} else {
	
		tForm.submit()
	}
	
}


function validateContactForm(){
	var tValidatedOK = true
	var tMessage = "The following field(s) are required:\n"
	
	tForm = document.contact_form;
	
	if (tForm.yourname.value  == "" || tForm.yourname.value  == "Your name*"){
		tMessage += "          Your name\n"
		tValidatedOK = false
	}
	
	if (check_email(tForm.email.value) == false || trim(tForm.email.value) == ""){
		tMessage += "          Your email address\n"
		tValidatedOK = false
	}
	
	if(tValidatedOK == false){
		alert(tMessage)
	} else {
		document.contact_form.submit()
	}
}

function clear_field(aField, aDefault){
	if(aField.value == aDefault){
		aField.value = ""
	}
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



function showhide(id){ 
	if (document.getElementById){ 
		obj = document.getElementById(id); 
		if (obj.style.display == "none"){ 
			obj.style.display = ""; 
		} else { 
			obj.style.display = "none"; 
		} 
	} 
} 

function add_to_cart(aId){
	
	var tValidatedOK = true
	var tMessage = "The following field(s) are required:\n"
		
	tForm = document.getElementById("buy_form_" + aId)
	
	if(tForm.has_sizes.value == 1){
		if (tForm.size_id.value  == ""){
			tMessage += "          Size of Item\n"
			tValidatedOK = false
		}
	}
	if(tForm.has_colours.value == 1){
		if (tForm.colour_id.value  == ""){
			tMessage += "          Colour of Item\n"
			tValidatedOK = false
		} 
	}
	
	if(tValidatedOK == false){
		alert(tMessage)
	} else {
		tForm.submit()
	}
	
}


function add_to_special_order_cart(aId){
	
	var tValidatedOK = true
	var tMessage = "The following field(s) are required:\n"
		
	tForm = document.getElementById("buy_form_" + aId)
	
	if(tForm.has_sizes.value == 1){
		if (tForm.size_id.value  == ""){
			tMessage += "          Size of Item\n"
			tValidatedOK = false
		}
	}
	if(tForm.has_colours.value == 1){
		if (tForm.colour_id.value  == ""){
			tMessage += "          Colour of Item\n"
			tValidatedOK = false
		} 
	}
	
	if(tValidatedOK == false){
		alert(tMessage)
	} else {
		tForm.submit()
	}
	
}



function enlarge(aFile){
	win = MM_openBrWindow('enlarge.php?file=' + aFile,'view','width=650,height=650')
	win.focus()
	
}



function view_sizes(aBrand){
	win = MM_openBrWindow('view_sizes.php?bid=' + aBrand,'view','width=683,height=204')
	win.focus()
	
}


function clear_product(aID){
	if(confirm("Are you sure you wish to delete this item?")){
		document.getElementById("qty_" + aID).value = 0
		document.frmOrder.submit()
	}
}

function cart_update(aMode){
	document.frmOrder.mode.value = aMode
	document.frmOrder.submit()
}
 
Number.prototype.toDecimals=function(n){
	n=(isNaN(n))? 2: n;
	var  nT=Math.pow(10,n);
	function pad(s){
			s=s||'.';
			return (s.length>n)?  s: pad(s+'0');
	}
	return (isNaN(this))?
		this:
		(new String(
			Math.round(this*nT)/nT
		)).replace(/(\.\d*)?$/,pad);
}

function validate_delivery_details(){
	var tValidatedOK = true
	var tMessage = "The following field(s) are required:\n"
	
	tForm = document.frmDeliveryDetails
	
	if (tForm.firstname.value  == ""){
		tMessage += "          Your First Name\n"
		tValidatedOK = false
	}
	
	if (tForm.lastname.value  == ""){
		tMessage += "          Your Last Name\n"
		tValidatedOK = false
	}
	
	if (tForm.email.value  == ""){
		tMessage += "          Your Email Address\n"
		tValidatedOK = false
	}
	
	if (tForm.phone.value  == ""){
		tMessage += "          Your Phone Number\n"
		tValidatedOK = false
	}
	
	if (tForm.suburb.value  == ""){
		tMessage += "          Your Suburb\n"
		tValidatedOK = false
	}
	
	if (tForm.state.value  == ""){
		tMessage += "          Your State\n"
		tValidatedOK = false
	}
	
	if (tForm.postcode.value  == ""){
		tMessage += "          Your Postcode\n"
		tValidatedOK = false
	}
			
	if(tValidatedOK == false){
		alert(tMessage)
	} else {
	
		tForm.submit()
	}
	
}

function validate_card_details(){
	var tValidatedOK = true
	var tMessage = "The following field(s) are required:\n"
		
	tForm = document.frmCardDetails
	
	if (tForm.cardtype.value  == ""){
		tMessage += "          The Credit Card Type\n"
		tValidatedOK = false
	}
	if (tForm.cardnumber.value  == ""){
		tMessage += "          Your Credit Card Number\n"
		tValidatedOK = false
	}
	if (tForm.cardname.value  == ""){
		tMessage += "          The Name on your Credit Card\n"
		tValidatedOK = false
	}
	if (tForm.cvn.value  == ""){
		tMessage += "          Your CVN Number of your Credit Card\n"
		tValidatedOK = false
	}

	
	if(tValidatedOK == false){
		alert(tMessage)
	} else {
		showhide("divProcessButton")
		showhide("divProcessing")
		
		tForm.submit()
	}
	
}

function validate_special_order_card_details(){
	var tValidatedOK = true
	var tMessage = "The following field(s) are required:\n"
		
	tForm = document.frmCardDetails
	
	if (tForm.cardtype.value  == ""){
		tMessage += "          The Credit Card Type\n"
		tValidatedOK = false
	}
	if (tForm.cardnumber.value  == ""){
		tMessage += "          Your Credit Card Number\n"
		tValidatedOK = false
	}
	if (tForm.cardname.value  == ""){
		tMessage += "          The Name on your Credit Card\n"
		tValidatedOK = false
	}
	if (tForm.cvn.value  == ""){
		tMessage += "          Your CVN Number of your Credit Card\n"
		tValidatedOK = false
	}
	 
	if (tForm.terms.checked  == false){
		tMessage += "          You must accept the terms and conditions\n"
		tValidatedOK = false
	}

	
	if(tValidatedOK == false){
		alert(tMessage)
	} else {
		showhide("divProcessButton")
		showhide("divProcessing")
		
		tForm.submit()
	}
	
}
 
function validate_details_confirmed(){	
	tForm = document.frmConfirm
	tForm.submit()	
}



function validate_out_of_stock(aId){
	var tValidatedOK = true
	var tMessage = ""
		
	tForm = document.getElementById("frmOutOfStock_" + aId)

	
	if (tForm.email.value  == ""){
		tMessage += "You must enter your Email Address\n"
		tValidatedOK = false
	}
			
	if(tValidatedOK == false){
		alert(tMessage)
	} else {
	
		tForm.submit()
	}
	
}

function validate_out_of_stock_size(aId){
	var tValidatedOK = true
	var tMessage = "The following field(s) are required:\n"
	
	tForm = document.getElementById("frmOutOfStock_" + aId)

	 
	if (tForm.size_id.value  == ""){
		tMessage += "          You must enter your Size\n"
		tValidatedOK = false
	}
	
	if (tForm.email.value  == ""){
		tMessage += "          You must enter your Email Address\n"
		tValidatedOK = false
	}
			
	if(tValidatedOK == false){
		alert(tMessage)
	} else {
	
		tForm.submit()
	}
	
}

function validate_out_of_stock_colour(aId){
	var tValidatedOK = true
	var tMessage = "The following field(s) are required:\n"
	
	tForm = document.getElementById("frmOutOfStock_" + aId)

	 
	if (tForm.colour_id.value  == ""){
		tMessage += "          You must enter your Colour\n"
		tValidatedOK = false
	}
	
	if (tForm.email.value  == ""){
		tMessage += "          You must enter your Email Address\n"
		tValidatedOK = false
	}
			
	if(tValidatedOK == false){
		alert(tMessage)
	} else {
	
		tForm.submit()
	}
	
}

function show_special_order_info(aDiv){
	document.getElementById(aDiv).style.display = "block"
 	
}
