function checkCart(){
	var error = 0;
	$(".isNumber").each(function isNumber(){
		var quantity = $(this).val();
		if (!isNumeric($(this).val()) || quantity == 0){
			error++;
		}
	});
	
	if (error > 0){
		alert("請輸入適當數量");
	} else {
		$("#cartForm").submit();
	}
}

function isNumeric(value) 
{ 
    if (value.match(/^\d+$/) == null) 
        return false; 
    else 
        return true; 
}
function toggleItById(id){
	
}

function addCart(selfObjId,itemId,goCartId,ajaxloadId){
$("#"+selfObjId).toggle();
$("#"+ajaxloadId).toggle();
var query = "itemId="+itemId;

$.ajax({
	type: "GET",
	url: "cartAdd.php",
	data: query,
	success: function(msg){setTimeout(function(){
		$("#"+ajaxloadId).toggle();
		$("#"+goCartId).toggle();
	},500);}
});
}
function addCartNew(selfObjId,itemId,goCartId,ajaxloadId){
$("#"+selfObjId).toggle();
$("#"+ajaxloadId).toggle();
var query = "itemId="+itemId;

$("#propertyForm"+itemId+" input:radio:checked").each( function(){ query += "&propertyId[]=" + this.value });

$.ajax({
	type: "GET",
	url: "cartAddNew.php",
	data: query,
	success: function(msg){setTimeout(function(){
		$("#"+ajaxloadId).toggle();
		$("#"+goCartId).toggle();
	},500);}
});
}

var targetObjGlobal = "";
var sourceSrcGlobal = "";

function changeFullImage(targetObj,sourceSrc){
	targetObjGlobal = targetObj;
	sourceSrcGlobal = sourceSrc;
	$(targetObj).fadeOut("slow",function(){targetObjGlobal.src=sourceSrcGlobal;$(targetObjGlobal).fadeIn("slow")});
}
var targetObjNumIdGlobal = "";
var sourceSrc1Global = "";
var sourceSrc2Global = "";
var sourceSrc3Global = "";
function changeAllFullImage(targetObjNumId,sourceSrc1,sourceSrc2,sourceSrc3){
	targetObjNumIdGlobal = targetObjNumId;
	sourceSrc1Global = sourceSrc1;
	sourceSrc2Global = sourceSrc2;
	sourceSrc3Global = sourceSrc3;
	
	$("#imageImg"+targetObjNumIdGlobal).fadeOut("slow",function(){
		$("#imageImg"+targetObjNumIdGlobal).attr("src",sourceSrc1Global);
		$("#imageImg"+targetObjNumIdGlobal).fadeIn("slow")}
	);
	$("#imageImgBig"+targetObjNumIdGlobal).fadeOut("slow",function(){
		$("#imageImgBig"+targetObjNumIdGlobal).attr("src",sourceSrc2Global);
		$("#imageImgBig"+targetObjNumIdGlobal).fadeIn("slow")}
	);
	$("#imageImgSmall"+targetObjNumIdGlobal).fadeOut("slow",function(){
		$("#imageImgSmall"+targetObjNumIdGlobal).attr("src",sourceSrc3Global);
		$("#imageImgSmall"+targetObjNumIdGlobal).fadeIn("slow")}
	);
}

function changeOrderStatus(orderId,type,ButtonId){
	var value = "";
	if($("#"+type+"0"+orderId).attr("checked")){
		value = 0;
	} else if($("#"+type+"1"+orderId).attr("checked")){
		value = 1;
	} else {
		alert("An error occur due to no value input");
		return ;
	}

	$.ajax({
		type: "POST",
		url: "orderChangeStatus.php",
		data: "orderId="+orderId+"&type="+type+"&value="+value,
		success: function(msg){
			if (msg == "success"){
				displayTurnToNone(ButtonId);
			} else {
				alert(msg);
			}
		},
		error: function(msg){
			alert(msg);
		}
	});
}

function toggleOMD(id){
$("#orederManageDivId"+id).slideToggle("fast");
}
function displayTurnToNone(id){
$("#"+id).hide("slow");
}
function displayTurnToBlock(id){
$("#"+id).show("slow");
}
var isMenuAnimated = false;
function displayTurnToBlockFromMenu(id,aClass){
	if(isMenuAnimated == false){
		if($("#"+id).css("display")=="none"){ 			
			isMenuAnimated = true;
			var counter = 0;
			$("."+aClass+":not(:animated)").hide("slow",function showSelectedMenu(){
				counter++;
				if(counter > $("."+aClass).size()-1){
					$("#"+id).show("slow",function chanageIsMenuAnimated(){
						isMenuAnimated = false;
					});
				}
				
			});
		}
	}
	/*
	if(isMenuAnimated == false){
		if($("#"+id).css("display")=="none"){ 
			isMenuAnimated = true;
			$("#"+id).show("fast",function chanageIsMenuAnimated(){isMenuAnimated = false;});
			$("."+aClass+":not(:animated)").hide("slow");
		}
	}*/
}


function toggleById(id){
	$("#"+id).slideToggle("fast");
}
function openSubsribeWin(){
	window.open('http://virgo.hkfyg.org.hk/subscribe/index.php','','width=453,height=260');
}

function checkSearchForm(obj){
	if (obj.keyword.value.length < 1) {
		alert("請輸入關鍵詞");
		return false;
	}
	return true;
}

function showSelftake(){
$("#selftakeAddressTr").show();
$("#shippingAddressTr").hide();
$("#shippingDistrictTr").hide();
}

function showShipping(){
$("#selftakeAddressTr").hide();
$("#shippingAddressTr").show();
$("#shippingDistrictTr").show();
}

function deleteBrowseHistoryCookie()
{
	try{
		for(var i=0 ; i<6 ; i++){
			document.cookie="browseHistory["+i+"]=" + 0 +";expires=Thu, 01-Jan-1970 00:00:01 GMT";
		}
	} catch (err) {}
	$("#browseHistoryDiv").hide("slow");
}

function showItemName(text){
	var ie=(document.all)?true:false;
	if (ie) {
      tempX=window.event.clientX+document.documentElement.scrollLeft;
      tempY=window.event.clientY+document.documentElement.scrollTop;
   } else {
      tempX=e.pageX;
      tempY=e.pageY;
   }
   tempX += 15;
   document.getElementById('showItemNameSpan').innerHTML = text;
   document.getElementById('showItemNameDiv').style.display = "block";
   document.getElementById('showItemNameDiv').style.top = tempY;
   document.getElementById('showItemNameDiv').style.left = tempX;
}

function hideItemName(){
	document.getElementById('showItemNameDiv').style.display = "none";
}

$(function(){
	var path = location.search.substring(1);
	if ( path ) {
		$('#category a[@href$="' + path + '"]').attr('class', 'selected');
	}
});

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}


