// JavaScript Document

var divVisible = 0;
var divTimeout = 0;
var divSubTimeout = 0;
var divStockTimeout = 0;
var menuHeight = 3;
var menuWidth = 4;
var divCatVisible = 0;
var divCatVisible2 = 0;
var lastmenu;
var lastSubMenu;
var lastDivMenuVisible = 0;
var selectedCat;

function MM_jumpMenu2(targ,selObj,restore)
{ 
    selectedCat = selObj.options[selObj.selectedIndex].value;
    
    if (selectedCat == "#")
        hideDiv('jumpMenu');
    else   
        showDiv('jumpMenu');
}

function stripSpecialChars(string) {
	marks = "/:?'<>|\\";
	str = string;

	for (i = 0; i < marks.length; i++) {

		mark = marks.charAt(i);

		while (str.indexOf(mark) != -1) {
			point = str.indexOf(mark);
			first_part = str.substring(0, point);
			second_part = str.substring(point + 1, str.length);
			str = first_part + second_part;
		}
	}
	return str;
}

function redirectPDF(targ, url) {
    url = selectedCat+url+".pdf";
    url = stripSpecialChars(url);
    window.open("products_pdfs/"+url);
}

function showDivSubMenu(obj, div) {       	
    hideDivNow(lastSubMenu);
    clearTimeout(divSubTimeout);
    if (document.getElementById && document.getElementById(div)!= null)
	{   
		var style2 = document.getElementById(div).style;
		var leftMargin = 0;
		var topMargin =20;
	    var width = 30;
	    
			
	    if (style2.display == "none" || style2.display == "")
		{
			style2.display = "block";
		}
		else
		{
			style2.display = "none";
		}
		
		

		lastSubMenu = div;
		menuPos =  findPos (obj); // (mouseX(event) - leftMargin);
		style2.left = (menuPos[0] + leftMargin) + "px";
		style2.top = (menuPos[1] + topMargin) + "px";
		
		
	}
}

function hideDivSubMenu(div) {
  divSubTimeout = setTimeout('hideDivNow("'+div+'")',300);
}

function showDivStockDescription(obj, div) {              
    obj = document.getElementById(obj);
    
    if (document.getElementById && document.getElementById(div)!= null)
	{   
		var style2 = document.getElementById(div).style;
		var leftMargin = 100;
		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 showMainDiv(div, catId)
{
    hideMainDiv(lastDivMenuVisible);
    if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		style2.display = "block";
		style2.width = (155 * menuWidth)-35+"px";
		style2.height = (90 * menuHeight)-40+"px";
		divCatVisible = catId;
		lastDivMenuVisible = div;
		showCat(catId);
	} 
}

function showSubMainDiv(div, mainCat)
{
    hideDivNow(divCatVisible2);
    if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		style2.display = "block";				
		
		subHeight = document.getElementById(div).offsetHeight;
		mainHeight = document.getElementById(mainCat).offsetHeight;
		
	    topMargin = Math.abs(((mainHeight-40) - subHeight) /2);	
		style2.width = (155 * (menuWidth-1))+"px";
		
		style2.marginTop = topMargin;	
		divCatVisible2 = div;
		showCat(div);
	} 
}

function showCat(div)
{
    if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		style2.display = "block";
	}
}

function hideMainDiv(div)
{
    hideDivNow(div); // main 
	hideDivNow(divCatVisible); // categories
	hideDivNow(divCatVisible2); // subcategories
}

function hideSubMainDiv(div)
{
    hideDivNow(div); // main 
	hideDivNow(divCatVisible2); // categories
}


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 hideDivNow(div) 
{    
	if (document.getElementById && document.getElementById(div)!= null)
	{
	    divVisible = 0;
		var style2 = document.getElementById(div).style;
		style2.display = "none";	
	}
}


function showMenuDiv(div)
{
 clearTimeout(divTimeout);
 if (!divVisible) // only if div is not visible
 {
    divVisible = 1;  
        
	if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		style2.display = "block";
	}
 }
}

function showMenu(obj, div)
{   

    obj = obj.offsetParent;
    if (lastmenu == div)
       clearTimeout(divTimeout); 
    else 
       hideDivNow(lastmenu);
   
	if (!divVisible) // only if div is not visible
    {      
        divVisible = 1; // flags div as visible
        lastmenu = div; // stores last menu visited (this one)
        
	    if (document.getElementById)
	    {
		    var leftMargin = 160;
		    var topMargin = -2;
		    var style2 = document.getElementById(div).style;
		    style2.display = style2.display ? "" : "block";
		    menuPos =  findPos (obj); // (mouseX(event) - leftMargin);
		    style2.left = (menuPos[0] + leftMargin) + "px";
		    style2.top = (menuPos[1] + topMargin) + "px";
	    }
	}
}

function hideMenuDiv(div)
{
   divTimeout=setTimeout('hideDivNow("'+div+'")',500);
}

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 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 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");
}

