// JavaScript Document

function doSignUpNewsletter(frmObj) {

	var fo=frmObj;
	if (isBlank(fo.email_address.value)) {
		showError(fo,"Please enter your email.","error",fo.email_address);
		fo.email_address.focus();
 		return ;
	} 
	else if (!isEmail(fo.email_address.value)) {
		showError(fo,"Please enter valid email.","error",fo.email_address);
		fo.email_address.focus();
 		return ;
	}

	/*url = 'index.php?nav1=newsletter&' + fo.toQueryString();
	var myAjax = new Ajax(url, {method: 'get', onComplete : showFinishSignup});
	myAjax.request();*/
	window.open('http://oi.vresp.com?fid=69fe741083', 'vr_optin_popup', 'scrollbars=yes,width=600,height=450' );
	fo.submit();	
}


function doSignUpNewsletterSubmit(frmObj) {
	var fo=frmObj;
	
	if (isBlank(fo.first_name.value)) {
		showError(fo,"Please enter your first name.","error",fo.first_name);
		fo.first_name.focus();
 		return false;
	} 
	
	if (isBlank(fo.last_name.value)) {
		showError(fo,"Please enter your last name.","error",fo.last_name);
		fo.last_name.focus();
 		return false;
	} 

	if (isBlank(fo.email_address.value)) {
		showError(fo,"Please enter your email.","error",fo.email_address);
		fo.email_address.focus();
 		return false;
	} 
	else if (!isEmail(fo.email_address.value)) {
		showError(fo,"Please enter valid email.","error",fo.email_address);
		fo.email_address.focus();
 		return false;
	}

	//window.open( 'http://www.verticalresponse.com', 'vr_optin_popup', 'scrollbars=yes,width=600,height=450' );
	/*url = 'index.php?nav1=newsletter&' + fo.toQueryString();
	var myAjax = new Ajax(url, {method: 'get', onComplete : showFinishSignup});
	myAjax.request();*/
	//window.open('http://oi.vresp.com?fid=69fe741083', 'vr_optin_popup', 'scrollbars=yes,width=600,height=450' );
	//return true;
}


function goNewsletter(frmObj) {
	var fo=frmObj;

	if (isBlank(fo.email_address.value)) {
		showError(fo,"Please enter your email.","error",fo.email_address);
		fo.email_address.focus();
 		return false;
	} 
	else if (!isEmail(fo.email_address.value)) {
		showError(fo,"Please enter valid email.","error",fo.email_address);
		fo.email_address.focus();
 		return false;
	}
	
	return true;
}



function showFinishSignup(response)
{
	fo = $('register');
	if (response == 'success')
	{
		showError(fo,"Your email has been saved to our newsletter database.","error",fo.email);		
	}
	else
	{		
		showError(fo,"The email has existed in database already. Please input another email.","error",fo.email);
			$('register').email.focus();
	}
}


function doLogin()
{
	$('login').action = "?nav1=login";
	$('login').send({onComplete: handleLogin});
}

function handleLogin(response)
{
	if (response == "fail")
	{
		alert("Invalid Username or Password.");
		$('login').username.focus();
		return;
	}
	else
	{		
		$('login').getChildren().each(function(item){
			item.remove();
		});

		$('login').setHTML(response);
	}
}


function doLogout()
{
	$('login').action = "?nav1=logout";
	$('login').send({onComplete: handleLogout});
}


function handleLogout(response)
{	
	$('login').getChildren().each(function(item){
		item.remove();
	});
	$('login').setHTML(response);
}

function galleryShow(path, nav)
{
	indexGallery = indexGallery + nav;
	if (indexGallery == 0)
	{
		//indexPress = arrayPhoto.length-1;	
		$('portfolioBtnBack').style.display="none";
		$('portfolioBtnNext').style.display="block";
	}
	else if (indexGallery == arrayPhoto.length-1)
	{
		//indexPress = 0;
		$('portfolioBtnBack').setAttribute("style","float:right");
		$('portfolioBtnBack').style.display="block";
		$('portfolioBtnNext').style.display="none";		
	}
	else
	{
		$('portfolioBtnBack').setAttribute("style","float:left");
		$('portfolioBtnNext').style.display="block";
		$('portfolioBtnBack').style.display="block";
	}

	imageLoad = path + arrayPhoto[indexGallery];	
	$('gallery_image').src = imageLoad;
}


