// display decision alert box
function decision(message, url){
	if(confirm(message)) location.href = url;
}

// open browser window
function openPopUp(url, windowName, w, h, scrollbar) {

           var winl = (screen.width - w) / 2;
           var wint = (screen.height - h) / 2;
           winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scrollbar ;
		   win = window.open(url, windowName, winprops);
           if (parseInt(navigator.appVersion) >= 4) { 
              	win.window.focus(); 
           } 
}

function jumpMenu(target,object,restore){ 
  eval(target+".location='"+object.options[object.selectedIndex].value+"'");
  if (restore) object.selectedIndex=0;
}

function findObj(n, d) {
  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=findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function getImage(imageName)
{
		findObj('img').src = imageName;
}

function submitDoc(formName) { 
  var obj;
 
	if (obj=findObj(formName)!=null) 
	{
		findObj(formName).submit(); 
	}
	else 
	{
		alert('The form you are attempting to submit called \'' + formName + '\' couldn\'t be found. Please make sure the submitDoc function has the correct id and name.');
	}

}
/*
  -------------------------------------------------------------------
  Added by Luka ;)
  Mail functions
  -------------------------------------------------------------------
*/

function mail_build(host,name) {
	var at = "@";
	return name+at+host;
}

function mail_link(host,name,label,attributes) {
	address = mail_build(host,name);
	var mt = "mailto:";
	if (label == "") {
		label = address;
	}
	return '<a href="'+mt+address+'" '+attributes+' >'+label+'</a>';
}

function show_help(eltId) {
    var helpElt = document.getElementById(eltId);
	
    if (document.layers) {
        if (helpElt.visibility == "show") {
            helpElt.visibility = "hide";
            helpElt.display = "none"; 
        }
        else {
            helpElt.visibility = "show";
            helpElt.display = "block";
        }
    }
    else {
        if (helpElt.style.visibility == "visible") {
            helpElt.style.visibility = "hidden";
            helpElt.style.display = "none";
        }
        else {
            helpElt.style.visibility = "visible";
            helpElt.style.display = "block";
        }    
    }
}
/*
---------------------------------------------------------------------
*/

function setTextOfLayer(objName,x,newText) { 
  if ((obj=findobj(objName))!=null) with (obj)
    if (document.layers) {document.write(unescape(newText)); document.close();}
    else innerHTML = unescape(newText);
}

function formatAsMoney(mnt) { 
    mnt -= 0; 
    mnt = (Math.round(mnt*100))/100; 
    //return (mnt == Math.floor(mnt))? mnt + '.00' : ( (mnt*10 == Math.floor(mnt*10))? mnt + '0' : mnt); 
	nStr = mnt + '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = (x.length <= 1) ? ',00' : ((x[1].length == 1) ? ','+x[1]+'0' : ','+x[1]);

	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return x1 + x2;
}

function computePrice(price,priceSec) {
	var str= "";
	var newPrice = 0;
	var optPrice = '';
	var priceAdded = false;
    var priceField = document.getElementById(price); 
    var priceSecField = document.getElementById(priceSec); 
    var oldHTML = '';        
    
    var strPrice = 0;

	for(var i=0; i<document.addtobasket.elements.length; i++) {
		if (document.addtobasket.elements[i].name == 'productOptions[]') {
			str = str + "\n - " +document.addtobasket.elements[i].name+" : "+document.addtobasket.elements[i].value;
			optPrice = prices[document.addtobasket.elements[i].value];
			strPrice += "\n*"+optPrice+"*";
			
            if (!(optPrice == null)) {              
                var ch = optPrice.charAt(0);
                strPrice += "\n"+ch+") ";
				if(ch == '+') {
					newPrice = newPrice + parseFloat(optPrice.substring(1));
                    strPrice += "1-#"+optPrice.substring(1)+"#";
				}
				else if (ch == '-') {
					newPrice = newPrice - parseFloat(optPrice.substring(1));
                    strPrice += "2-#"+optPrice.substring(1)+"#"; 
				}
				else if (ch == '=') {
					newPrice = newPrice + parseFloat(optPrice.substring(1));
					priceAdded = true;
                    strPrice += "3-#"+optPrice.substring(1)+"#"; 
				}
                strPrice += " NP:" + newPrice+"\n";		
			}			
		}
	}
	if (!priceAdded) {
		newPrice = newPrice + parseFloat(startPrice);
	}
    
   // alert(newPrice);

    priceField.innerHTML = changeNumber(priceField.innerHTML,formatAsMoney(newPrice));
    priceSecField.innerHTML = changeNumber(priceSecField.innerHTML,formatAsMoney(newPrice*secCurrVal));
	//alert(str+"\n"+strPrice+"\n\n:"+formatAsMoney(newPrice));	
	//alert(document.getElementById('productOptions[]'));	
}

/*
    Changes number between currencies for examle... 
*/
function changeNumber(oldText, newText) {
    var left = '';
    var right = '';    
    for (i=0; i<oldText.length; i++) {
        if (/^\d$/.test(oldText.charAt(i))) {
            left = oldText.substring(0,i);
            break;
        }
    }
    for (i=oldText.length-1;i>=0; i--) {
        if (/^\d$/.test(oldText.charAt(i))) {
            right = oldText.substring(i+1);
            break;
        }
    }  
    return left + newText + right;  
}