var FEE_SHP_NORMAL = 1.5;
var FEE_SHP_REG = 5;
var FEE_SHP_COURIER = 10;

var FEE_GIFTCARD = 3.5;
var FEE_GIFTWRAP = 5;

var txtareaCount = 3000;

var txtareaCount_short = 150;


function isIE() {
	if (navigator.userAgent.indexOf("Firefox")!=-1)
		return false;
	else if (navigator.userAgent.indexOf("MSIE")!=-1)
		return true;
	return false;
}

function jumpto(choice) {
  var opt = choice.options[choice.selectedIndex].value;
  alert(opt);
  if (opt != "")
     parent.content.location=opt;
}


function confirmDel(url) {
	if (confirm("Are you sure you want to delete this product?"))
		location.href = url;
}

function isNumberKey(evt) {
	
	var charCode = (evt.which) ? evt.which : event.keyCode;
	
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;

	return true;
}

function limiter(x){
	var txt = document.getElementById(x).value;
	var len = txt.length;
	
	if(len > txtareaCount){
        txt = txt.substring(0,txtareaCount);
        document.getElementById(x).value = txt;
        return false;
	}
	document.getElementById("flimit").value = txtareaCount - len;
}

function limiter_short(x){
	var txt = document.getElementById(x).value;
	var len = txt.length;
	
	if(len > txtareaCount_short){
        txt = txt.substring(0,txtareaCount_short);
        document.getElementById(x).value = txt;
        return false;
	}
	document.getElementById("flimit").value = txtareaCount_short - len;
}


function chgInfo(element, info){
	var obj = document.getElementById(element);
	obj.innerHTML = info;
}

function chgInfoGallery(var1, var2, var3, var4, var5, colorId1, colorId2, colorId3, colorId4, colorId5, color1, color2, color3, color4, color5){
	var obj = document.getElementById('bigInfoGallery');
	var info = "";
	var url = "";
	
	for (var x=1; x<6; x++) {
		tmp = eval("var" + x);
		
		if (tmp!="")
			info += "<b>" + tmp + "</b>: ";
		
		tmp = eval("colorId" + x);
		tmp2 = eval("color" + x);
		if (tmp>0 && tmp2!="")
			info += "<a href='detail.php?cid=" + tmp + "'>" + tmp2 + "</a><br>";
	}
	
	/*
	if (var1!="")
			info += var1 + ": ";
		
		if (colorId1>0 && color1!="")
			info += "<a href='detail.php?cid=" + colorId1 + "'>" + color1 + "</a><br>";
			*/
		
	obj.innerHTML = url + info;
}

function chgFrmInfo(element, info){
	var obj = document.getElementById(element );
	obj.value = info;
}

function chgCursor(element) {
	element.style.cursor='pointer';
}

function getXMLHttpRequest() {
	try {
		req = new XMLHttpRequest(); /* e.g. Firefox */
	} catch(err1) {
	  	try {
	  		req = new ActiveXObject("Msxml2.XMLHTTP"); /* some versions IE */
	  	} catch (err2) {
	    	try {
	    		req = new ActiveXObject("Microsoft.XMLHTTP"); /* some versions IE */
	    	} catch (err3) {
	     		req = false;
	   		}
	  	}
	}
	return req;
}

var xmlHTTPreq = getXMLHttpRequest();

var xmlHTTPreq = getXMLHttpRequest();


function subscribe_milis() {
	var email = document.getElementById("milis_email").value;
	
	if (email!="" && email!="your email here") {
		var url = 'milis.php';
		var myRand = parseInt(Math.random()*999999999999999);
		
		// add random number to URL to avoid cache problems
		url = url+"?email="+email+"&rand="+myRand;
		xmlHTTPreq.open("GET", url, true);
		// set up the callback function
		xmlHTTPreq.onreadystatechange = displayMsg;
		xmlHTTPreq.send(null);
	} 
}

function displayMsg() {
	var obj1 = document.getElementById("milis_msg");
	var obj2 = document.getElementById("milis_box");
	
	obj2.style.display = "none";
	
	if (xmlHTTPreq.readyState == 4) {
    	if(xmlHTTPreq.status == 200) {
			
    		var msg = xmlHTTPreq.responseText;
    		obj1.innerHTML = msg;

    		/*
    		var cType = xmlHTTPreq.getResponseHeader("Content-Type");
    		document.getElementById('contentType').innerHTML = cType;
    		
    		var msg = xmlHTTPreq.responseXML.getElementsByTagName("message");
    		document.getElementById('msg').innerHTML = message.item(0).childNodes[0].firstChild.nodeValue;
			*/
    	}
	} else {
		obj1.innerHTML = "";
	}
	
	obj2.style.display = "none";
	obj1.style.display = "block";
}


