var globalAjaxDestination = "/ajax/";

function jsonEncodeOrder(orderdata){
    /*
    var jsonstring = '[';

    var orderlines = orderdata.orderlines;

    for(var i=0; i<orderlines.length; i++){
        orderline = orderlines[i];
        adressen = orderline["adressen"];

        var adressenstring = '';
        for(var j=0; j<adressen.length; j++){
            adres = adressen[j];
            if(j>0) adressenstring += ',';
            adressenstring += '{"adresId":'+adres["adresId"]+',"amount":'+adres["amount"]+',"color":'+adres["color"]+'}';
        }
        if(i>0) jsonstring += ',';
        jsonstring += '{"pdfId":'+orderline["pdfId"]+',"color":'+ordorderlineer["color"]+',"amount":'+orderline["amount"]+',"adressen":['+adressenstring+']}';

    }
    jsonstring += ']';

    return jsonstring;
    */
   return "";
}

var http = this.createRequestObject();
var _doAlert = false;
var _doFunc = false;



function createRequestObject(){
    var request_;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        request_ = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        request_ = new XMLHttpRequest();
    }
    return request_;
}

function handleInfo(){
    if(http.readyState == 4){
        if(_doAlert) alert(http.responseText);
        if(_doFunc){
            eval(_doFunc + '('+http.responseText+');');
		}
        _doAlert = false;
        _doFunc = false;
    }
}


function createRequestObject(){
  var requestObject;
  var browser = navigator.appName;
  if(browser == "Microsoft Internet Explorer"){
    requestObject = new ActiveXObject("Microsoft.XMLHTTP");
  }else{
    requestObject = new XMLHttpRequest();
  }
  return requestObject;
}

