///////////////////////////////////////////////////////////////////////////////
// Toevoeging aan script.js voor eShop

function openList() {
	var list = new Array();
	this.init = function(str) {
		names = str.split(",");
		for( idx=0; idx<names.length; idx++ ) {
			if ( names[idx].length != 0 ) { list[ names[idx] ] = 1; }
		}
	}
	this.cleanup = function(str) {
		if ( ( idx = str.lastIndexOf("_") ) != -1 ) str = str.substr(idx+1);
		return ( ""+parseInt(str) != str ) ? undefined : str;
	}
	this.add = function(id) {
		if ( ( id = this.cleanup(id) ) != undefined ) {
			if ( list[id] == undefined ) { list[id] = 1; }
		}
	};
	this.del = function(id) {
		if ( ( id = this.cleanup(id) ) != undefined ) {
			if ( list[ id] != undefined ) {	delete(list[id]); }
		}
	};
	this.toString = function() {
		str = new Array();
		for(name in list) { str.push( name ) }
		return str.join(",");
	}
}

function tellafriendsubmit() {  // This function does the AJAX request
	var frm = document.forms["tellafriendform"]
	var yname = encodeURIComponent(frm.elements["yname"].value)
	var yemail = encodeURIComponent(frm.elements["yemail"].value)
	var femail = encodeURIComponent(frm.elements["femail"].value)
	var comments = encodeURIComponent(frm.elements["comments"].value)
	var parameters = "yname="+yname+"&yemail="+yemail+"&femail="+femail+"&comments="+comments
	http.open( "POST", "/eshop/tellafriend.php", true )
	http.setRequestHeader( "Content-type", "application/x-www-form-urlencoded" )
	http.onreadystatechange = getHttpRes2;
	http.send(parameters)
}

function doHttpGETRequest() {  // This function does the AJAX request
//  var value = encodeURIComponent(document.forms["loader"].elements["openedNodes"].value)
//  var value = openlist
//  alert("openlist: " + openlist )
  var parameters = "?openedNodes="+openlist // value
  http.open( "GET", "eshop/updater.php" + parameters, true);
  http.onreadystatechange = getHttpRes;
  http.send(null);
}

function getHttpRes2() {
  if (http.readyState == 4) { 
    res = http.responseText;  // These following lines get the response and update the page
    rc = res.substring(0,1)
    message = res.substring(1)
    document.getElementById('tellafriendresponse').innerHTML = message;
    if ( rc == "+" ) {
      var frm = document.forms["tellafriendform"]
      frm.elements["yname"].value = ""
      frm.elements["yemail"].value = ""
      frm.elements["femail"].value = ""
      frm.elements["comments"].value = ""
    }
  }
}

function getHttpRes() {
  if (http.readyState == 4) { 
    res = http.responseText;  // These following lines get the response and update the page
    document.getElementById('updater').innerHTML = res;	
  }
}

function getXHTTP() {
  var xhttp;
   try {   // The following “try” blocks get the XMLHTTP object for various browsers…
      xhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
 		 // This block handles Mozilla/Firefox browsers...
	    try {
	      xhttp = new XMLHttpRequest();
	    } catch (e3) {
	      xhttp = false;
	    }
      }
    }
  return xhttp; // Return the XMLHTTP object
}

var http = getXHTTP();

var openlist = new openList();


$( document ).ready( function() {
	openlist.init(window.openeditems);
} );


///////////////////////////////////////////////////////////////////////////////
$( document ).ready( function() {

	$( "div.cat_box_controller" ).click( function() {
		
		box = $( this ).parent().parent();
		box_content = box.find( "div.cat_box_content:first" );

		controller = $( this ).parent().find( "div.cat_box_controller img" );
		
		if( box_content.css( "display" ) == "block" ) {
			box_content.slideUp();
			controller.attr( "src", "./images/cat/cat_clapper_plus.gif" );
			openlist.del( box.attr("id") );
		} else {
			box_content.slideDown();
			controller.attr( "src", "./images/cat/cat_clapper_minus.gif" );
			openlist.add( box.attr("id") );
		}
		doHttpGETRequest();
	} );
	
} );

///////////////////////////////////////////////////////////////////////////////
function edit_ipod( state ) {
	
	document.getElementById( 'edit_layer_ipod' ).style.visibility	= state;
	document.getElementById( 'edit_layer_tarif' ).style.visibility	= "hidden";
}

///////////////////////////////////////////////////////////////////////////////
function edit_tarif( state ) {
	
	document.getElementById( 'edit_layer_tarif' ).style.visibility	= state;
	document.getElementById( 'edit_layer_ipod' ).style.visibility	= "hidden";
}

///////////////////////////////////////////////////////////////////////////////
function detail_window( obj ) {
	
	window.open( obj.href, obj.target, 'width=320,height=320,scrollbars=0' );
}
///////////////////////////////////////////////////////////////////////////////

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}
 
function prepareInputsForHints() {
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		// test to see if the hint span exists first
		if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
			// the span exists!  on focus, show the hint
			inputs[i].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			// when the cursor moves away from the field, hide the hint
			inputs[i].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
	// repeat the same tests as above for selects
	var selects = document.getElementsByTagName("select");
	for (var k=0; k<selects.length; k++){
		if (selects[k].parentNode.getElementsByTagName("span")[0]) {
			selects[k].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			selects[k].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////

	function guiUpdatePayment( obj ) {
		for ( var i=0; i<obj.length; i++ ) {
			var elm = document.getElementById(obj[i].value)
			if( elm ) elm.style.display = ( i == obj.selectedIndex ) ? "block" : "none"
		}
	}
	
	function guiUpdatePZ( obj ) {
		lyrs=['z_bedrijfsnaam','z_btwnummer','z_afleveradres','ca_bedrijfsnaam','ca_btwnummer','ca_afleveradres']
		 for (var i_tem = 0; i_tem < lyrs.length; i_tem++)
		 var lyr = document.getElementById(lyrs[i_tem]).style.display=( obj.checked ) ? "none":"block"
	}

	function guiUpdateCA( obj ) {
		lyrs=['ca_login','ca_password']
		 for (var i_tem = 0; i_tem < lyrs.length; i_tem++)
		 var lyr = document.getElementById(lyrs[i_tem]).style.display=( obj.checked ) ? "none":"block"
	}