function validateCart() {
	var obj1 = document.getElementById("frmSize");
	var obj2 = document.getElementById("frmNoSize");
	
	if ((obj1.value=="") && (obj2.value==0)){
		chgFrmInfo('oosInfo','Please choose the size.');
		return false;
	}
	return true;
}


function validateSearch() {
	var obj = document.getElementById("tag");
	
	if (obj.value==""){
		alert("Please type a keyword for your search.");
		document.frmsearch.tag.focus();
		return false;
	}
	
	return true;
}

function isEmpty(x, val, msg){
	var obj = document.getElementById(x);
	if (obj.value==val) {
		alert(msg);
		obj.focus();
		return true;
	} else {
		return false;
	}
}

function validateOrder() {
	
	if (document.getElementById("custId").value=="") {
		if (isEmpty("frmFirstName","", "Please fill in your First Name.")) return false;
		if (isEmpty("frmLastName","", "Please fill in your Last Name.")) return false;
		if (isEmpty("frmEmail","", "Please fill in your Email.")) return false;
		if (isEmpty("frmMobile","", "Please fill in your Mobile Number.")) return false;
		
		if (document.getElementById("frmAddr1").value=="" && document.getElementById("frmAddr2").value=="") {
			alert("Please fill in your Address.");
			document.getElementById("frmAddr1").focus();
			return false;
		}
		
		if (isEmpty("frmPostalCode","", "Please fill in your Postal Code.")) return false;
		if (isEmpty("frmCity","", "Please fill in your City.")) return false;
		if (isEmpty("frmCountry","", "Please select the Country.")) return false;
	}
	
	if (document.getElementById("frmDAddr1").value!="") {
		if (isEmpty("frmDPostalCode","", "Please fill in the Postal Code.")) return false;
		if (isEmpty("frmDCity","", "Please fill in the City.")) return false;
		if (isEmpty("frmDCountry","", "Please select the Country.")) return false;
	}
	
	return true;
}

function chgBigPic(img, path) {
	var obj = document.getElementById("bigPic");
	var ratio;
	var imgWidth;
	var imgHeight;
	var ctr;
	
	obj.innerHTML = "<img src='" + path + "/" + img + "' id='imgBigPic' name='imgBigPic' />";
}

function adjustPicSize(x) {
	var obj = document.getElementById(x);
	
	imgWidth = Number(obj.width);
	imgHeight = Number(obj.height);
	
	if (imgWidth > imgHeight) {
		ratio = 400 / imgWidth;
	} else {
		ratio = 400 / imgHeight;
	}
	
	obj.width =  Math.round(imgWidth * ratio);
	obj.height =  Math.round(imgHeight * ratio);
}

//var tellfriend = false;
//var tellfriend_submit = false;

function display_tellfriend() {
	/*
	if (tellfriend_submit) {
		if (tellfriend) {
			tellfriend=false;
			document.getElementById("tfthx").style.display = "none";
		} else {
			tellfriend=true;
			document.getElementById("tfthx").style.display = "block";
		}
	} else {
		if (tellfriend) {
			tellfriend=false;
			document.getElementById("tfform").style.display = "none";
		} else {
			tellfriend=true;
			document.getElementById("tfform").style.display = "block";
		}
	}*/
	
	if (document.getElementById("tfform").style.display == "none") {
		document.getElementById("tfthx").style.display = "none";
		document.getElementById("tfform").style.display = "block";
	} else if (document.getElementById("tfthx").style.display == "block") {
		document.getElementById("tfthx").style.display = "none";
		document.getElementById("tfform").style.display = "none";
	} else {
		document.getElementById("tfthx").style.display = "none";
		document.getElementById("tfform").style.display = "none";
	}
}

function submit_tellfriend() {
	
	if (isEmpty("tfName", "", "Please fill in your Name")) return false;
	if (isEmpty("tfFriendEmail", "", "Please fill in your friend's Email")) return false;
	
	var tfName = document.getElementById("tfName").value;
	var tfFriendEmail = document.getElementById("tfFriendEmail").value;
	var colorId = document.getElementById("frmColorId").value;
	
	var url = "tellfriend.php";
	var myRand = parseInt(Math.random()*999999999999999);
		
	// add random number to URL to avoid cache problems
	url = url+"?tfName="+tfName+"&tfFriendEmail="+tfFriendEmail+"&colorId="+colorId+"&rand="+myRand;
	
	xmlHTTPreq.open("GET", url, true);
	// set up the callback function
	xmlHTTPreq.onreadystatechange = thx_tell_friend;
	xmlHTTPreq.send(null);
}

