function newWin(link) {
  var winFeatures = "scrollbars=yes,width=650,height=450,directories=no,status=yes,resizable=yes,menubar=yes,toolbar=yes,location=yes";
  var bookWindow = window.open(link, "popup", winFeatures);
  bookWindow.focus();
  var doTrack=false;
  var isDoc=link.match(/\.(?:pdf)($|\&|\?)/);
  var lnkDesc
	if (link.charAt(0)== "/") {
           if (isDoc) { 
		lnkDesc=link; 
		doTrack=true;
	   }
 	}
	else
	{
	   if (parseUri(link).host == "www.ilo.org") {
		if (isDoc) {
			lnkDesc=parseUri(link).path;
			doTrack=true;
			}
		}
	   else 
		{
		lnkDesc="/external/" + link;
		doTrack=true;
 		}
	}
	try{
  		if (doTrack) pageTracker._trackPageview(lnkDesc);
	}catch(err){};

}

//Opens a new popup window with a specified name. The popup will use its name to deduce names of target elements into which to insert the 
//resulting selected values.
function oPenImag3(iMagPath, name)
{
localwin=window.open(iMagPath,name,"scrollbars=yes,width=650,height=450,directories=no,status=yes,resizable=yes,menubar=yes,toolbar=yes,location=yes");
localwin.focus();
}

function oPenImag2(iMagPath)
{
localwin=window.open(iMagPath,'preview',"scrollbars=yes,width=650,height=450,directories=no,status=yes,resizable=yes,menubar=yes,toolbar=yes,location=yes");
localwin.focus();
}

//Collects values of all checked checkboxes on the popup selector and passes them back to the calling window.
//If the checkbox is followed by a LABEL tag, will also colelct the text of the label and pass it back to the opener.
//To pass back values, the calling window must define an input field with id <called-popup-name>
//To pass back labels, the calling window must define a container tag (SPAN, DIV, etc) with id <called-popup-name>Names
//Use oPenImag3 method to call the popup window.
//Example:
/*
     <p>
        //Called window name is 'country', so countryNames will receive selected checkboxes' labels.
        <span id="countryNames"></span>
        //countryValues will receive values of received checkboxes.
        <input type="hidden" id="country" name="country" />
        <a href="#" onclick="oPenImag3('popup-country.htm','country')">select countries</a>
     </p>
*/
function collectSelected()
{
    var selected = new Array();
    var selectedNames = new Array();
    
    //Collect selected values and their names. The checkbox must be followed by the LABEL tag to collect the name.
    var allCb = document.getElementsByTagName("input");
    for (var i = 0; i < allCb.length; i++)
    {
        var cb = allCb[i];
        if (cb.type == "checkbox" && cb.checked)
        {
           selected.push(cb.value);
           var label = getAssociatedLabel(cb);
           if (label)
           {
                selectedNames.push(normalizeSpace(label.innerHTML));
           } 
        }
    }
    var v = "";
    var n = "";
    for (var i = 0; i < selected.length; i++)
    {
        if (v.length > 0) v += ",";
        v += selected[i];
        if (n.length > 0) n += ", ";
        n += selectedNames[i];
    }

    //Transfer data back to the parent window.
    var w = window.opener;
    if (w)
    {
        var name = window.name;
        var valueField = w.document.getElementById(name);
        var namesField = w.document.getElementById(name + 'Names');
        if (valueField)
        {
            valueField.value = v;
        }
        if (namesField)
        {
            if (namesField.tagName == "INPUT")
            {
                namesField.value = n;
            } 
            else
            {
                namesField.innerHTML = n;
            }
        }
    }
   self.close(); 
}

function getAssociatedLabel(el)
{
    if (el == null) return null;   
    
    var label = el.nextSibling;
    while (label != null)
    {
        if (label.tagName == 'LABEL')
        {
            return label;
        }
        label = label.nextSibling;
    }
    return null;
}

function normalizeSpace(s)
{
    while (s.indexOf("  ") > -1)
    {
        s = s.replace(/  /g, " ");
    }
    return s.replace(/\n/g, "").replace(/\r/g,"");
}


//This method checks all checkboxes that the user choose the previous time the popup was opened.
//The method expects to find an input field with the same name as its window name.
//Call this method from the body.onload event of the popup window
//Open the window with oPenImag3 method.
function selectExisting()
{
    var w = window.opener;
    if (w)
    {
        var name = window.name;
        var valueField = w.document.getElementById(name);
        if (valueField)
        {
            var lookup = new Array(); 
            var selected = valueField.value.split(',');

            if (selected)
            {
                for (var i = 0; i < selected.length; i++)
                {
                    lookup[selected[i]] = true;
                }
            }
             
            var allCb = document.getElementsByTagName("input");
            for (var i = 0; i < allCb.length; i++)
            {
                var cb = allCb[i];
                if (cb.type == "checkbox" && lookup[cb.value])
                {
                   cb.checked = true;
                }
            }
            
        }
    }
}
    function surfTo( objSelect )
    {
        objOption = objSelect.options[objSelect.selectedIndex];
        strRedirectionType = objOption.getAttribute( "redirectiontype" );

        if ( strRedirectionType == "sameWindow" )
        {
            location.href = objOption.value;
        }
        else if ( strRedirectionType == "popup" )
        {
            var winFeatures = "scrollbars=yes,width=650,height=450,directories=no,status=yes,resizable=yes,menubar=yes,toolbar=yes,location=yes";
            var newWindow = window.open( objOption.value, "popup", winFeatures);
            newWindow.focus();
        }
    }

	


