////////////////////////////////////////////////////////////////////////
//////////// function for trim start and end space chars ///////////////
////////////////////////////////////////////////////////////////////////

function sessionTimeout(url){
	if(confirm("Your session expires soon. Go to home page to keep your access ?")){
		window.document.location=url+"/home.cfm";
	}		
}

function trim(string) { 
	return string.replace(/(^\s*)|(\s*$)/g,''); 
}

function popup(page, x, y){
	window.open (page, 'AKDMIA', config='height='+y+', width='+x+', toolbar=no, menubar=no, scrollbars=yes, resizable=no, location=no, directories=no, status=no');
}

function newWindow(url,key){
	if(key != -1){
		var mylink = url+key;
		window.open (mylink, 'AKDMIA', config='toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes, location=yes, directories=yes, status=yes');
	}
}


function showDetails(typeDetails){
	document.getElementById(typeDetails).style.display = 'block';
}

function hideDetails(typeDetails){
	document.getElementById(typeDetails).style.display = 'none';
}

function inverse(id)
{
	var d = document.getElementById(id);

	if( d.style.display == 'none' ){
		/* first mask all other menus */
		
		if(document.getElementById('pubDetails') != null)		{hideDetails('pubDetails');}
		if(document.getElementById('contactDetails') != null)	{hideDetails('contactDetails');}
		if(document.getElementById('webDetails') != null)		{hideDetails('webDetails');}
		
		d.style.display='block';
	}
	else{
		d.style.display='none';
	}
} 

function inverseall(id)
{
	var d = document.getElementById(id);

	if( d.style.display == 'none' ){
		d.style.display='block';
	}
	else{
		d.style.display='none';
}
} 

function SubmitProfile(url){
	if(confirm("Updating the draft will change the status of the profile from submitted to unsubmitted. Would you like to confirm?")){
		document.location=url;	
	}
	else{
		document.forms["valideProfile"].elements["Update"].disabled	 = false;
		document.forms["valideProfile"].elements["Update"].value 	 = "Submit For Validation";
	}
}

function validaction(url){
	if(confirm("Do you confirm this validation ?")){
		document.location=url;		
	}
	else{
		document.forms["Webprofile"].elements["validate"].disabled = false;
		document.forms["Webprofile"].elements["validate"].value = "Validate";
	}
	
}

function deleteaction(url){
	if(confirm("Delete definitively ?")){
		document.location=url;		
	}
	else{
		document.forms["cv"].elements["Delete"].disabled = false;
		document.forms["cv"].elements["Delete"].value = "Delete";
	}
	
}

function alert_max(type, num){
	alert("Maximum "+type+" reach ("+num+" max)");
}

function get_balance(){
	allocated = document.getElementById("TOTAL_ALLOCATED").value;
	spent	  = document.getElementById("TOTAL_EXPENSES").value;
	
	if(allocated > 0 && spent > 0){
		balance = allocated - spent;
	}
	else{balance = 0;}
 
  	document.getElementById("balance").value = balance;
}



function RepositionObjects (evt) {
	var W3C = window.addEventListener;

	var pageOffsetX;
	var pageOffsetY;
	
	var objectWidth = '200px';
	var objectHeight = '200px';

	if(W3C) {
	    height = parseInt(document.documentElement.clientHeight);
        width = parseInt(document.documentElement.clientWidth);
               
		pageOffsetX = parseInt(window.pageXOffset);
		pageOffsetY = parseInt(window.pageYOffset);
	}
	else {          
	    height = parseInt(document.body.clientHeight);
        width = parseInt(document.body.clientWidth);
        
		pageOffsetX = parseInt(document.body.scrollLeft || document.documentElement.scrollLeft);
		pageOffsetY = parseInt(document.body.scrollTop || document.documentElement.scrollTop);
	}
	
	// unmovableObj.style.left = (pageOffsetX + (width / 2)) + 'px'; 
	// unmovableObj.style.top = (pageOffsetY + (height / 2)) + 'px'; 
	
	document.getElementById("ajaxwait").style.left = (pageOffsetX + (width / 2) - (parseInt(objectWidth) / 2)) + 'px'; 
	document.getElementById("ajaxwait").style.top = (pageOffsetY + (height / 2) - (parseInt(objectHeight) / 2)) + 'px'; 
}