function changePhoto(path, nav, img1, img2)
{
	imageLoad = path + arrayPhoto[nav];
	//imageDetailLoad = path + arrayPhotoDetail[nav];
	img1.src = imageLoad;
	//img2.src = imageDetailLoad;
	
	var strColor = $('color').options[$('color').selectedIndex].text;
	var arrPart = strColor.split('+');
	var numPart = arrPart.length;
	var sel = $('color').selectedIndex;
	
	$$('#colorList img').each(function(itm, idx){
			i = numPart * sel + idx;
			if (i < arrayColor.length)
			{
				itm.src = path + arrayColor[i];				
				itm.title = arrayColorName[i];	
				itm.alt = arrayColorName[i];	
			}

			itm.alt = arrPart[idx]
	});
	
	if (arrayPhotoBig[nav] != '')
		$('big_img_pop').href = path + arrayPhotoBig[nav];
		
	$('color').selectedIndex = nav;
	indexGallery = -1;

	/*if (arrayProductImg.length > 0)
	{
		$('portfolioBtnBack').style.display="none";
		$('portfolioBtnNext').style.display="block";
	}*/		
	
	$('portfolioBtnBack').style.display="none";
	$('portfolioBtnNext').style.display="none";
}

function changePhotoComponent(path, img2)
{
	if (arrayPhotoDetail[$('size').selectedIndex] != '')
	{
		imageDetailLoad = path + arrayPhotoDetail[$('size').selectedIndex];	
		img2.src = imageDetailLoad;
	}				
	
	if (arrayReducePrice[$('size').selectedIndex] > 0)
	{
		var roundprice = Math.round((arrayPrice[$('size').selectedIndex]-arrayReducePrice[$('size').selectedIndex])/arrayPrice[$('size').selectedIndex]*100);
		var str_price = "Price: <strong><u>" + arrayPrice[$('size').selectedIndex] + "</u> CAD</strong>";
		str_price += "<span class=\"now\"> now <strong>"+ arrayReducePrice[$('size').selectedIndex] +"</strong> (save " + roundprice +"%)</span>";
		$('price').setHTML(str_price);		
		price = arrayReducePrice[$('size').selectedIndex];
	}
	else
	{
		var str_price = "Price: <strong>" + arrayPrice[$('size').selectedIndex] + "CAD</strong>";
		$('price').setHTML(str_price);
		price = arrayPrice[$('size').selectedIndex];
	}
}

function changePhotoColor(path, img, sel)
{
	if (arrayColorImg[sel.selectedIndex] != '')
	{
		imageDetailLoad = path + arrayColorImg[sel.selectedIndex];	
		img.src = imageDetailLoad;
	}				
}

function productShow(path, nav)
{
	indexProductImg = indexProductImg + nav;
	
	if (indexProductImg == 0)
	{
		//indexPress = arrayPhoto.length-1;	
		$('portfolioBtnBack').style.display="none";
		$('portfolioBtnNext').style.display="block";
	}
	else if (indexProductImg == arrayProductImg.length-1)
	{
		//indexPress = 0;
		$('portfolioBtnBack').setAttribute("style","float:right");
		$('portfolioBtnBack').style.display="block";
		$('portfolioBtnNext').style.display="none";		
	}
	else
	{
		$('portfolioBtnBack').setAttribute("style","float:left");
		$('portfolioBtnNext').style.display="block";
		$('portfolioBtnBack').style.display="block";
	}
	
	if (arrayProductImg.length == 1)
	{
		$('portfolioBtnNext').style.display="none";		
		$('portfolioBtnBack').style.display="none";
	}
	
	imageLoad = path + arrayProductImg[indexProductImg];	
	$('product_image').src = imageLoad;
}

