/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/
var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}
/**
*  end of URL encode / decode
**/

function readCookie(name) {
	var cookieName = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(cookieName) == 0) return c.substring(cookieName.length,c.length);
	}
	return null;
}


function openFormBuilderPopUp(url){
	var hostName = getFormBuilderHost(window.location.href);
	if(hostName != '') {
		url.replace('http*/wfb/', hostName);
	}
	var qs;
	var newUrl = window.location.href;
	if(window.top.location.search.substring(1).length>0){
		if(getParameter(window.top.location.search.substring(1), 'startFormbuilder') == 'null' || getParameter(window.top.location.search.substring(1), 'startFormbuilder') != Url.decode(url)){
			if(getParameter(window.top.location.search.substring(1), 'startFormbuilder') == 'null') {
				// parameter not in queryString
				qs=window.top.location.search.substring(1)+'&startFormbuilder='+url;
			} else {
				// parameter already in queryString, but other form wanted
				qs=window.top.location.search.substring(1);
				// remove current parameter from queryString
				var stringToReplace = 'startFormbuilder='+getParameter(window.top.location.search.substring(1), 'startFormbuilder');
				qs=qs.replace(stringToReplace, '');
				// add new one
				qs=qs+'startFormbuilder='+url;
			}
			
			newUrl=newUrl.replace(window.top.location.search.substring(1),qs);
		} else {
			// query string already contains formbuilder parameter
			var cookieVal = readCookie('LANGUAGE');
			switch(cookieVal) {
				case 'en':
 				window.alert('please, refresh the window and wait a few seconds.');
 				break;
				case 'fr':
 				window.alert('Veuillez rafraichir la fenêtre et attendre quelques secondes.');
 				break;
				case 'nl':
 				window.alert('please, refresh the window and wait a few seconds.');
 				break;
				window.alert('please, refresh the window and wait a few seconds.');
			}
			if(cookieVal==null) {
				window.alert('please, refresh the window and wait a few seconds.');
			}        			
			return;
		}
		
	} else {
		newUrl=newUrl+'?startFormbuilder='+url;
	}
	window.location.replace(newUrl);
}
          
function getParameter ( queryString, parameterName ) {
   if ( queryString.length > 0 ) {
      // Add "=" to the parameter name (i.e. parameterName=value)
      var parameterName = parameterName + "=";
      // Find the beginning of the string
      begin = queryString.indexOf ( parameterName );
      // If the parameter name is not found, skip it, otherwise return the value
      if ( begin != -1 ) {
         // Add the length (integer) to the beginning
         begin += parameterName.length;
         // Multiple parameters are separated by the "&" sign
         end = queryString.indexOf ( "&" , begin );
         
         if ( end == -1 ) {
         	end = queryString.length
      	}
      	// Return the string
      	return unescape ( queryString.substring ( begin, end ) );
      }	   
   }
   // Return "null" if no parameter has been found
   return "null";
}

function testParams(){
	try {
	    if(getParameter(window.top.location.search.substring(1),'startFormbuilder') != 'null') {
	    	var encodedUrl = getParameter(window.top.location.search.substring(1),'startFormbuilder');
	    	var decodedUrl = Url.decode(encodedUrl);
	    	// delay popup opening to be sure all variables are initialized
	    	setTimeout("showPopWin('"+decodedUrl+"', 750, 500, null, true)",500);
	    }
	}catch(e) {
		// should not occur unless other error in page
	}
}

function getFormBuilderHost(url) {
	if (url.indexOf('https://admit-uat.') == 0) {
		return 'https://admit-uat.belgacom.be/eshop/';
	}
	if (url.indexOf('https://admit-itt.') == 0) {
		return 'https://admit-itt.belgacom.be/eshop/';
	}
	if (url.indexOf('https://admit.') == 0) {
		return 'https://admit.belgacom.be/eshop/';
	}
	return '';
}
// check at load if formbuilder popup needs to be opened
Event.observe( window, 'load', testParams);