// JavaScript Document
var divStockTimeout = 0;

function findPos(obj) {
	var posleft = postop = 0;
	if (obj.offsetParent)
	{
		posleft = obj.offsetLeft
		postop = obj.offsetTop
		while (obj = obj.offsetParent)
		{
			posleft += obj.offsetLeft
			postop += obj.offsetTop
		}
	}
	return [posleft,postop];
}

function showDivStockDescription(obj, div) {   
    obj = document.getElementById(obj);

    if (document.getElementById && document.getElementById(div)!= null)
	{   
		var style2 = document.getElementById(div).style;
		var leftMargin = 77;
		var topMargin = -10;
		style2.display = "block";	
		menuPos =  findPos (obj); // (mouseX(event) - leftMargin);
		style2.left = (menuPos[0] + leftMargin) + "px";
		style2.top = (menuPos[1] + topMargin) + "px";
	}
}

function hideDivStockDescription(div) {
    clearTimeout(divStockTimeout);
    if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		style2.display = "none";
	}
}

function showHideDiv(div)
{
	if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		if (style2.display == "none" || style2.display == "")
		{
			style2.display = "block";
		}
		else
		{
			style2.display = "none";
		}
	}
}

function hideDiv(div)
{
	if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		style2.display = "none";
	}
}

function showDiv(div)
{
	if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		style2.display = "block";
	}
}

function MM_jumpMenu(targ,selObj,restore)
{ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function showShippingMode(targ,selObj,restore)
{
	var sId = selObj.options[selObj.selectedIndex].value;
	for (i=0; i< selObj.options.length; i++)
	{
		var id = selObj.options[i].value;
		hideDiv("shippingDiv_" + id);	
		hideDiv("paymentDiv_" + id);	
	}
	if (sId != null && sId != "0")
	{
		showHideDiv("shippingDiv_" + sId);
		showHideDiv("paymentDiv_" + sId);
	}
	if (sId == "0")
	{
		showHideDiv("shippingDiv_0");
		showHideDiv("paymentDiv_0");
	}
}

function validateOrder()
{	
	if (document.getElementById)
	{
		var shipping = document.getElementById("shipping");
		if(shipping == null || shipping.value == "0")
		{
			showHideDivRedBorder("orderStep_2")
			showHideDiv("orderStep_2_error");
			return false;
		}
		else
		{
			var shipping = document.getElementById("shipping");
			var destination = document.getElementById("destination_" + shipping.value);
			if(destination == null || destination.value == "-1")
			{
				showHideDivRedBorder("orderStep_3")
				showHideDiv("orderStep_3_error");
				return false;
			}	
			
			var radioPayment = document.forms["orderForm"].elements["payment_" + shipping.value];
			var flagClear = false;
			if (radioPayment.length == null)
			{
				if (radioPayment.checked == true)
				{
					flagClear = true;
				}
			}
			else
			{
				for(var i = 0; i < radioPayment.length; i++) 
				{
					var	payment = radioPayment[i];
					if (payment.checked == true)
					{
						flagClear = true;
					}
				}
			}
			
			if (flagClear == false)
			{
				showHideDivRedBorder("orderStep_4");
				showHideDiv("orderStep_4_error");
				return false;
			}
			
			var termsAndConditions = document.forms["orderForm"].elements["termsAndConditions"];
			if (termsAndConditions.checked != true)
			{
				showHideDivRedBorder("orderStep_5");
				showHideDiv("orderStep_5_error");
				return false;
			}
		}
	}
	return true;
}

function showHideDivRedBorder(div)
{
	if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		if (style2.border == null || style2.border == "")
		{
			style2.border = "solid 2px #FF0000";
		}
		else
		{
			style2.border = "";
		}
	}
}

function hideDivRedBorder(div)
{
	if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		style2.border = "";
	}
}

function cleanStepAllStepsErrors()
{
	hideDivRedBorder("orderStep_2");
	hideDiv("orderStep_2_error");
	hideDivRedBorder("orderStep_3");
	hideDiv("orderStep_3_error");
	hideDivRedBorder("orderStep_4");
	hideDiv("orderStep_4_error");
	hideDivRedBorder("orderStep_5");
	hideDiv("orderStep_5_error");
}