function addCart(product_id, title, code, price, weight)
{	
	$('quantity').value = parseInt($('quantity').value);
	if (!isNumber($('quantity').value))
	{
		alert("Number is invalid.");
		$('quantity').focus();
		return;
	}	
		

	url = web_path + 'index.php?nav1=cart&action=add';
	queryStr  = "&product_id=" + product_id;
	queryStr += "&name=" + title;
	queryStr += "&code=" + code;
	if (document.getElementById('size'))
		queryStr += "&price=" + $('size').value;
	else
		queryStr += "&price=" + price;
		
	queryStr += "&weight=" + weight;
	
	
	if (product_id != -1)
	{
		if (document.getElementById('size'))
			queryStr += "&size=" + $('size').options[$('size').selectedIndex].text;
		else
			queryStr += "&size=" + $('size_1').value;
		
		if ($('chkCustom').checked == true)
		{
			if ($('custom_main').selectedIndex == 0 && $('custom_partA').selectedIndex == 0 && $('custom_partB').selectedIndex == 0 && $('custom_partC').selectedIndex == 0)
			{
				alert("Plese select your custom color.");
				$('custom_main').focus();
				return;
			}
		}
		
		/*if ($('chkReverse').checked == true && $('chkCustom').checked != true)
		{
			alert("Plese choose custom color.");
			$('chkCustom').focus();
			return;
		}*/
		
		if ($('chkReverse').checked == true)
		{
			queryStr += "&reverse=true";
		}
		
		if ($('chkCustom').checked == true)
		{
			var str_color = 'custom';
			queryStr += "&color=" + str_color;
			queryStr += "&custom=true";
			var str_note = "Main part : " + $('custom_main').value;
			str_note += ", Part A : " + $('custom_partA').value;
			str_note += ", Part B : " + $('custom_partB').value;
			str_note += ", Part C : " + $('custom_partC').value;
			queryStr += "&note=" + str_note;
			queryStr += "&main_part=" + $('custom_main').value;
			queryStr += "&partA=" + $('custom_partA').value;
			queryStr += "&partB=" + $('custom_partB').value;
			queryStr += "&partC=" + $('custom_partC').value;
		}
		else
		{
			var str_color = $('color').options[$('color').selectedIndex].text;

			for (j=0; j<4; j++)
				str_color = str_color.replace('+','___');
			
			queryStr += "&color=" + str_color;

			var str_color1 = $('color').value;
			
			var arrPart = str_color1.split('-');
			queryStr += "&main_part=" + arrPart[0];
			queryStr += "&partA=" + arrPart[1];
			queryStr += "&partB=" + arrPart[2];
			queryStr += "&partC=" + arrPart[3];
		}
	}
	else
	{		
		frm = $('addToCartForm');
		if (trim($('gift_to').value) == '')
		{
			showError(frm,"Please input name.","error",frm.gift_to);
			frm.gift_to.focus();
			return;
		}
		if (trim($('gift_from').value) == '')
		{
			showError(frm,"Please input name.","error",frm.gift_from);
			frm.gift_from.focus();
			return;
		}
		if ($('has_coupon_already').value == 1)
		{
			go_continue = confirm('You already have a gift certificate in your cart. You can only purchase 1 gift certificate per checkout.  Click OK to overwrite your current gift certificate');
			if (!go_continue)
				return;
		}
		queryStr += "&gift_from=" + $('gift_from').value;
		queryStr += "&gift_to=" + $('gift_to').value;
		queryStr += "&expire_date=" + $('expire_date').value;
		queryStr += "&coupon_expire_date=" + $('coupon_expire').value;
	}
	queryStr += "&quantity=" + $('quantity').value;
	url += queryStr;
	$('my_total_amount').value = parseInt($('my_total_amount').value) + $('quantity').value;

	var myAjax = new Ajax(url, {method: 'get', onComplete:doViewCart});
	myAjax.request();	
}

function updateCart(product_id,idx,color,size)
{
	$('quantity'+idx).value = parseInt($('quantity'+idx).value);
	if (!isNumber($('quantity'+idx).value))
	{
		alert("Number is invalid.");
		$('quantity'+idx).focus();
		return;
	}

	url = web_path + 'index.php?nav1=cart&action=update';
	queryStr  = "&product_id=" + product_id;
	if (product_id != -1)
	{
		var str_color = color;
		for (j=0; j<4; j++)
			str_color = str_color.replace('+','___');
		queryStr += "&color=" + str_color;
	}
	queryStr += "&quantity=" + $('quantity'+idx).value;
	queryStr += "&size=" + size;
	url += queryStr;

	var myAjax = new Ajax(url, {method: 'get', onComplete:doViewCart});
	myAjax.request();	
}

function removeCart(product_id,color)
{
	url = web_path + 'index.php?nav1=cart&action=remove';
	queryStr  = "&product_id=" + product_id;
	if (product_id != -1)
	{
		var str_color = color;
		for (j=0; j<4; j++)
			str_color = str_color.replace('+','___');
		queryStr += "&color=" + str_color;
	}
	url += queryStr;

	var myAjax = new Ajax(url, {method: 'get', onComplete:doViewCart});
	myAjax.request();	
}

function doViewCart(response)
{
	window.location = web_path + "your-cart/";
}