function thx_tell_friend() {
	var obj1 = document.getElementById("tfthx");
	var obj2 = document.getElementById("tfform");
	
	obj2.style.display = "none";
	
	if (xmlHTTPreq.readyState == 4) {
    	if(xmlHTTPreq.status == 200) {
    		var msg = xmlHTTPreq.responseText;
    		obj1.innerHTML = "<table width='100%' class='box'><tr><td>"+msg+"</td></tr></table>";
    	}
	} else {
		obj1.innerHTML = "";
	}
	
	obj2.style.display = "none";
	obj1.style.display = "block";
	tellfriend_submit = true;
}


function validateReg() {
	 
	if (isEmpty("frmEmail","", "Please fill in your Email.")) return false;
	if (isEmpty("frmPwd","", "Please fill in your Password.")) return false;
	
	if (isEmpty("frmFirstName","", "Please fill in your First Name.")) return false;
	if (isEmpty("frmLastName","", "Please fill in your Last Name.")) return false;
	
	var gender = 0;
	for (var i=0; i < document.frmReg.frmGender.length; i++) {
		if (document.frmReg.frmGender[i].checked) {
			gender = 1;
		}
	}
	
	if (gender<1) {
		alert("Please select your Gender.");
		document.getElementById("frmGender").focus();
		return false;
	} 
	
	if (isEmpty("frmMobile","", "Please fill in your Mobile Number.")) return false;
	
	if ((document.getElementById("frmAddr1").value=="") && (document.getElementById("frmAddr2").value=="")) {
		alert("Please fill in your Address.");
		document.getElementById("frmAddr1").focus();
		return false;
	}
	
	if (isEmpty("frmPostalCode","", "Please fill in your Postal Code.")) return false;
	if (isEmpty("frmCity","", "Please fill in your City.")) return false;
	if (isEmpty("frmCountry","", "Please select your Country.")) return false;
	
	return true;
}

function validateSignIn() {	 

	if (isEmpty("email","", "Please fill in your Email.")) return false;
	if (isEmpty("pwd","", "Please fill in your Password")) return false;
		
	return true;
}

function validateForgot() {	 

	if (isEmpty("email","", "Please fill in your Email.")) return false;
		
	return true;
}


function validateProfile() {
	 
	if (isEmpty("frmFirstName","", "Please fill in your First Name.")) return false;
	if (isEmpty("frmLastName","", "Please fill in your Last Name.")) return false;
	
	var gender = 0;
	for (var i=0; i < document.frmReg.frmGender.length; i++) {
		if (document.frmReg.frmGender[i].checked) {
			gender = 1;
		}
	}
	
	if (gender<1) {
		alert("Please select your Gender.");
		document.getElementById("frmGender").focus();
		return false;
	} 
	
	if (isEmpty("frmMobile","", "Please fill in your Mobile Number.")) return false;
	
	if ((document.getElementById("frmAddr1").value=="") && (document.getElementById("frmAddr2").value=="")) {
		alert("Please fill in your Address.");
		document.getElementById("frmAddr1").focus();
		return false;
	}
	
	if (isEmpty("frmPostalCode","", "Please fill in your Postal Code.")) return false;
	if (isEmpty("frmCity","", "Please fill in your City.")) return false;
	if (isEmpty("frmCountry","", "Please select your Country.")) return false;
	
	return true;
}

function validateChgPwd() {	 

	if (isEmpty("pwd1","", "Please fill in your new password.")) return false;
	if (isEmpty("pwd2","", "Please type your new password one more time.")) return false;
	
	if (document.getElementById("pwd1").value != document.getElementById("pwd2").value) {
		alert("Please make sure to type your new password exactly the same twice.");
		document.getElementById("pwd2").focus();
		return false;
	}
		
	return true;
}

function validateChgEmail() {
	if (isEmpty("email","", "Please fill in your new email address.")) return false;
	
	if (document.getElementById("email").value == document.getElementById("current_email").value) {
		alert("Please make sure to type your new email address different from the current one.");
		document.getElementById("email").focus();
		return false;
	}
		
	return true;
}


