// 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);
	if( isNaN(resString))
	{
		$("productNotifier").innerHTML = 'Product already added to cart';
	}
	else
	{
		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];
}

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('$$');
	$('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($('mailid').value =="E-mail Id" || $('mailid').value=='')
	{
		alert('Please Enter the mail id');
	}
	else
	{
		mail = $('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 = ($('mailid').value).split('@');
		$('showMsg').innerHTML = "Thanks for signing up!";
        $("mailid").value="E-mail Id";

}


function showShippingTimeSheet()
{
	window.open('../shipping/timesheet.html',"TimeSheet","width=695,height=200");

}

function changeCost(clearDiscount)
{
	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);

    //set initial discount value as "0"
    $('discount').innerHTML = parseInt(0);
    $('discAmount').value = parseInt(0);
    document.getElementById("promoCode").value = "";
    document.getElementById("discountDate").value = "";
    if(clearDiscount == true)
    {
        $('promoInput').value = "#N/A";
        $('promoButton').disabled = false;
    }

}

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)
{

	imagepath = respPopular.responseText;
	$("popular").innerHTML =imagepath;
}
function chkNumber(a)
{
	val =document.getElementById(a).value;
	if(isNaN(val))
	{
		alert('Please enter a number!');
		document.getElementById(a).value="";
	}
}