function pressShow(path, nav)
{
	indexPress = indexPress + nav;
	if (indexPress == 0)
	{
		//indexPress = arrayPhoto.length-1;	
		$('pressBtnBack').style.display="none";
		$('pressBtnNext').style.display="block";
	}
	else if (indexPress == arrayPhoto.length-1)
	{
		//indexPress = 0;
		$('pressBtnBack').setAttribute("style","float:right");
		$('pressBtnBack').style.display="block";
		$('pressBtnNext').style.display="none";
	}
	else
	{
		$('pressBtnBack').setAttribute("style","float:left");
		$('pressBtnNext').style.display="block";
		$('pressBtnBack').style.display="block";
	}


	imageLoad = path + arrayPhoto[indexPress];
	$('press_image').src = imageLoad;
	$('press_download').href = "?nav1=download&type=1&id="+arrayPhotoID[indexPress];
}


function testimonialShow(path, nav)
{
	indexPress = indexPress + nav;
	if (indexPress == 0)
	{
		//indexPress = arrayPhoto.length-1;	
		$('pressBtnBack').style.display="none";
		$('pressBtnNext').style.display="block";
	}
	else if (indexPress == arrayPhoto.length-1)
	{
		//indexPress = 0;
		$('pressBtnBack').setAttribute("style","float:right");
		$('pressBtnBack').style.display="block";
		$('pressBtnNext').style.display="none";
	}
	else
	{
		$('pressBtnBack').setAttribute("style","float:left");
		$('pressBtnNext').style.display="block";
		$('pressBtnBack').style.display="block";
	}
	imageLoad = path + arrayPhoto[indexPress];
	$('testimonial_image').src = imageLoad;	
}

function doRegister(frm)
{
	if (trim(frm.firstName.value)=="")
	{
		alert("Please input your first name.");
		frm.firstName.focus();
		return;
	}

	if (trim(frm.lastName.value)=="")
	{
		alert("Please input your last name.");
		frm.lastName.focus();
		return;
	}

	if (!isEmail(frm.email.value))
	{
		alert("Email is invalid.");
		frm.email.focus();
		return;
	}

	if (trim(frm.username.value)=="")
	{
		alert("Please input username.");
		frm.username.focus();
		return;
	}

	if (trim(frm.password.value)=="")
	{
		alert("Please input password.");
		frm.password.focus();
		return;
	}

	if (trim(frm.confirmPassword.value)=="")
	{
		alert("Please confirm your password.");
		frm.confirmPassword.focus();
		return;
	}

	if (frm.confirmPassword.value!=frm.password.value)
	{
		alert("Password is not match.");
		frm.confirmPassword.focus();
		return;
	}

	$('contactForm').action = "?nav1=register";
	$('contactForm').send({onComplete: handleRegister});
}







function handleRegister(response)
{
	if (response == "fail_user")
	{
		alert("The username has been registered. Please choose another name.");
		$('contactForm').username.focus();
	}
	else if (response == "fail_email")
	{
		alert("The email has been registered. Please choose another email.");
		$('contactForm').email.focus();
	}
	else
	{
		$('contactForm').getChildren().each(function(item){
			item.remove();
		});
		$('contactForm').setHTML(response);
	}
}

function sendForgot()
{
	if (!isEmail($('contactForm').email.value))
	{
		alert("Email is invalid.");
		$('contactForm').email.focus();
		return;
	}

	$('contactForm').action = "?nav1=forgot";
	$('contactForm').send({onComplete: handleForgot});
}

function handleForgot(response)
{
	$('contactForm').getChildren().each(function(item){
		item.remove();
	});

	$('contactForm').setHTML(response);
}