// ---------------------------------------- Shipping ----------------------------------------

function selectShipping() {
	
	var grandTotal = 0;
	var sfee= 0;
	var curr_sfee= 0;
	
	var idx = document.getElementById("shippingOpt").selectedIndex;
	
	var shippingOpt = document.getElementById("shippingOpt")[idx].value;
	var curr_shippingOpt = document.getElementById("curr_shippingOpt").value;
	
	if (curr_shippingOpt=="")
		curr_sfee = 0;
	else if (curr_shippingOpt=="N")
		curr_sfee = FEE_SHP_NORMAL;
	else if (curr_shippingOpt=="R")
		curr_sfee = FEE_SHP_REG;
	else if (curr_shippingOpt=="C")
		curr_sfee = FEE_SHP_COURIER;
	
	if (shippingOpt=="")
		sfee = 0;
	else if (shippingOpt=="N")
		sfee = FEE_SHP_NORMAL;
	else if (shippingOpt=="R")
		sfee = FEE_SHP_REG;
	else if (shippingOpt=="C")
		sfee = FEE_SHP_COURIER;
		
	document.getElementById("shipping_fee").innerHTML = "$" + String(sfee.toFixed(2));
	
    grandTotal = Number(document.getElementById("grandTotal").value) + sfee - curr_sfee;
	
	
	chgFrmInfo("curr_shippingOpt", shippingOpt);
	//chgFrmInfo("total_after_promo", grandTotal);
	chgFrmInfo("grandTotal", grandTotal);
	chgInfo("grandTotal_fee", "$" + String(grandTotal.toFixed(2)));
}

function validateConfirm() {	 

	if (isEmpty("shippingOpt","", "Please select your preferred shipping.")) return false;
		
	return true;
}


// ---------------------------------------- Extra ----------------------------------------

function chkGiftCard() {
	var grandTotal = 0;
	
	grandTotal = Number(document.getElementById("grandTotal").value);
	
	for (var i=0; i<document.frmConfirm.elements.length; i++) {
		if (document.frmConfirm.elements[i].type == "checkbox") {
			if (document.frmConfirm.elements[i].name=="giftCard") {
				if (document.frmConfirm.elements[i].checked) {
					grandTotal += FEE_GIFTCARD;
					chgInfo("giftCard_fee", "$" + String(FEE_GIFTCARD.toFixed(2)));
				} else {
					grandTotal -= FEE_GIFTCARD;
					chgInfo("giftCard_fee", "-");
				}
			}
		}
    }
	
	chgFrmInfo("grandTotal", grandTotal);
	chgInfo("grandTotal_fee", "$" + String(grandTotal.toFixed(2)));
}

function chkGiftWrap() {
	var grandTotal = 0;
	
	grandTotal = Number(document.getElementById("grandTotal").value);
	
	for (var i=0; i<document.frmConfirm.elements.length; i++) {
		if (document.frmConfirm.elements[i].type == "checkbox") {
			if (document.frmConfirm.elements[i].name=="giftWrap") {
				if (document.frmConfirm.elements[i].checked) {
					grandTotal += FEE_GIFTWRAP;
					chgInfo("giftWrap_fee", "$" + String(FEE_GIFTWRAP.toFixed(2)));
				} else {
					grandTotal -= FEE_GIFTWRAP;
					chgInfo("giftWrap_fee", "-");
				}
			}
		}
    }
	
	chgFrmInfo("grandTotal", grandTotal);
	chgInfo("grandTotal_fee", "$" + String(grandTotal.toFixed(2)));
}


// ---------------------------------------- Milis ----------------------------------------


function subscribe_milis() {
	var email = document.getElementById("milis_email").value;
	
	if (email!="" && email!="your email here") {
		var url = 'milis.php';
		var myRand = parseInt(Math.random()*999999999999999);
		
		// add random number to URL to avoid cache problems
		url = url+"?email="+email+"&rand="+myRand;
		xmlHTTPreq.open("GET", url, true);
		// set up the callback function
		xmlHTTPreq.onreadystatechange = displayMsg;
		xmlHTTPreq.send(null);
	} 
}

