// onload start the function Start to create DOM element  
if(FastInit) {  
	FastInit.addOnLoad(StartIt);  
} else {  
	Event.observe(window, 'load', StartIt);  
}  

var SiteUrl = ''

function StartIt()  
{  
	MM_preloadImages('/files/others/homebutroll1.gif','/files/others/productsroll1.gif','/files/others/buyersguideroll1.gif','/files/others/orderroll1.gif','/files/others/checkoutroll1.gif','/files/others/aboutroll1.gif')
	
	// show customers order and update the order div
	if($('CustomerOrders') != null)
	{
	var mem_id = $('Customer').value;
	new Ajax.PeriodicalUpdater('CustomerOrders',
			'/Customer-Home/Customer-Log-in.html?Customer_home=Orders&option=customer&mem_id='+mem_id,
			{
				method: 'get',
				frequency: 10,
				decay: 2
			});
	}
	
//+----------------------------------------------------------------------------------------------------------

	// for secure pay tech onload reset form and do not allow customer to click browser back button
	if($('cardform') != null)  
	{  
		document.cardform.reset(); window.history.forward(1);  
	}  

//+----------------------------------------------------------------------------------------------------------


	// on product page onclick rating do it
	if($("RatingButton") != null)
	{		
		$("Ratings").innerHTML = 'Loading...';
		var prod_id = document.BuyaProduct.prod_id.value;
		UpdateDiv("Ratings" , "/system/Ajax/product_page.php?UpdateRating=True&prod_id="+prod_id);
	
		if($('RatingButton') != null)
		{
			$('RatingButton').onkeypress = kH;
			if (document.layers) document.captureEvents(Event.KEYPRESS);
			//Event.observe($('RatingButton'), 'click', function(event){ doRateProduct(); return false; });
		}
	} 

//+----------------------------------------------------------------------------------------------------------

	// function to send the Review
	if($("ReviewButton") != null)
	{
		$("Review").innerHTML = 'Loading...';
		var prod_id = document.BuyaProduct.prod_id.value;
		UpdateDiv("Review" , "/system/Ajax/product_page.php?UpdateReviews=True&prod_id="+prod_id);

		Event.observe($('RShowHide'), 'click', function(event){ Reviews() });	

		$('ReviewButton').onkeypress = kH;
		if (document.layers) document.captureEvents(Event.KEYPRESS);
		//Event.observe($('ReviewButton'), 'click', function(event){ doReviewProduct(); return false; });
	} 

}   

//----------------------------------------------------------------------------------------------------------*  
// the following are the functions used in the script above..................................................  
//----------------------------------------------------------------------------------------------------------*  

// product page functions
//+----------------------------------------------------------------------------------------------------------