function changeCountry(obj_select, country_code)
{
	if (country_code=='US')
	{
		var arrStateCode = Array('-1','AK','AL','AS','AR','AZ','CA','CO','CT','DC','DE','FL','GA','GU','HI','IA','ID','IL','IN','KS','KY','LA','MA','MD','ME','MI','FM','MN','MO','MS','MT','NC','ND','NE','NH','NJ','NM','NV','NY','OH','OK','OR','PA','PR','RI','SC','SD','TN','TX','UT','VA','VI','VT','WA','WI','WV','WY');

		var arrStateName = Array('please choose','Alaska','Alabama','American Samoa','Arkansas','Arizona','California','Colorado','Connecticut','District of Columbia','Delaware','Florida','Georgia','Guam','Hawaii','Iowa','Idaho','Illinois','Indiana','Kansas','Kentucky','Louisiana','Massachusetts','Maryland','Maine','Michigan','Micronesia','Minnesota','Missouri','Mississippi','Montana','North Carolina','North Dakota','Nebraska','New Hampshire','New Jersey','New Mexico','Nevada','New York','Ohio','Oklahoma','Oregon','Pennsylvania','Puerto Rico','Rhode Island','South Carolina','South Dakota','Tennessee','Texas','Utah','Virginia','Virgin Islands','Vermont','Washington','Wisconsin','West Virginia','Wyoming');
	}
	else if (country_code=='CA')
	{
		var arrStateCode = Array('-1','AB','BC','MB','NB','NL','NS','NT','NU','ON','PE','QC','SK','YT');
		var arrStateName = Array('please choose','Alberta','British Columbia','Manitoba','New Brunswick','Newfoundland','Nova Scotia','Northwest Territories','Nunavut','Ontario','Prince Edward Island','Quebec','Saskatchewan','Yukon');
	}
	else
	{
		var arrStateCode = Array('-1','--');
		var arrStateName = Array('please choose','Outside U.S./Canada');
	}

	while (obj_select.options.length > 0) 
	{
		obj_select.options[(obj_select.options.length - 1)] = null;
	}

	for (var i=1; i <= arrStateCode.length; i++) 
	{
		var option = new Option(arrStateName[i-1],arrStateCode[i-1]);
		obj_select.options[i-1]=option;
	}
}


function changeImg(obj,src)
{
	if (src != '')
		obj.src = src;
}

function redeemCode()
{	
	fo = $('contactFormFinal');
	if (trim($('txtCoupon').value) == '')
	{
		showError(fo,"Please enter coupon code.","error",fo.txtCoupon);
		$('txtCoupon').focus();
		return;
	}

	url = web_path+'checkout/' + $('txtCoupon').value + '/redeem/' ;
	window.location =url;
}

function checkCustomColor()
{
	if ($('chkCustom').checked == true)
	{
		$('color').options[$('color').length] = new Option('custom', 'custom');
		$('color').selectedIndex = $('color').length - 1;
		$('color').disabled = true;
		//$('chkReverse').checked = true;
	}
	else
	{
		$('color').removeChild($('color').options[$('color').length - 1]);
		$('color').disabled = false;
		//$('chkReverse').checked = false;
	}

}

function checkReverse()
{
	if ($('chkCustom').checked == true)
		$('chkReverse').checked = true;
}

function checkRush()
{
	if ($('chkRushShipping').checked == true)
		$('chkRush').value = 1;
	else
		$('chkRush').value = '';
}

function filterProduct()
{
	$('frmSearch').action = window.location;
	$('frmSearch').send({update:'content', onComplete:function(){				
		// Init fix tooltip
		productList_fixThumb();
	}});
	return false;
}

function changePage(obj)
{	
	if (isNaN(obj))
		var page = parseInt(obj.text)-1;
	else
		page = obj;

	$('page').value = page;
	$('frmSearch').action = window.location;
	$('frmSearch').send({update:'content', onComplete:function(){
		if (Number(obj)=="-1"){
			document.getElementById('pagingStatus').style.width="466px";
		};
		
		// Init fix tooltip
		productList_fixThumb();
	}});
	
	
}

function postTags(tag)
{
	url = 'index.php?nav1=tag';
	var myAjax = new Ajax(url, {method: 'post', data : {'tagname': tag, 'act' : 'tag'}, onComplete:function(){window.location=web_path+'product-list/'}});
	myAjax.request();		
}

function isGift(obj)
{
	if (obj.checked == true)
		val = 'y';
	else
		val = 'n';
	url = 'index.php?nav1=cart&action=is_gift&val='+val;

	var myAjax = new Ajax(url, {method: 'get', update:'cart_content'});

	myAjax.request();	
}

function initState()
{
	obj_select = $("ordCountry");
	obj_select1 = $("shipCountry");
	
	if (obj_select.value == 'CA')
		changeCountry($("ordProvince"), 'CA');
	else if (obj_select.value == 'US')	
		changeCountry($("ordProvince"), 'US');
	else
		changeCountry($("ordProvince"), '');
		
	if (obj_select1.value == 'CA')
		changeCountry($("shipProvince"), 'CA');
	else if (obj_select1.value == 'US')	
		changeCountry($("shipProvince"), 'US');
	else
		changeCountry($("shipProvince"), '');
}