function displayMsg() {
	var obj1 = document.getElementById("milis_msg");
	var obj2 = document.getElementById("milis_box");
	
	obj2.style.display = "none";
	
	if (xmlHTTPreq.readyState == 4) {
    	if(xmlHTTPreq.status == 200) {
			
    		var msg = xmlHTTPreq.responseText;
    		obj1.innerHTML = msg;

    		/*
    		var cType = xmlHTTPreq.getResponseHeader("Content-Type");
    		document.getElementById('contentType').innerHTML = cType;
    		
    		var msg = xmlHTTPreq.responseXML.getElementsByTagName("message");
    		document.getElementById('msg').innerHTML = message.item(0).childNodes[0].firstChild.nodeValue;
			*/
    	}
	} else {
		obj1.innerHTML = "";
	}
	
	obj2.style.display = "none";
	obj1.style.display = "block";
}


// ---------------------------------------- Photocard ----------------------------------------

function giftCard_upload() {
	if (isEmpty("giftCard_photo", "", "Please select a photo for your photocard.")) return false;
	
	var ctr = 0;
	for (var i=0; i < document.frmGiftCard.template.length; i++) {
		if (document.frmGiftCard.template[i].checked) {
			ctr = 1;
		}
	}
	
	if (ctr<1) {
		alert("Please select a card template that you want to use.");
		document.getElementById("template").focus();
		return false;
	} 
	
	if (isEmpty("template", "", "Please select a card template for your photocard.")) return false;
	chgFrmInfo("issubmit", 1);
	return true;
}


function finishUpload(status){
	if (status == 0){
		document.getElementById("result").innerHTML = "<p class='msg'>Sorry, upload was unsuccessful. Please try again.</p>";
	} else {
		document.getElementById("result").innerHTML = status;
	}
	
	document.getElementById("upload_progress").style.visibility = "hidden";
	document.getElementById("result").style.visibility = "visible";
	return true;
}


function setPreview(orientation, extra) {
	
	var photo = document.getElementById("preview_photo");
	var msg = document.getElementById("preview_msg");
	
	if (orientation=='L') {
		// landscape
		if (isIE()) {
			photo.style.top = 236 + 'px';
			photo.style.left = Math.round((document.body.offsetWidth/2)) - 4 + extra + 'px';
			msg.style.width = 330 + 'px';
			msg.style.height = 80 + 'px';
			msg.style.top = 484 + 'px';
			msg.style.left = Math.round((document.body.offsetWidth/2)) + 25 + extra + 'px';
		} else {
			photo.style.top =  212 + 'px';
			photo.style.left =  Math.round((document.body.offsetWidth/2)) - 13 + extra + 'px';
			msg.style.width = 330 + 'px';
			msg.style.height = 80 + 'px';
			msg.style.top = 460 + 'px';
			msg.style.left = Math.round((document.body.offsetWidth/2)) + 10 + extra + 'px';
		}
		msg.style.textAlign = "right";
		
	} else {
		// portrait
		if (isIE()) {
			photo.style.top = 222 + 'px';
			photo.style.left = Math.round((document.body.offsetWidth/2)) - 145 + extra + 'px';
			msg.style.width = 190 + 'px';
			msg.style.height = 200 + 'px';
			msg.style.top = 222 + 'px';
			msg.style.left = Math.round((document.body.offsetWidth/2)) + 111 + extra + 'px';
			
		} else {
			photo.style.top = 198 + 'px';
			photo.style.left = Math.round((document.body.offsetWidth/2)) - 153 + extra + 'px';
			msg.style.width = 190 + 'px';
			msg.style.height = 200 + 'px';
			msg.style.top = 198 + 'px';
			msg.style.left = Math.round((document.body.offsetWidth/2)) + 100 + extra + 'px';
			
		}
		msg.style.textAlign = "left";
	}
}

//-----------------------------------------------------------------------------------

var rcmsg="Sorry, function has been disabled.";

function clickIE4(){
	
	if (event.button==2){
		alert(rcmsg);
		return false;
	}
}

function clickNS4(e){
	if (document.layers||document.getElementById&&!document.all){
		if (e.which==2||e.which==3){
			alert(rcmsg);
			return false;
		}
	}
}

if (document.layers){
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown=clickNS4;
} else if (document.all&&!document.getElementById){
	document.onmousedown=clickIE4;
}

document.oncontextmenu=new Function("alert(rcmsg);return false");


function chgCart(colorId, cartId) {
	var qty = document.getElementById("qty"+colorId).value;
	if (qty>0)
		location.href="shopcart_edit.php?cartId="+cartId+"&qty="+qty;
	else
		alert("Please enter a valid quantity");
}

function ppchkout() {
	chgFrmInfo("ppchkout", 1);
	document.frmConfirm.submit();
}