var xmlHttp

function GetXmlHttpObject() {
    var objXMLHttp=null
    if (window.XMLHttpRequest) {
        objXMLHttp=new XMLHttpRequest()
    } else if (window.ActiveXObject) {
        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
    }

    return objXMLHttp
}

function chDisplay( element ) {
    var e = document.getElementById(element);
    
    if( e.style.display == 'none' ) {
        e.style.display = 'block';
    } else {
        e.style.display = 'none';
    }
}

function chDisplayBlock( element ) {
    document.getElementById(element).style.display = 'none';
}

function chVisibility( element, elm ) {
    var e = document.getElementById(element);
    
    if( e.style.visibility == 'hidden' ) {
        e.style.visibility = 'visible';
    } else {
        e.style.visibility = 'hidden';
    }
}

function openWin(url, winName, mywidth, myheight, scrbars) {
    if(!top.open) return
    if(!winName) winName='infoWin'
    if(!mywidth) mywidth=620
    if(!myheight) myheight=470
    iwtop=50
    iwleft=30
    iwlt=''
    if(window.screen)
    {
        iwleft = Math.round((window.screen.width-mywidth)/2)
        iwtop = Math.round((window.screen.height-myheight)/2-40)
        iwlt=',left='+iwleft+',top='+iwtop
    }
    document.infoWin = top.open(url, winName,'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars='+scrbars+iwlt+',resizable=no,width='+mywidth+',height='+myheight+',innerWidth='+(40+mywidth)+',innerHeight='+(10+myheight))
}

function hrefloc( loc ) {
    location.href = loc;
}

function checkUncheckAll(theElement) {
    var theForm = theElement.form, z = 0;
    while(z < theForm.length) {
        if(theForm[z].type == 'checkbox') {
            theForm[z].checked = theElement.checked;
        }
        z++
    }
}

function transport_method( id ) {
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request")
        return
    }
    
    var url= root + 'ajax/paymethods.php'
    url+='?object='+id
    url+='&price='+document.getElementById('totalPriceDphH').value + '|' + document.getElementById('paymethod_price').value
    url+='&type=transport'
    url+='&lang='+lang
    url+='&sid='+Math.random()
    
    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {        
            document.getElementById('catch_transport_text').innerHTML = xmlHttp.responseXML.getElementsByTagName('text')[0].childNodes[0].nodeValue;
            document.getElementById('transport_title').innerHTML = xmlHttp.responseXML.getElementsByTagName('title')[0].childNodes[0].nodeValue;
            document.getElementById('transport_price').value = xmlHttp.responseXML.getElementsByTagName('price')[0].childNodes[0].nodeValue;
            document.getElementById('totalPriceDph').value = xmlHttp.responseXML.getElementsByTagName('totaldphprice')[0].childNodes[0].nodeValue;
        }
    }
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function payment_method( id ) {
    xmlHttp=GetXmlHttpObject()
    if (xmlHttp==null) {
        alert ("Browser does not support HTTP Request")
        return
    }
    
    var url= root + 'ajax/paymethods.php'
    url+='?object='+id
    url+='&price='+document.getElementById('totalPriceDphH').value + '|' + document.getElementById('transport_price').value
    url+='&type=paymethod'
    url+='&lang='+lang
    url+='&sid='+Math.random()

    xmlHttp.onreadystatechange = function() {
        if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {        
            document.getElementById('catch_paymethod_text').innerHTML = xmlHttp.responseXML.getElementsByTagName('text')[0].childNodes[0].nodeValue;
            document.getElementById('paymethod_title').innerHTML = xmlHttp.responseXML.getElementsByTagName('title')[0].childNodes[0].nodeValue;
            document.getElementById('paymethod_price').value = xmlHttp.responseXML.getElementsByTagName('price')[0].childNodes[0].nodeValue;
            document.getElementById('totalPriceDph').value = xmlHttp.responseXML.getElementsByTagName('totaldphprice')[0].childNodes[0].nodeValue;
        }
    }
    xmlHttp.open("GET",url,true)
    xmlHttp.send(null)
}

function number_format( number, decimals, dec_point, thousands_sep ) {
	 var exponent = "";
	 var numberstr = number.toString ();
	 var eindex = numberstr.indexOf ("e");
	 if (eindex > -1)
	 {
	   exponent = numberstr.substring (eindex);
	   number = parseFloat (numberstr.substring (0, eindex));
	 }
	 
	 if (decimals != null)
	 {
	   var temp = Math.pow (10, decimals);
	   number = Math.round (number * temp) / temp;
	 }
	 var sign = number < 0 ? "-" : "";
	 var integer = (number > 0 ? 
	     Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
	 
	 var fractional = number.toString ().substring (integer.length + sign.length);
	 dec_point = dec_point != null ? dec_point : ".";
	 fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
	              (dec_point + fractional.substring (1)) : "";
	 if (decimals != null && decimals > 0)
	 {
	   for (i = fractional.length - 1, z = decimals; i < z; ++i)
	     fractional += "0";
	 }
	 
	 thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
	                 thousands_sep : null;
	 if (thousands_sep != null && thousands_sep != "")
	 {
	for (i = integer.length - 3; i > 0; i -= 3)
	     integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
	 }
	 
	 return sign + integer + fractional + exponent;
}

function str_replace(search, replace, subject) {
  var result = "";
  var  oldi = 0;
  for (i = subject.indexOf (search)
     ; i > -1
     ; i = subject.indexOf (search, i))
  {
    result += subject.substring (oldi, i);
    result += replace;
    i += search.length;
    oldi = i;
  }
  return result + subject.substring (oldi, subject.length);
}

function setMenu( setmenu, unsetmenu1, unsetmenu2 ) {
    var a = document.getElementById(setmenu);
    var b = document.getElementById(unsetmenu1);
    var c = document.getElementById(unsetmenu2);
    
    if( a )
        a.className = 'tab_active';
    
    if( b )
        b.className = 'tab';
    
    if( c )
        c.className = 'tab';
    
    if( b ) chDisplayBlock(unsetmenu1 + '_list');
    if( c ) chDisplayBlock(unsetmenu2 + '_list');
    if( a ) chDisplayBlock(setmenu + '_list');
    
    chDisplay(setmenu + '_list');
}

function fbs_click() {
    u=location.href;
    t=document.title;
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&amp;t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
    return false;
}