function MM_preloadImages() { //v3.0
 var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
 var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
 if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
 var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
 d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
 if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
 for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
 if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImgRestore() { //v3.0
 var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
 var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
 if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function openWin(myWin)
{
myWin = open(myWin, "displayWindow1", "width=400,height=580,scrollbars=no,resizable=yes,status=no,toolbar=no,menubar=no");
}

	// show/hide product reviews
	function Reviews()
	{
		LinkTxt = $('RShowHide').innerHTML;
		if(LinkTxt == "Show")
		{
			$('RShowHide').innerHTML = "Hide";

			var prod_id = document.BuyaProduct.prod_id.value;
			UpdateDiv("Review" , "/system/Ajax/product_page.php?UpdateReviews=True&prod_id="+prod_id);

			new Effect.BlindDown('Review', {duration:0.5}); 
		}
		else
		{
			new Effect.BlindUp('Review', {duration:0.5});  
			$('RShowHide').innerHTML = "Show";
		}
	}

//+----------------------------------------------------------------------------------------------------------

	function doReviewProduct()
	{		
		var OldValue = $('ReviewButton').value;
		$('ReviewButton').disabled=true;
		$('ReviewButton').value= "Reviewing...";

		new Ajax.Request("/system/Ajax/product_page.php",
		{
		method:'post',
		parameters: $("ReviewForm").serialize(true) ,
			onSuccess: function(transport)
			{
				var response = transport.responseText || "Something went wrong.";
				$('ReviewButton').disabled = false;  
				$('ReviewButton').value= OldValue;

				alert(response); 
				$("Review").innerHTML = 'Loading...';
				var prod_id = document.BuyaProduct.prod_id.value;
				UpdateDiv("Review" , "/system/Ajax/product_page.php?UpdateReviews=True&prod_id="+prod_id);
			},

			onFailure: function()
			{ 
				$('ReviewButton').disabled = false;
				$('ReviewButton').value= OldValue;
				alert("Something went wrong."); 
			}
		});
	return false;
	}
	
//+----------------------------------------------------------------------------------------------------------

	function doRateProduct()
	{		
		var prod_id = document.RatingForm.prod_id.value;
		var OldValue = $('RatingButton').value;
		$('RatingButton').disabled=true;
		$('RatingButton').value= "Rating...";

		new Ajax.Request("/system/Ajax/product_page.php",
		{
		method:'post',
		parameters: $("RatingForm").serialize(true) ,
			onSuccess: function(transport)
			{
				var response = transport.responseText || "Something went wrong.";
				UpdateDiv("Ratings" , "/system/Ajax/product_page.php?UpdateRating=True&prod_id="+prod_id);

				$('RatingButton').disabled = false;  
				$('RatingButton').value= OldValue;

				alert(response); 

				UpdateDiv("Ratings" , "/system/Ajax/product_page.php?UpdateRating=True&prod_id="+prod_id);
			},

			onFailure: function()
			{ 
				$('RatingButton').disabled = false;
				$('RatingButton').value= OldValue;
				alert("Something went wrong."); 
			}
		});
	}

//+----------------------------------------------------------------------------------------------------------
// payment page functions
//+----------------------------------------------------------------------------------------------------------

	function ShowPaymentMethod(PaymentId)
	{
	
		//var x = findPosX($('PaymentDiv')) 
		//var y = findPosY($('PaymentDiv')) 
		//$('PaymentAniDiv').style.position = 'absolute';
		//$('PaymentAniDiv').style.top      = y + 'px';
		//$('PaymentAniDiv').style.left     = x + 'px';
		//$('PaymentAniDiv').style.display = "block";
		new Effect.Appear('PaymentAniDiv');

		new Ajax.Request('/system/Ajax/checkout_proccess.php',
		{
		method:'post',
		parameters: {PaymentKey:'ShowPayment' , PaymentId : PaymentId} ,
			onSuccess: function(transport)
			{
				var response = transport.responseText || "Something went wrong. Please try again.";
				$('PaymentAniDiv').hide();
				$('PaymentDiv').innerHTML = response; 
			},

			onFailure: function()
			{ 
				alert("Something went wrong. Please try again."); 
			}
		});
	}

//+----------------------------------------------------------------------------------------------------------

	function ProcessOfflinePayment(Btn , Frm , PaymentId)
	{
		var OldValue = $(Btn).value;
		$(Btn).disabled=true;
		$(Btn).value= "Processing...";

		new Ajax.Request('/system/Ajax/payment_process.php',
		{
		method:'post',
		parameters: $(Frm).serialize(true) ,
			onSuccess: function(transport)
			{
				var response = transport.responseText || "Something went wrong.";
				$(Btn).disabled = false;  
				$(Btn).value= OldValue;
				if(response != "Yes")
				{
					alert(response); 
				}
				else
				{
					window.location='/Thank-You.html?PaymentType=' + PaymentId
					//alert('Yes')
				}

			},

			onFailure: function()
			{ 
				$(Btn).disabled = false;
				$(Btn).value= OldValue;
				alert("Something went wrong."); 
			}
		});
	}

//+----------------------------------------------------------------------------------------------------------

	function ProcessOnlinePayment(Btn , Frm , PaymentId)
	{
		if(Btn == "securePayTechBtn")
		{
			document.securePayTechForm.CardExpiry.value=document.securePayTechForm.ExpMon.value+""+document.securePayTechForm.ExpYear.value;
		}

		var OldValue = $(Btn).value;
		$(Btn).disabled=true;
		$(Btn).value= "Processing...";

		new Ajax.Request('/system/Ajax/payment_process.php',
		{
		method:'post',
		parameters: $(Frm).serialize(true) ,
			onSuccess: function(transport)
			{
				var response = transport.responseText || "Something went wrong.";
				$(Btn).disabled = false;  
				$(Btn).value= OldValue;
				if(response != "Yes")
				{
					var is_err = response.indexOf('-');
					//alert(is_err)
					if (is_err == 0)
					{ 
					 	 alert(response);
					}
					else
					{
						$('PaymentDiv').innerHTML = response; 
					}
				}
				else
				{
					window.location='/Thank-You.html?PaymentType=' + PaymentId
					//alert('Yes')
				}

			},

			onFailure: function()
			{ 
				$(Btn).disabled = false;
				$(Btn).value= OldValue;
				alert("Something went wrong."); 
			}
		});
	}

//+----------------------------------------------------------------------------------------------------------
// cart page functions
//+----------------------------------------------------------------------------------------------------------

	function CartSubmition()
	{
		var oldBtn = $('ToCheckout').value;
		$('ToCheckout').value = "Proccessing...";
		new Ajax.Request('/system/Ajax/checkout_proccess.php',  
		{  
			method:'post',  
			parameters: {check:'cartToCheckout'} ,  
			onSuccess: function(transport)  
			{  
				var response = transport.responseText || "Something went wrong. Please check your cart details.";  
			
				if(response == "Yes")
				{
					window.location= "Checkout.html";
				}
				else
				{
					alert(response);
				}
				$('ToCheckout').value = oldBtn;
			},  

			onFailure: function(){ $('ToCheckout').value = oldBtn; aler("Something went wrong. Please try again later."); }  
		});  
	}
	
//+----------------------------------------------------------------------------------------------------------

	function RemoveItemInCart(id)  
	{  
		window.location = '/Cart.html?RemoveItemInCart=True&id='+id;  
	}  
 
	function UpdateCartQty(FieldId , FieldValue)  
	{  
		window.location = '/Cart.html?UpdateCartItemQty=True&id='+FieldId+'&qty='+FieldValue;  
	}  

//+----------------------------------------------------------------------------------------------------------
// checkout page functions
//+----------------------------------------------------------------------------------------------------------

	function ShippingSameBilling(What)  
	{  
		var bill = $('billFields').getElementsByTagName('input');  
		var ship = $('shipFields').getElementsByTagName('input');  

		if(What == "yes")  
		{  
			for(i=0; i<bill.length; i++)  
			{  
				ship[i].value = bill[i].value;  
			}  
		}  
		else  
		{  
			for(i=0; i<bill.length; i++)  
			{  
				ship[i].value = "";  
			}  
		}  
	}  

//+----------------------------------------------------------------------------------------------------------
// customer page functions
//+----------------------------------------------------------------------------------------------------------

  	function ShowOrderDetailsForCustomer(order_id)
  	{
			window.open('ViewOrder.html?TheOrder='+order_id , 'myorderdetails' , 'resizable=no, scrollbars=yes');
  	}
 
//+----------------------------------------------------------------------------------------------------------
// other functions
//+----------------------------------------------------------------------------------------------------------

	function UpdateDiv(DivName , Url)
	{
		new Ajax.Updater(DivName, Url,
		{
			method: 'get'
		});
	}

	function paymark_verify(merchant)
	{
	window.open('http://paymark.co.nz/dart/darthttp.dll?etsl&tn=verify&merchantid='

	+ merchant, 'verify' , 'scrollbars=yes , width=400 , height=400');
	}

	function ResizeImage( imgwidth ,imgheight  , maxwidth , maxheight , type)  
	{  
	  if (imgwidth > maxwidth)
	  {
		  ration = maxwidth/imgwidth;
		  newwidth = Math.round(imgwidth*ration);
		  newheight = Math.round(imgheight*ration);
		  if (newheight > maxheight)
		  {
			  ration = maxheight/newheight;
			  newwidth = Math.round(newwidth*ration);
			  newheight = Math.round(newheight*ration);

				if(type == 'w')
					return newwidth;
				else
					return newheight;
		  }
		  else
		  {
				if(type == 'w')
					return newwidth;
				else
					return newheight;
		  }
	  }
	  else if (imgheight > maxheight)
	  {
		  ration = maxheight/imgheight;
		  newwidth = Math.round(imgwidth*ration);
		  newheight = Math.round(imgheight*ration);
		  if (newwidth > maxwidth)
		  {
			  ration = maxwidth/newwidth;
			  newwidth = Math.round(newwidth*ration);
			  newheight = Math.round(newheight*ration);
				if(type == 'w')
					return newwidth;
				else
					return newheight;
		  }
		  else
		  {
				if(type == 'w')
					return newwidth;
				else
					return newheight;
		  }
	  }
	  else
	  {
				if(type == 'w')
					return imgwidth;
				else
					return imgheight;
	  }
	}

//+----------------------------------------------------------------------------------------------------------

	// others
	// please disabled all the enter key for a form (from the internet)
	function kH(e) {
	var pK = e ? e.which : window.event.keyCode;
	return pK != 13;
	}

//+----------------------------------------------------------------------------------------------------------

	// X and Y of an element by by Peter-Paul Koch & Alex Tingle  
	function findPosX(obj)  
	{  
	var curleft = 0;  
	if(obj.offsetParent)  
		while(1)  
		{  
		  curleft += obj.offsetLeft;  
		  if(!obj.offsetParent)  
			break;  
		  obj = obj.offsetParent;  
		}  
	else if(obj.x)  
		curleft += obj.x;  
	return curleft;  
	}  

	function findPosY(obj)  
	{  
	var curtop = 0;  
	if(obj.offsetParent)  
		while(1)  
		{  
		  curtop += obj.offsetTop;  
		  if(!obj.offsetParent)  
			break;  
		  obj = obj.offsetParent;  
		}  
	else if(obj.y)  
		curtop += obj.y;  
	return curtop;  
	}  