/**
* Code that identifies the English language of the site
*/
var ENGLISH_LANGUAGE_CODE = "English";

/**
* Code that identifies the France language of the site
*/
var FRANCE_LANGUAGE_CODE = "France";

/**
* Code that identifies the Spanish language of the site
*/
var SPANISH_LANGUAGE_CODE = "Spanish";

/**
* Code that identifies the current language of the site
*/
var CURRENT_LANGUAGE_CODE = "";


/**
* List of messages for empty form field in diferent languages
*/
strEmptyFieldMessage = new Array();
strEmptyFieldMessage[ENGLISH_LANGUAGE_CODE] = "The field [%FIELD_NAME%] cannot be empty";
strEmptyFieldMessage[FRANCE_LANGUAGE_CODE] = "Le champ [%FIELD_NAME%] est obligatoire";
strEmptyFieldMessage[SPANISH_LANGUAGE_CODE] = "El campo [%FIELD_NAME%] no puede estar vacio";

/**
* List of messages for valid e-mail in diferent languages
*/
strInvalidEmailMessage = new Array();
strInvalidEmailMessage[ENGLISH_LANGUAGE_CODE] = "The field [%FIELD_NAME%] is not a valid e-mail";
strInvalidEmailMessage[FRANCE_LANGUAGE_CODE] = "L'adresse email [%FIELD_NAME%] n'est pas correcte";
strInvalidEmailMessage[SPANISH_LANGUAGE_CODE] = "El campo ... no contiene un correo electrónico válido";

/**
* List of messages used in the beginning of the form validation
*/
strBeginningOfValidationMessage = new Array();
strBeginningOfValidationMessage[ENGLISH_LANGUAGE_CODE] = "Sorry. The following problems were found in the online form\n\n";
strBeginningOfValidationMessage[FRANCE_LANGUAGE_CODE] = "Désolé. Les problèmes suivant viennent d'apparaître dans le formulaire en ligne\n\n";
strBeginningOfValidationMessage[SPANISH_LANGUAGE_CODE] = "Los siguientes problemas fueron encontrados en el formulario\n\n";


	

/**
 *  Class for form validation.
 */
function FormValidation(strFormName){

	/** 
	* Reference to the HTML form
	*/
	this.objForm = null;
	/** 
	* Array of regex used to validate each form field
	*/
	this.strValidationRules = new Array();
	/** 
	* Array of messages displayed in case of validation problem in each form field
	*/
	this.strValidationMessage = new Array();
	/** 
	* Complete message displayed in case of validation problems
	*/
	this.strFinalMessage = new Array();
	/** 
	* Flag that indicates that the there are validation problems in the form
	*/
	this.flgFormProblems = false;


	/**
	 * Prepare the rule and the message that are going to be used to check if the form field strFieldName is empty
	 *
	 *@param  strFieldName Form field name attribute
	 *@param  strFieldLabel Form field label
	 *@return void 
	 */
	this.isEmpty = function(strFieldName,strFieldLabel){
		this.strValidationRules[strFieldName] = "\\S+";
		this.strValidationMessage[strFieldName] = strEmptyFieldMessage[CURRENT_LANGUAGE_CODE].replace(/\[%FIELD_NAME%\]/g, strFieldLabel);
	}
	
	/**
	 * Prepare the rule and the message that are going to be used to check if the form field strFieldName is a valid e-mail
	 *
	 *@param  strFieldName Form field name attribute
	 *@param  strFieldLabel Form field label
	 *@return void 
	 */
	this.isEmail = function(strFieldName,strFieldLabel){
		this.strValidationRules[strFieldName] = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$";
		this.strValidationMessage[strFieldName] = strInvalidEmailMessage[CURRENT_LANGUAGE_CODE].replace(/\[%FIELD_NAME%\]/g, strFieldLabel);
	}
	
	
	/**
	 * Perform the validation tests in the fields of the form objFormRef
	 *
	 *@param  objFormRef Reference to the HTML form
	 *@return bool 
	 */
	this.performValidation = function(objFormRef){
		this.objForm = objFormRef;
		this.flgFormProblems = false;

		this.strFinalMessage = strBeginningOfValidationMessage[CURRENT_LANGUAGE_CODE];
		
		for(validationRule in this.strValidationRules){
			var re = new RegExp(this.strValidationRules[validationRule]);
			if (!this.objForm.elements[validationRule].value.match(re)){
				this.flgFormProblems = true;
				this.strFinalMessage += this.strValidationMessage[validationRule];
				this.strFinalMessage += "\n";
			}
		}
		
		if(this.flgFormProblems){
			alert(this.strFinalMessage);
			return false;
		}else{
			return true;
		}
	}
}

 /*
 Script that unobufuscate email addresses written user%40@server.com 
 and transform them to user@server.com 
*/
    function unobfuscateEmail(elementId){
		  var obj_recipient = document.getElementById(elementId);
		  obj_recipient.value =   obj_recipient.value.replace("%40","@");
	}
	
/*
 Script that displays/hides the additional ul lists 
*/
function displayhide (obj) {
	var which=obj.id;
	which=which.slice(5);
	if (document.getElementById(which).style.display!="block") { 
		document.getElementById(which).style.display = "block"; 
	}
	else { 
		document.getElementById(which).style.display = "none"; 
	}
}
function changeSign(obj){
	obj.innerHTML = (obj.innerHTML != '[+]') ? '[+]' : '[-]';
}


/*
	parseUri 1.2.1
	(c) 2007 Steven Levithan <stevenlevithan.com>
	MIT License
*/

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

