var xmlhttp, xmlhttp_2, xmlhttp_3, xmlhttp_4;

var gblPauseSeconds = 6.25;
var gblFadeSeconds = .85;
var gblRotations = 99;
var tempRowId	=1;
 
// End Customization section
  
var gblOpacity = 100;

var gbl_row_id;
var gbl_value;

function getData(url, str)
{

	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
 	{
  		alert ("Your browser does not support XMLHTTP!");
  		return;
  	}
	url	= url + str
	// url=url+"&sid="+Math.random(); kan gebruikt worden om te voorkomen dat er een cached file gebruikt word
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function setSubMenu(str)
{

	xmlhttp_2=GetXmlHttpObject();
	if (xmlhttp_2==null)
 	{
  		alert ("Your browser does not support XMLHTTP!");
  		return;
  	}
	url	= "/setSubMenu.php" + str
	// url=url+"&sid="+Math.random(); kan gebruikt worden om te voorkomen dat er een cached file gebruikt word
	xmlhttp_2.onreadystatechange=stateChangedSubMenu;
	xmlhttp_2.open("GET",url,true);
	xmlhttp_2.send(null);
}

function getProductsData(str)
{
	getData("/getProducts.php", str);
	setSubMenu(str);
	if( browserCheck() ) {
		startFade(6);
	} else {
		document.getElementById("header_image").src = '/images/headers/'+headers[6][0];
	}
}

function getPageData(str, pageId)
{
	getData("/getPages.php", str);
	setSubMenu(str);
	if( browserCheck() ) {
		startFade(pageId);
	} else {
		document.getElementById("header_image").src = '/images/headers/'+headers[pageId][0];
	}
}

function setValues(str)
{
	//alert("TEST ");
	xmlhttp_3=GetXmlHttpObject();
	if (xmlhttp_3==null)
 	{
  		alert ("Your browser does not support XMLHTTP!");
  		return;
  	}
	url	= "/setValues.php" + str;
	xmlhttp_3.open("GET",url,true);
	xmlhttp_3.onreadystatechange=stateChangedCart;
	/*	switch( option ) {
			case 1 	:	setPrintPrices(value, row_id);
						updatePrices(row_id);
						break;
						
			case 2	:	setInstallPrices(value, row_id);
						updatePrices(row_id);
						break;
						
			case 3	:	setProductQty(value, row_id);
						updatePrices(row_id);
						break;
	}*/

	xmlhttp_3.send(null);	
}

function doNothing() {
}

function browserCheck()
{
	var browser=navigator.appName;
	var b_version=navigator.appVersion;
	var version=parseFloat(b_version);
	//alert( browser + "  " + version );
	if ( browser=="Microsoft Internet Explorer" && (version>=4))
	{
	   return false;
	}
	else
	{
		return true;
	}
}

function stateChanged()
{
	if (xmlhttp.readyState==4)
	{
	  document.getElementById("content_box").innerHTML=xmlhttp.responseText;
	}
}

function stateChangedSubMenu()
{
	if (xmlhttp_2.readyState==4)
	{
	  document.getElementById("header_sub_menu").innerHTML=xmlhttp_2.responseText;
	}
}

function stateChangedCart()
{
	if (xmlhttp_3.readyState==4)
	{
	  document.getElementById("content_box").innerHTML=xmlhttp_3.responseText;
	}
}
	
function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
	  	// code for IE7+, Firefox, Chrome, Opera, Safari
	  	return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
	  	// code for IE6, IE5
	  	return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function startFade(pageId)
{
	document.getElementById("header_box").style.backgroundImage = 'url(/images/headers/'+headers[pageId][0]+')';
	headerFade();
}

function headerFade()
{
  	var theImg = document.getElementById("header_image");
  	// determine delta based on number of fade seconds
	// the slower the fade the more increments needed
    var fadeDelta = 100 / (30 * gblFadeSeconds);
	// fade top out to reveal bottom image
	if (gblOpacity < 2*fadeDelta ) 
	{
	  gblOpacity = 100;
	  document.getElementById("header_image").src = document.getElementById('header_box').style.backgroundImage;
	}
	else
	{
	  gblOpacity -= fadeDelta;
	  setOpacity(theImg,gblOpacity);
	  setTimeout("headerFade()",30);  // 1/30th of a second
	}
}

function setOpacity(obj, opacity) 
{
  opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  

  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
 
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
 
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

function setPrintPrices(value, row_id)
{
	switch( value ) {
		case 0	:	document.getElementById('print_no_'+row_id).style.color = "#000000";
					document.getElementById('print_no_'+row_id).style.textDecoration ='none';
					document.getElementById('print_yes_'+row_id).style.color = "#999999";
					document.getElementById('print_yes_'+row_id).style.textDecoration ='line-through';
					break;
					
		case 1	:	document.getElementById('print_yes_'+row_id).style.color = "#000000";
					document.getElementById('print_yes_'+row_id).style.textDecoration ='none';
					document.getElementById('print_no_'+row_id).style.color = "#999999";
					document.getElementById('print_no_'+row_id).style.textDecoration ='line-through';
					break;
	}					
}

function setInstallPrices(value, row_id)
{
	switch( value ) {
		case 0	:	document.getElementById('install_no_'+row_id).style.color = "#000000";
					document.getElementById('install_no_'+row_id).style.textDecoration ='none';
					document.getElementById('install_yes_'+row_id).style.color = "#999999";
					document.getElementById('install_yes_'+row_id).style.textDecoration ='line-through';
					break;
					
		case 1	:	document.getElementById('install_yes_'+row_id).style.color = "#000000";
					document.getElementById('install_yes_'+row_id).style.textDecoration ='none';
					document.getElementById('install_no_'+row_id).style.color = "#999999";
					document.getElementById('install_no_'+row_id).style.textDecoration ='line-through';
					break;
	}					
}

function updatePrices(row_id)
{
	xmlhttp_4=GetXmlHttpObject();
	if (xmlhttp_4==null)
 	{
  		alert ("Your browser does not support XMLHTTP!");
  		return;
  	}
	url	= "/setValues.php?setting=updateprice&product="+row_id;
	this.tempRowId	= row_id;
	xmlhttp_4.onreadystatechange=stateUpdatePrice;
	xmlhttp_4.open("GET",url,true);
	xmlhttp_4.send(null);
}

function stateUpdatePrice()
{
	if (xmlhttp_4.readyState==4)
	{
		//alert(tempRowId);
		document.getElementById('sub_total_'+tempRowId).innerHTML=xmlhttp_4.responseText;
	}
}

function setProductQty(value, row_id)
{
	switch( value ) {
		case 0	:	var div_qty			 	= document.getElementById('qty_'+row_id);
					var div_sub_total		= document.getElementById('product_sub_total_'+row_id);
					var div_product_price	= document.getElementById('product_price_'+row_id);
					var product_price		= div_product_price.firstChild.nodeValue;
					var qty = div_qty.firstChild.nodeValue;
					qty--;
					if( qty < 1 ) qty = 1;
					div_qty.firstChild.nodeValue = qty;
					div_sub_total.firstChild.nodeValue = number_format((qty * product_price), 2, '.', '');
					break;
					
		case 1	:	var div_qty			 	= document.getElementById('qty_'+row_id);
					var div_sub_total		= document.getElementById('product_sub_total_'+row_id);
					var div_product_price	= document.getElementById('product_price_'+row_id);
					var product_price		= div_product_price.firstChild.nodeValue;
					var qty = div_qty.firstChild.nodeValue;				
					qty++;
					div_qty.firstChild.nodeValue = qty;
					div_sub_total.firstChild.nodeValue = number_format((qty * product_price), 2, '.', '');
					break;
	}					
}

function number_format (number, decimals, dec_point, thousands_sep) {
    var n = number, prec = decimals;
 
    var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
        return (Math.round(n*k)/k).toString();
    };
 
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 0 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;
 
    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;
 
    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;
 
    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;
 
        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }
 
    var decPos = s.indexOf(dec);
    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}