/* PBA Javascript file
 * Created by: Jordan
 * 
 * 
 */
function upgrade(price, checkbox, checkbox2, targetdiv){
	var total=new Number(price);
	if(checkbox.checked==true){
		var addval=new Number(checkbox.value)
		total=price + addval;
	}
	if(checkbox2.checked==true){
		var addval2=new Number(checkbox2.value)
		total=total + addval2;
	}
	//alert(total.toFixed(2));
	document.getElementById(targetdiv).innerHTML="S$ "+CommaFormatted(total.toFixed(2));
}

function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	amount = new String(amount);
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}


function show_ajax_msg(msg){
	var div=document.getElementById("ajax_message");
	div.innerHTML="<p style='line-height:20px; padding: 5px 5px 5px 5px'>"+msg+"</p>";
	div.style.display="block";
	setTimeout("hide_ajax_msg()", 3000);
}

function hide_ajax_msg(){
	var div=document.getElementById("ajax_message");
	div.innerHTML="&nbsp;";
	div.style.display="none";
}

function add2cart(iwish, prod, qty, update){
	//alert("PRODUCT:"+prod+" PRICE:"+iwish+ " QTY:"+qty);
	
	if((iwish != "")||(iwish!=0)){
	
	SendMessage = new sack();	
	SendMessage.requestFile = "add2cart.php"; 
	SendMessage.method="POST";

    SendMessage.setVar("product", prod);		
    SendMessage.setVar("iwish_price", iwish);
    SendMessage.setVar("qty", qty);
    SendMessage.setVar("update", update);
    	
    if(update=="true")
        SendMessage.onCompletion= function(){location.reload(true);}
    else
    	SendMessage.onCompletion = function(){ partial_refreshcart();show_ajax_msg("Item added to cart<br/><img src='images/itemadded.gif'>");}
	SendMessage.runAJAX();
	}else{
		alert("Please enter an iwish amount.");
	}
	return false;
}

function update_click_count(crm_id){
	if((crm_id != "")||(crm_id!=0)){
		SendMessage = new sack();	
		SendMessage.requestFile = "ad_clicks.php"; 
		SendMessage.method="POST";
	    SendMessage.setVar("crm_id", crm_id);		
	    SendMessage.runAJAX();
		return true;
	}
}

function remove_cart_item(cart_id, product_id){
	/*do ajax to remove item from cart */
	
	remove = new sack();
	remove.requestFile = "remove_cart_item.php";
	remove.method="POST";
	
	remove.setVar("cart_id",cart_id);
	remove.setVar("product_id",product_id);
	remove.onCompletion = function(){ location.reload(true);}
	remove.runAJAX();
	return false;
}
function partial_refreshcart(){
	//alert("refreshing cart");
	refresh = new sack();
	refresh.requestFile = "refreshcart.php";
	refresh.method="POST";
	refresh.setVar("refresh","partial");
	refresh.execute=true;
	refresh.runAJAX();
	return false;
}
function showaddress(){
		document.getElementById("delivery_address").style.display="block";
}
function hideaddress(){
		document.getElementById("delivery_address").style.display="none";
}

function showmembersform(){
	if(!document.getElementById("MemberCheck").checked)
		document.getElementById("FamilyForm").style.display="none";
	else
		document.getElementById("FamilyForm").style.display="block";
}
function checktc(){
	var checkbox=document.getElementById("iagreetc");
	if(checkbox.checked)
		return true;
	else{
		alert("You must agree to the Terms and Conditions before proceeding.")
		return false;
	}	
}
function removeslot(no){
	var slot=document.getElementById("compareslot"+no);
	if(slot.innerHTML=="")
		alert("there is nothing to remove here");
	else
		slot.innerHTML="";
	
	document.cookie="compare_products_slot_" + no + "=";
}
function compare_check(checkbox, thumbnail){
  if(checkbox.checked)
	  addslot(checkbox.value, thumbnail);
  else{
	  for(var i=0; i<3; i++){
		  if(checkbox.value==CompareSlotArray[i])
			  removeslot(i+1);
	  }
  }
}
function addslot(product_id, thumbnail){
	var slot1=document.getElementById("compareslot1");
	var slot2=document.getElementById("compareslot2");
	var slot3=document.getElementById("compareslot3");
	var img="<img src='"+thumbnail+"' class='comparethumb'>";
	var prod="<input type='hidden' value='"+product_id+"' name='compare_product[]'>";
	if(slot1.innerHTML==""){
		
		slot1.innerHTML=img+prod;
		CompareSlotArray[0]=product_id;
		set_compareProducts_cookie("compare_products_slot_1", product_id, thumbnail);
	}else if (slot2.innerHTML==""){
	    slot2.innerHTML=img+prod;
	    CompareSlotArray[1]=product_id;
	    set_compareProducts_cookie("compare_products_slot_2", product_id, thumbnail);
	}else if (slot3.innerHTML==""){
		slot3.innerHTML=img+prod;
		CompareSlotArray[2]=product_id;
		set_compareProducts_cookie("compare_products_slot_3", product_id, thumbnail);
	}else{
		slot1.innerHTML=img+prod;
		CompareSlotArray[0]=product_id;
		set_compareProducts_cookie("compare_products_slot_1", product_id, thumbnail);
	}

}

//set cookie function for compare products
//to allow data retrieval accross all pages
function set_compareProducts_cookie(slot, product_id, thumbnail) {
	document.cookie=slot + "=" + product_id+"|"+thumbnail;
}

//check input to only allow integers for price input
function checkNan(id){
	var element=document.getElementById(id);
	  
	if(element.value != ""){
		if(isNaN(element.value)){
			alert("Notice: Numbers only please");
			element.value=0;
			element.focus();
	  	}else{
	  		return true;
	  	}
	}
}