function showToolTip(e,text){
	if(document.all)e = event;
	
	var obj = document.getElementById('helpbox');
	var obj2 = document.getElementById('helpbox_content');
	obj2.innerHTML = text;
	obj.style.display = 'block';
	
	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	var leftPos = e.clientX - 40;
	if(leftPos<0)leftPos = 0;
	obj.style.left = leftPos + 'px';
	obj.style.top = e.clientY - obj.offsetHeight -1 + st + 'px';
}	

function inverseImg(type, id){
	if(type = "arbo"){
		
		file = document.images["img_"+id].src;
		path = file.substr(0, file.lastIndexOf("/"));
		d = document.getElementById("group_"+id);	
		if (d.style.display == "none"){document.images["img_"+id].src = path+"/arbo_moins.gif";}
		else{document.images["img_"+id].src = path+"/arbo_plus.gif";}
	}
	
}

function getPeriodicalTitle(periodicity, target){
	 var xhr_object = null; 
	 
		if(window.XMLHttpRequest) // Firefox 
	      xhr_object = new XMLHttpRequest(); 
	   else if(window.ActiveXObject) // Internet Explorer 
	      xhr_object = new ActiveXObject("Microsoft.XMLHTTP"); 
	   else { // XMLHttpRequest non supporté par le navigateur 
	      alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	      return; 
	   } 
	 
	   xhr_object.open("POST", base_address+"/secure/research/get_periodicalTitle.cfm", true); 
	     
	   xhr_object.onreadystatechange = function() { 
	      if(xhr_object.readyState == 4) 
	         eval(xhr_object.responseText); 
	   } 
	   xhr_object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
	   var data = "periodicity="+periodicity+"&target="+target; 
	   xhr_object.send(data); 	
	
}

function verif_conn(){
	if(document.login.user_login.value==''){
		alert("Please, enter your username !")
		return false;
		}
	if(document.login.user_password.value==''){
		alert("Please, enter your password !")
		return false;
		}
}

function checkdata_text(value, fieldname,formName){
	if(value == ''){ 
		alert('Please fill '+fieldname+' field before submit.'); 
		
		document.forms[formName].elements['Add'].disabled = false;
		document.forms[formName].elements['Add'].value = 'Add';
		return false;
	}
	else{document.forms[formName].submit();}
}

function computeRef(the_form){
		ref = document.forms[the_form].elements["DOCUMENT_KEY"].value;
		CASESERIES_PART 	= document.forms[the_form].elements["CASESERIES_PART"].value;
		DOCUMENTTYPE_KEY 	= document.forms[the_form].elements["DOCUMENTTYPE_KEY"].options[document.forms[the_form].elements["DOCUMENTTYPE_KEY"].selectedIndex].value;
		LANGUAGE_CODE 		= document.forms[the_form].elements["LANGUAGE_CODE"].value;
		RELEASE_NUMBER 		= document.forms[the_form].elements["RELEASE_NUMBER"].value;
		INSEAD_NUMBER 		= document.forms[the_form].elements["INSEAD_NUMBER"].value;
		if(CASESERIES_PART == '')	{CASESERIES_PART = "[CASE_PART]";} 
		if(DOCUMENTTYPE_KEY == '')	{DOCUMENTTYPE_KEY = "[DOCUMENT_TYPE]";}
		if(LANGUAGE_CODE == '')		{LANGUAGE_CODE = "[LANGUAGE]";}
		if(RELEASE_NUMBER == '')	{RELEASE_NUMBER = "[REVISION_NUMBER]";}
		if(INSEAD_NUMBER == '')		{INSEAD_NUMBER = "[INSEAD_NUMBER]";}
		
		ref = ref+"-"+CASESERIES_PART+"-"+DOCUMENTTYPE_KEY+"-"+LANGUAGE_CODE+"-"+RELEASE_NUMBER+"-"+INSEAD_NUMBER;

		
		document.forms[the_form].elements["DOCUMENT_REF"].value = ref;
}

function hidePart(action){
	if(action == "edit"){
		if(document.getElementById("CASESERIES_YN").checked  == false){hideDetails("CASESERIES_PART");}
		else{showDetails("CASESERIES_PART");}
	}
}

/*
function asciiEncode(string){
	string = string.replace("&",escape("&"));
	return string
}

/**
*
* Base64 encode / decode
* http://www.webtoolkit.info/
*
**/

/*
var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;

            output = output + String.fromCharCode(chr1);

            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }

        }

        output = Base64._utf8_decode(output);

        return output;

    },

    // 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;
    }

}
*/		


                        