// JavaScript Document

function addtocart(a)
{

	var url ="../functions/addtocart.php";
	var par ="prodid="+a;
	var ajaxObj = new Ajax.Request(url,
				 {
					 method:'get',
					 parameters:par,
					 onComplete:showresponsecart
				 });
}

function showresponsecart(cart_text)
{
	$("productNotifier").style.display="block";
	hide_interval = setInterval(hideObject,7000,"productNotifier");
	resString = (cart_text.responseText).toString();
	//alert(resString);
	//alert( cart_text.responseText );
	if( isNaN(resString))
	{
		$("productNotifier").innerHTML = 'Product already added to cart';
	}
	else
	{
		//alert( cart_text.responseText );
		document.getElementById('cart').innerHTML ="<a href='../showcart/'>Cart ("+parseInt(cart_text.responseText)+")</a>";
				$("productNotifier").innerHTML = 'Product added successfully';
	}
	/*document.getElementById('highlightNav').innerHTML = "Product Added Successfully";
	
	document.getElementById('highlightNav').style.display='block';
	hide_interval = setInterval(hide_response,7000);*/
	

}
function hideObject(target)
{
	$(target).style.display="none";
	clearInterval(hide_interval);
}
function showSlogan()
{
	var url = '../functions/showSlogan.php';
	var par ="";
	var sloganObj = new Ajax.Request(url,
						{
							method:'post',
							parameters:par,
							onComplete :loadSlogan
						});	
}

function loadSlogan(respSlogan)
{
	
	slogan = (respSlogan.responseText).split('$$');
	
	//$('dynamicSlogan').innerHTML =slogan[0];
	//$('sloganAuthor').innerHTML =slogan[1];
	document.getElementById('dynamicSlogan').innerHTML =slogan[0];
	document.getElementById('sloganAuthor').innerHTML =slogan[1];
	document.getElementById('IDSlogan').value =slogan[2];
}

function showTestimony()
{
	var url = '../functions/showTestimony.php';
	var par ="";
	var testimonyObj = new Ajax.Request(url,
						{
							method:'post',
							parameters:par,
							onComplete :loadTestimony
						});	
}

function loadTestimony(respTestimony)
{
	testimony = (respTestimony.responseText).split('$$');
	document.getElementById('dynamicTestimony').innerHTML =testimony[0];
	document.getElementById('testimonyAuthor').innerHTML =testimony[1];
	document.getElementById('testimonyImg').src ='../testimony/'+testimony[2];
	document.getElementById('testimonyImg').width = testimony[3];
	document.getElementById('testimonyImg').height = testimony[4];
	/*$('dynamicTestimony').innerHTML =testimony[0];
	$('testimonyAuthor').innerHTML =testimony[1];
	$('testimonyImg').src ='../testimony/'+testimony[2];
	$('testimonyImg').width = testimony[3];
	$('testimonyImg').height = testimony[4];*/
	

}

function product_find()
{

	value = document.getElementById('find').value;
	if(value == "")
		alert('enter the search key word');
	else
		document.location.href = '../products/index.php?q='+value;
}




function changeCont()
{
	if(document.getElementById('mailid').value=="E-mail Id" || document.getElementById('mailid').value=='')
	{
		alert('Please Enter the mail id');
	}
	else
	{
		//mail = $('mailid').value;
		mail=document.getElementById('mailid').value;
		var url = '../functions/addmails.php';
		var par ="mail="+mail;
		var testimonyObj = new Ajax.Request(url,
							{
								method:'post',
								parameters:par,
								onComplete :showRespMsg
							});	
	}
}

function showRespMsg()
{
		names = document.getElementById('mailid').value.split('@');
		document.getElementById('showMsg').innerHTML = "Thanks for signing up!";
        document.getElementById('mailid').value="E-mail Id";
		
}


function showShippingTimeSheet()
{
	window.open('../shipping/timesheet.html',"TimeSheet","width=695,height=200");
	
}

function changeCost()
{
	var mylist=document.getElementById("shipping")
	var shipcst =mylist.options[mylist.selectedIndex].value;
	var mylist=document.getElementById("shipping")
	//document.getElementById("shippingCost").innerHTML='Rs ' + mylist.options[mylist.selectedIndex].value;
	
	
	totalGrams = $('totalGrams').value;
	//shippingCost = mylist.options[mylist.selectedIndex].value;
	//alert(totalGrams);
	gramsToKg = (totalGrams/500);
	//alert(gramsToKg);
	StrGrams = gramsToKg.toString();
	//alert(StrGrams);
	kgVal = StrGrams.split('.');
	//alert(kgVal);
	if(kgVal[1]<=9 && kgVal[0]==0)
	{
		kgVal[0] = 1;
		kgVal[1] = 0;
	}
	else
	{
		kgVal[0] = parseInt(kgVal[0])+1;
		kgVal[1] = 0;
	}

	totgrams = kgVal[0]+'.'+kgVal[1];
	//alert(totgrams);
	$('shippingCost').innerHTML = parseInt(shipcst)*totgrams;
	$('total').innerHTML = parseInt($('subtotal').value)+(parseInt(shipcst)*totgrams);
	$('Amount').value = parseInt($('subtotal').value)+(parseInt(shipcst)*totgrams);
}