function sendAjax(requestObject,doAlert,doFunc,ignoreEncoding){
	var globalAjaxRequestObject = this.createRequestObject();
	//fix quotes and some other illegal chars in request
	var myRequestObject = requestObject;
	if(!ignoreEncoding && !myRequestObject.ignoreEncoding){
		myRequestObject = iterate(myRequestObject,xmlEncode);
		jsonRequestString = escape( JSON.encode(myRequestObject) );
	} else {
		//added unescape to fix double encoding
		jsonRequestString = escape(JSON.encode(myRequestObject));
	}
	//debugger;
	
	globalAjaxRequestObject.open('post', globalAjaxDestination);
	globalAjaxRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
	var sendData = "jsonrequest="+jsonRequestString;
	
	globalAjaxRequestObject.send(sendData);
	
	globalAjaxRequestObject.onreadystatechange = function () {
		if(globalAjaxRequestObject.readyState == 4){
			if(globalAjaxRequestObject.status==200){
                var response = eval('(' + globalAjaxRequestObject.responseText + ')');
				response = iterate(response, xmlDecode);
				if(doAlert == 'message'){
					alert(response["message"]);
				}else if(doAlert){
					alert('Status ok\nResponse:\n'+ globalAjaxRequestObject.responseText);
				}
				if (typeof doFunc === 'function') {
					//var response = eval('(' + globalAjaxRequestObject.responseText + ')');
					//response = iterate(response, xmlDecode);
					doFunc(response);
				} else if (doFunc) {
					var txt = xmlDecode(globalAjaxRequestObject.responseText);
					eval(doFunc + '('+txt+');');
					//doFunc(response);
				}
			}else{
                /*
                Naast de 'normale' fouten (4xx, 5xx) zijn er ook non-standard status codes (IE):                 
                    12002 - Server timeout
                    12029,12030, 12031 - dropped connections (either web server or DB server)
                    12152 - Connection closed by server.
                    13030 - StatusText properties are unavailable, and a query attempt throws an exception
                */
				alert('Uw verzoek kon niet verwerkt worden, uw browser kan geen verbinding krijgen met de website.\nHerlaad de pagina en probeer het a.u.b. opnieuw.\n\nFout bij ajax gegevensverwerking ' + requestObject.action + ',' + globalAjaxRequestObject.status + '\nDetails:\n' + globalAjaxRequestObject.responseText);
			}
		}
	};
}
//functie om input te fixen/encoden voordat ze verzonden wordt via een ajax request.
function xmlEncode(input){
		var replace_with	=	'';
		if(typeof(input) == 'undefined')	{
			return;
		}
		input	=	input.toString();
		// The 'g' in the first argument is used to tell the function 'replace' 
		// that all occurences (g = global)
		// of the character in between slashes have to be replaced.
		//, the i stands for case insensitive
		replace_with		=	'&amp;';
		input 				= 	input.replace(/&/g,		replace_with);
		replace_with		=	'&lt;';
		input 				= 	input.replace(/</g,		replace_with);
		replace_with		=	'&gt;';
		input 				= 	input.replace(/>/g,		replace_with);
		replace_with		=	'';
		input 				= 	input.replace(/'/g,		replace_with);
		replace_with		=	'';
		input 				= 	input.replace(/"/g,		replace_with);
		replace_with		=	'&#043;';
		input 				= 	input.replace(/\+/g,	replace_with);
		replace_with		=	'';
		input 				= 	input.replace(/\\/g,	replace_with);
		replace_with		=	'&sect;';
		input 				= 	input.replace(/§/g,		replace_with);
		replace_with		=	'&ouml;';
		input 				= 	input.replace(/ö/gi,	replace_with);
		replace_with		=	'&auml;';
		input 				= 	input.replace(/ä/gi,	replace_with);
		replace_with		=	'&euml;';
		input 				= 	input.replace(/ë/gi,	replace_with);
		replace_with		=	'&iuml;';
		input 				= 	input.replace(/ï/gi,	replace_with);
		replace_with		=	'&uuml;';
		input 				= 	input.replace(/ü/gi,	replace_with);
		replace_with		=	'&eacute;';
		input 				= 	input.replace(/é/gi,	replace_with);
		replace_with		=	'&egrave;';
		input 				= 	input.replace(/è/gi,	replace_with);
		replace_with		=	'&agrave;';
		input 				= 	input.replace(/à/g,		replace_with);
		input = encodeURIComponent(input);
		return input.toString();
	}
//functie om input te fixen/encoden voordat ze verzonden wordt via een ajax request.
function xmlDecode(input, decodeQuotes){
	var replace_with	=	'';
	if(typeof(input) == 'undefined')	{
		return;
	}
	input	=	input.toString();
	input = decodeURIComponent(input);
	//alert(input);
	// The 'g' in the first argument is used to tell the function 'replace' 
	// that all occurences (g = global)
	// of the character in between slashes have to be replaced.
	//, the i stands for case insensitive
	
	replace_with		=	'ö';
	input 				= 	input.replace(/&ouml;/gi,	replace_with);
	replace_with		=	'à';
	input 				= 	input.replace(/&agrave;/gi,	replace_with);
	replace_with		=	'\'';
	input 				= 	input.replace(/&#039;/gi,	replace_with);
	replace_with		=	'ä';
	input 				= 	input.replace(/&auml;/gi,	replace_with);
	replace_with		=	'ë';
	input 				= 	input.replace(/&euml;/gi,	replace_with);
	replace_with		=	'ï';
	input 				= 	input.replace(/&iuml;/gi,	replace_with);
	replace_with		=	'ü';
	input 				= 	input.replace(/&uuml;/gi,	replace_with);
	replace_with		=	'é';
	input 				= 	input.replace(/&eacute;/gi,	replace_with);
	replace_with		=	'è';
	input 				= 	input.replace(/&egrave;/gi,	replace_with);
	replace_with		=	'&';
	input 				= 	input.replace(/&amp;/g,	replace_with);
	replace_with		=	'<';
	input 				= 	input.replace(/&lt;/g,	replace_with);
	replace_with		=	'>';
	input 				= 	input.replace(/&gt;/g,	replace_with);
	replace_with		=	'\+';
	input 				= 	input.replace(/&#043;/g,	replace_with);
	replace_with		=	'§';
	input 				= 	input.replace(/&sect;/g,	replace_with);
	if (decodeQuotes)	{
	  replace_with		=	'"';
	  input 				= 	input.replace(/&quot;/gi,	replace_with);
	}
	//alert(input.toString());
	return input.toString();
	
}
function iterate(obj, func){
	for(var x in obj){
		switch (typeof(obj[x])) {
			case "object":
				obj[x] = iterate.apply(this,Array(obj[x],func));
				break;
			case "array":
				obj[x] = iterate.apply(this,Array(obj[x],func));
				break;
			case "string":
				obj[x] = func.call(this,obj[x]);
				break;
			default:
				void(0);
		}
	}
	return obj;
}

function is_numeric(val){
   return (input - 0) == input && input.length > 0;
}

/* plotgemak 1.1
function createRequestObject(){
  var requestObject;
  var browser = navigator.appName;
  if(browser == "Microsoft Internet Explorer"){
    requestObject = new ActiveXObject("Microsoft.XMLHTTP");
  }else{
    requestObject = new XMLHttpRequest();
  }
  return requestObject;
}

function sendAjax(requestObject,doAlert,doFunc) {

  var globalAjaxRequestObject = this.createRequestObject();

  jsonRequestString = escape( JSON.encode(requestObject) );

  globalAjaxRequestObject.open('post', globalAjaxDestination);
  globalAjaxRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  //var sendData = "jsonrequest="+jsonRequestString;


    var sendData = "";
    var i = 0;
    
    for(prop in requestObject){
      if(i>0) sendData += "&";
      value = requestObject[prop];
	  alert( value );
      value = value.replace("&","%26"); // escape & in content, interferes with post &
alert( value );
      sendData += prop+"="+escape(value);
      i++;
    }

  globalAjaxRequestObject.send(sendData);
  globalAjaxRequestObject.onreadystatechange = function () {
    if(globalAjaxRequestObject.readyState == 4){
      if(globalAjaxRequestObject.status==200){
        if(doAlert){
          alert('Status ok\nResponse:\n'+ globalAjaxRequestObject.responseText);
        }
		
        if (typeof doFunc === 'function') {
          var response = eval('(' + globalAjaxRequestObject.responseText + ')');
          doFunc(response);
        } else if (doFunc) {
          x = eval(doFunc + '('+globalAjaxRequestObject.responseText+');');
        }
      }else{
        alert('Fout bij ajax gegevensverwerking ' + requestObject.action + ',' + globalAjaxRequestObject.status + '\nDetails:\n' + globalAjaxRequestObject.responseText);
      }
    }
  };
}
*/
/*
function sendAjax(args,doAlert,doFunc) {
     var adres = '/ajax/';

    _doAlert = doAlert;
    _doFunc = doFunc;

    http.open('post', adres);
    http.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    var sendData = "";
    var i = 0;
    
    for(prop in args){
      if(i>0) sendData += "&";
      value = args[prop];
      value = value.replace("&","%26"); // escape & in content, interferes with post &

      sendData += prop+"="+escape(value);
      i++;
    }
    //sendData = "action="+args.action+"&jsondata="+args.jsondata;


    http.send(sendData);
    http.onreadystatechange = this.handleInfo;
}
*/
function fillCheck(el,text){
    if(el.value==''){
      el.value = text;
    }else if(el.value==text){
      el.value = '';
    }
}
function fillCheckColored(el,text,colorFilled,colorEmpty,action){
   if(action=='in'){
	  if(el.value==text){
		el.value = '';
		el.style.color = colorFilled;
	  } 
   }
   if(action=='out'){
	  if(el.value=='' ){
		el.value = text;
		el.style.color = colorEmpty;
	  }else if(el.value==text){
		el.style.color = colorEmpty;
	  }else{
		  el.style.color = colorFilled;
	  } 
   }
   

}

function hideBlock(elid){
    el = document.getElementById(elid);
    el.style.display = 'none';
}

function showBlock(elid){
    el = document.getElementById(elid);
    el.style.display = 'block';
}
		
