// 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;
var actualDiv;
var actualMainCategorieDiv;

function setSWF(height, width, filename, bgcolor, flashVars) {
    document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="' + width + '"  height="' + height + '" id="banner" align="middle">');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="movie" value="banners_images/' + filename + '" />');
	document.write('<param name="quality" value="high" />');
	document.write('<param name="wmode" value="transparent" />');
	document.write('<param name="bgcolor" value="' + (bgcolor == null ? '' : bgcolor) + '" />');
	document.write('<param name="flashVars" value="' + (flashVars == null ? '' : flashVars) + '" />');
	document.write('<embed src="banners_images/' + filename + '"  quality="high" bgcolor="' + (bgcolor == null ? '' : bgcolor) + '" width="' + width + '"  height="' + height + '" wmode="transparent" flashVars="' + (flashVars == null ? '' : flashVars) + '" name="banner" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');

}

function setMenuSWF(height, width, filename, bgcolor, flashVars, id) {
    document.write('<object id="' + (id == null ? '' : id) + '" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="' + width + '"  height="' + height + '" id="menu" align="middle">');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="movie" value="' + filename + '" />');
	document.write('<param name="quality" value="high" />');
	document.write('<param name="align" value="TL" />');
	document.write('<param name="wmode" value="transparent" />');
	document.write('<param name="bgcolor" value="' + (bgcolor == null ? '' : bgcolor) + '" />');
	document.write('<param name="flashVars" value="' + (flashVars == null ? '' : flashVars) + '" />');
	document.write('<embed src="' + filename + '"  quality="high" bgcolor="' + (bgcolor == null ? '' : bgcolor) + '" width="' + width + '"  height="' + height + '" wmode="transparent" flashVars="' + (flashVars == null ? '' : flashVars) + '" id="' + (id == null ? '' : id) + '" name="banner" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');

}

function flash_spitter()
{
	
	document.write("<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\"");
	document.write("codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\"");
	document.write("width=\"584\" height=\"330\" id=\"solar\" align=\"middle\">");
    document.write("<param name=\"allowScriptAccess\" value=\"sameDomain\" />");
    document.write("<param name=\"movie\" value=\"solarporte.swf\" /><param name=\"quality\" value=\"high\" />");
    document.write("<param name=\"bgcolor\" value=\"#ffffff\" />");
    document.write("<param name=\"wmode\" value=\"opaque\" />");
    document.write("<embed src=\"solarporte.swf\" quality=\"high\" bgcolor=\"#ffffff\" width=\"584\" height=\"330\" name=\"solar\"");
    document.write("align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\"");
    document.write("pluginspage=\"http://www.macromedia.com/go/getflashplayer\"  wmode=\"opaque\"/>");
    document.write("</object>");
	
}

function minhoca()
{
	
	document.write("<object classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\"");
	document.write("codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0\"");
	document.write("width=\"252\" height=\"88\" id=\"solar\" align=\"middle\">");
    document.write("<param name=\"allowScriptAccess\" value=\"sameDomain\" />");
    document.write("<param name=\"movie\" value=\"minhoca.swf\" /><param name=\"quality\" value=\"high\" />");
    document.write("<param name=\"bgcolor\" value=\"#ffffff\" />");
    document.write("<param name=\"wmode\" value=\"opaque\" />");
    document.write("<embed src=\"minhoca.swf\" quality=\"high\" bgcolor=\"#ffffff\" width=\"252\" height=\"88\" name=\"solar\"");
    document.write("align=\"middle\" allowScriptAccess=\"sameDomain\" type=\"application/x-shockwave-flash\"");
    document.write("pluginspage=\"http://www.macromedia.com/go/getflashplayer\"  wmode=\"opaque\"/>");
    document.write("</object>");
	
}


function dd(obj) {
	alert(obj.selectedIndex);
	}

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 = 2;
		    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";
		   // alert(style2.top);
	    }
	}
}

function hideSubCategorieDiv()
{   
   showDiv(actualMainCategorieDiv);
   hideDivNow(actualDiv);   
   
}

function showCategorie(obj, div, parentDiv)
{   
    //acrescentar o algoritmo do Miguel
    showDiv(actualMainCategorieDiv);
    actualDiv = div;
    actualMainCategorieDiv = parentDiv;
    
    
    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 = 2;
		    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";
		    
	    }
	}
//	var style3 = document.getElementById(parentDiv).style;
//	style3.visibility = false;
	hideDivNow(parentDiv);
}

function hideMenuDiv(div)
{
   divTimeout=setTimeout('hideDivNow("'+div+'")',500);
}



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);
    
    //alert(obj);
    if (document.getElementById && document.getElementById(div)!= null)
	{   
		var style2 = document.getElementById(div).style;
		var leftMargin = 70;
		var topMargin = 1;
		style2.display = "block";	
		
		menuPos =  findPos (obj); // (mouseX(event) - leftMargin);
		style2.left = (menuPos[0] + leftMargin) + "px";
		style2.top = (menuPos[1] + topMargin) + "px";
//		alert(menuPos);
//		alert(style2.left);
//		alert(style2.top);
	}
}

function hideDivStockDescription(div) {
    clearTimeout(divStockTimeout);
    if (document.getElementById && document.getElementById(div)!= null)
	{
		var style2 = document.getElementById(div).style;
		style2.display = "none";
	}
}

function DoInstantMessage(person,screen)
{
	//Check if messenger is installed
	try{MsgrObj.classid="clsid:B69003B3-C55E-4B48-836C-BC5946FC3B28";}
	catch(e){if(!(e.number && 2148139390) == 2148139390)return;}
	
	//Check for logged in status
	if(MsgrObj.MyStatus == 1)
	{
		alert("bleh this is an error message about not being logged in");
		return;
	}
	
	//Check for duplicate contact
	try{var contact = MsgrObj.GetContact(person,"");}
	catch(e)
	{
		if((e.number && 2164261642) == 2164261642) //MSGR_E_USER_NOT_FOUND
		{
			if(confirm("Add "+screen+" to your contact list?")==true)MsgrObj.AddContact(0,person);
		}
	}
	
	//Send an instant message?
	if(confirm("Send "+screen+" an instant message?")==true)MsgrObj.InstantMessage(person);
}

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 = targ+".pdf";
    url = stripSpecialChars(url);
    //alert(url);
    window.open("products_pdfs/"+url);
}


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");
}

function formatNumber(number, decimalPlaces)
{
    if (decimalPlaces == null)
    {
        decimalPlaces = 2;
    }

    var temp = number.toFixed(decimalPlaces).replace(".", ",");
    var res = "";

    var idx = ((temp.indexOf(",") == -1)? temp.length : temp.indexOf(","));

    for (var i = 0; i < idx; i++)
    {
        if (((idx - i) % 3) == 0 && i != 0)
        {
            res += ".";
        }
        res += temp.charAt(i);
    }

    res += ",";
    
    for (var i = idx + 1; i < temp.length; i++)
    {
        res += temp.charAt(i);
    }

    var zeros = new Array(decimalPlaces);
    for (var i = 0; i < (decimalPlaces + 1); i++)
    {
        var sZeros = "";
        for (var j = 0; j < (decimalPlaces - i); j++)
        {
            sZeros += "0";
        }
        zeros[i] = sZeros;
    }
    res += zeros[temp.length - idx - 1];
    
    return res;
}