function checkShipping()
{
	var mylist=document.getElementById("shipping")
	var shipcst =mylist.options[mylist.selectedIndex].value;
	if(parseInt(shipcst))
		document.forms["checkout_form"].submit();
	else
		alert("Please select your shipping region");
}
showTooltip = function(e)
{
	$('tooltip').innerHTML ='Click for product view';
	
	/*if (document.all)
				event = window.event;
			
			posx = event.pageX ;
			posy = event.clientY;
		*/
	if(!e)var e = window.event;
	if (e.pageX || e.pageY) 	{//This is for FIrefox & opera
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{//This is for IE
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	posx +=8;
	posy +=15;
	$('tooltip').style.display = "block";
	$('tooltip').style.top = posy + "px";
	$('tooltip').style.left = posx + "px";
	
}

hideTooltip = function()
{
	$("tooltip").style.display = "none";
}

function showPopular()
{
	var url = '../functions/showPopular.php';
	var par ="";
	var sloganObj = new Ajax.Request(url,
						{
							method:'post',
							parameters:par,
							onComplete :loadPopular
						});	
}

function loadPopular(respPopular)
{
	var imagepath = respPopular.responseText;
	//$("popular").innerHTML =imagepath;
	document.getElementById("popular").innerHTML =imagepath;
}
function chkNumber(objValue)
{
	val = objValue.value;
	if(isNaN(val))
	{
		alert('Please enter a number!');
		objValue.value="";
	}
}


function addmore( usermailid, apppath, productid, productType, productStyle, productColor, currentLoad ,orderId){

	var namevalue = "";
	var e=document.getElementsByTagName("input");
	for(var i=0;i<e.length;i++){
		
		var textBoxName = e[i].name;
		
		if( e[i].type == "text" && textBoxName.indexOf('design') !=-1 )
		{
			//alert( e[i].name + "-" + e[i].value );
			namevalue = namevalue +  e[i].name + "-" + e[i].value + ",";
		}
	}
	
	var url = "showcart/addmore.php?uid="+productid+"&type="+productType+"&style="+productStyle+"&color="+productColor+"&cartuserid="+usermailid+"&currentload="+currentLoad+"&sessionQtyNameValue="+namevalue+"&order_id="+orderId;
	window.location.href=apppath + url;

	
	/*document.getElementById('uid').value = productid;
	document.getElementById('type').value = productType;
	document.getElementById('style').value = productStyle;
	document.getElementById('color').value = productColor;
	document.getElementById('cartuserid').value = usermailid;
	document.getElementById('currentload').value = currentLoad;
	document.getElementById('mode').value = "addnewitems";	
	document.showcart.action=apppath+"showcart/index.php";
	document.showcart.method="GET";
	document.showcart.submit();*/
	
} 

function addmoreCartItems( usermailid, apppath, productid, productType, productStyle, productColor,orderId){

	var product_type = productType;
	if( document.getElementById('producttype')){
		product_type = document.getElementById('producttype').value;
	}
	var product_style = productStyle;
	if( document.getElementById('productstyle')){
		product_style = document.getElementById('productstyle').value;
	}
	var product_color = productColor;
	
	if( document.getElementById('productcolor')){
		product_color = document.getElementById('productcolor').value;
	}
	var url = "showcart/addmoreitems.php?uid="+productid+"&type="+product_type+"&cartuserid="+usermailid+"&style="+product_style+"&color="+product_color+"&method=add&order_id="+orderId;
	//alert( url );
	var namevalue = "";
	var e=document.getElementsByTagName("input");
	for(var i=0;i<e.length;i++){
		
		var textBoxName = e[i].name;
		
		if( e[i].type == "text" && textBoxName.indexOf('design') !=-1 )
		{
			//alert( e[i].name + "-" + e[i].value );
			namevalue = namevalue +  e[i].name + "-" + e[i].value + ",";
		}
	}
	var url = "showcart/addmoreitems.php?uid="+productid+"&type="+product_type+"&cartuserid="+usermailid+"&style="+product_style+"&color="+product_color+"&method=add&sessionQtyNameValue="+namevalue+"&order_id="+orderId;
	//alert( url );
	window.location.href=apppath + url;
	
	/*var product_type = productType;
	if( document.getElementById('producttype')){
		product_type = document.getElementById('producttype').value;
	}
	var product_style = productStyle;
	if( document.getElementById('productstyle')){
		product_style = document.getElementById('productstyle').value;
	}
	var product_color = productColor;
	
	if( document.getElementById('productcolor')){
		product_color = document.getElementById('productcolor').value;
	}
	
	document.getElementById('uid').value = productid;
	document.getElementById('type').value = product_type;
	document.getElementById('style').value = product_style;
	document.getElementById('color').value = product_color;
	document.getElementById('cartuserid').value = usermailid;
	//document.getElementById('currentload').value = currentLoad;
	document.getElementById('mode').value = "add";	
	document.showcart.action=apppath+"showcart/index.php";
	document.showcart.method="GET";
	document.showcart.submit();*/
	
	
} 

function deletecartItems( apppath, prodid, orderNew, color, cartItems, productId, deletRefShowcart){

	var namevalue = "";
	var e=document.getElementsByTagName("input");
	for(var i=0;i<e.length;i++){
		
		var textBoxName = e[i].name;
		
		if( e[i].type == "text" && textBoxName.indexOf('design') !=-1 )
		{
			namevalue = namevalue +  e[i].name + "-" + e[i].value + ",";
		}
	}
	var url = "showcart/?prodid="+prodid+"&mode=DEL&" + orderNew +"&designFlag=design&color="+color+"&deleteCartItem="+cartItems+"&productid="+productId+"&sessionQtyNameValue="+namevalue+"&deletRefShowcart="+deletRefShowcart;	
	//alert(url);
	window.location.href=apppath + url;
}

function changeCombo(obj,apppath)
{
	//alert( obj.id );
	if( obj.id == 'producttype' ){
		document.getElementById('typesreload').value = 'yes';
	}
	//var url = "showcart/index.php?style="+obj.value;
	//window.location.href=apppath + url ;
	document.showcart.action=apppath+"showcart/index.php";
	document.showcart.method="GET";
	document.showcart.submit();
}

function checkOutSubmit(apppath,orderId,rowCount,rowColCount)
{


	// Added by James
	
	var namevalue = "";
	var e=document.getElementsByTagName("input");
	for(var i=0;i<e.length;i++){
		
		var textBoxName = e[i].name;
		
		if( e[i].type == "text" && textBoxName.indexOf('design') !=-1 )
		{
			namevalue = namevalue +  e[i].name + "-" + e[i].value + ",";
		}
	}

	var url = apppath+"showcart/showcart.php?"+orderId+"&sessionQtyNameValue="+namevalue;

	//alert(rowCount+" , Row Col Count :"+rowColCount);
	var rows = rowColCount.tokenize(":"," ", true);
	for(i=0;i<rows.length;i++)
	{
		//alert(rows[i]);
		var currentRow = rows[i].substring(0,rows[i].indexOf(','));
		var numOfCol = rows[i].substring(rows[i].indexOf(',')+1);
		//alert('currentRow :'+currentRow+ "& No of Col :"+numOfCol);
		var colArray=new Array();
		for(j=1;j<=numOfCol;j++)
		{
			var txtValue = document.getElementById(currentRow+'qtyTxt'+j).value;
			if(txtValue!="" && txtValue!=0)
				colArray[colArray.length] = j;
		}
		
		//alert("colArray :"+colArray);
		
		if(colArray=="")
		{
			alert("please enter quantity information to proceed !");
			return;
		}
	}

	document.showcart.action = url;
	document.showcart.submit();
}

String.prototype.tokenize = tokenize
function tokenize()
{
 var input             = ""
 var separator         = " "
 var trim              = ""
 var ignoreEmptyTokens = true
 try {
   String(this.toLowerCase())
 }
 catch(e) {
	return
 }
 if(typeof(this) != "undefined")
		 input = String(this)
 if(typeof(tokenize.arguments[0]) != "undefined")
		 separator = String(tokenize.arguments[0])
 if(typeof(tokenize.arguments[1]) != "undefined")
		 trim = String(tokenize.arguments[1])
 if(typeof(tokenize.arguments[2]) != "undefined")
   {
	  if(!tokenize.arguments[2])
		ignoreEmptyTokens = false
   }
 var array = input.split(separator)
 if(trim)
   for(var i=0; i<array.length; i++)
	 {
	   while(array[i].slice(0, trim.length) == trim)
		 array[i] = array[i].slice(trim.length)
	   while(array[i].slice(array[i].length-trim.length) == trim)
		 array[i] = array[i].slice(0, array[i].length-trim.length)
	 }
 var token = new Array()
 if(ignoreEmptyTokens)
   {
	  for(var i=0; i<array.length; i++)
		if(array[i] != "")
		  token.push(array[i])
   }
 else
	 token = array
 return token
}

