/**
 *
 */
 function returnSize() {
 	var myWidth = 0, myHeight = 0;
 	if( typeof( window.innerWidth ) == 'number' ){
  //Non-IE
  myWidth = window.innerWidth;
  myHeight = window.innerHeight;
 	}
 	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)){
  //IE 6+ in 'standards compliant mode'
  myWidth = document.documentElement.clientWidth;
  myHeight = document.documentElement.clientHeight;
 	}else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
  //IE 4 compatible
  myWidth = document.body.clientWidth;
  myHeight = document.body.clientHeight;
 	}
 	var result = new Array(myWidth,myHeight);
 	return result;
 }
 
 /**
 *
 */
 function windowCenter(myId) {
 	//arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
 	//return arrayPageSize;
  
 	result=returnSize();
 	result2=getPageSize();
 	var myWidth=result[0];
 	var myHeight=result[1];
 	var elementWidth=$(myId).offsetWidth;
 	var elementHeight=$(myId).offsetHeight;
 	
 	var myScrollLeft=document.body.scrollLeft;
 	var myScrollTop=document.body.scrollTop;
 	
 	var posX=myScrollLeft+myWidth-myWidth/2-elementWidth/2;
 	var posY=myScrollTop+myHeight-myHeight/2-elementHeight/2;
 	$(myId).setStyle({ 'left': posX+'px',  'top': posY+'px'});
	}
 
 /**
 *
 */
 function displayCenter(id){
 	windowCenter(id);
 	$(id).show();
 	windowCenter(id);
 }

	/**
	*
	*/
	function getPageSize(){
 
 var xScroll, yScroll;
 
 if (window.innerHeight && window.scrollMaxY) {	
 	xScroll = window.innerWidth + window.scrollMaxX;
 	yScroll = window.innerHeight + window.scrollMaxY;
 } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
 	xScroll = document.body.scrollWidth;
 	yScroll = document.body.scrollHeight;
 } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
 	xScroll = document.body.offsetWidth;
 	yScroll = document.body.offsetHeight;
 }
 
 var windowWidth, windowHeight;
 
	//	console.log(self.innerWidth);
	//	console.log(document.documentElement.clientWidth);

 if (self.innerHeight) {	// all except Explorer
 	if(document.documentElement.clientWidth){
  windowWidth = document.documentElement.clientWidth; 
 	} else {
  windowWidth = self.innerWidth;
 	}
 	windowHeight = self.innerHeight;
 } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
 	windowWidth = document.documentElement.clientWidth;
 	windowHeight = document.documentElement.clientHeight;
 } else if (document.body) { // other Explorers
 	windowWidth = document.body.clientWidth;
 	windowHeight = document.body.clientHeight;
 }	
 
 // for small pages with total height less then height of the viewport
 if(yScroll < windowHeight){
 	pageHeight = windowHeight;
 } else { 
 	pageHeight = yScroll;
 }

	//	console.log("xScroll " + xScroll)
	//	console.log("windowWidth " + windowWidth)

 // for small pages with total width less then width of the viewport
 if(xScroll < windowWidth){	
 	pageWidth = xScroll; 
 } else {
 	pageWidth = windowWidth;
 }
	//	console.log("pageWidth " + pageWidth)

 arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
 return arrayPageSize;
	}

/**
*
*/	
function delConfirm(){
	if(confirm('You sure?')){
		return true;
	}
	else{
		return false;
	}
}


/**
* Script to handle DOM manipulations
*/
var aInitArray= new Array();

/**
* Construction of initial array
*/
function buildInitArray(Selectid) {
	aInitArray[0]=$A($(Selectid+'1').options).clone() ;
}

/**
* Add a drop down menu
* selectname supp[]
* div class name suppliers
* div id name div_supp
* selectDefaultChoice Supplier
* selectId supp
* selectClassName 
*/
function addSelect(valeur,selectDefaultChoice,selectName,selectId,selectClassName,divClassName,divIdName){

	var array_type = document.getElementsByClassName(divClassName);
	var newselect = document.createElement('select');
	newselect.setAttribute('name',selectName);
			
	if(valeur==0) Select_Value_Set(newselect, 0);	
	new Insertion.After(array_type[array_type.length-1],'<div class="'+divClassName+'"></div>');
	array_type = document.getElementsByClassName(divClassName);
			
	newselect.options[0] = new Option(selectDefaultChoice+array_type.length+" : ", 0);
	for(j=1;j<aInitArray[0].length;j++){
		newselect.options[j] = new Option(aInitArray[0][j].text, aInitArray[0][j].value);
	}
			
	array_type[array_type.length-1].appendChild(newselect);
	array_type[array_type.length-1].setAttribute('id',divIdName+array_type.length);
	if(valeur!=0) Select_Value_Set(newselect,valeur);
	
	newselect.setAttribute('id',selectId+array_type.length);
	
	new Insertion.Before(newselect,' <a href="javascript:delSelect('+array_type.length+',\''+selectDefaultChoice+'\',\''+selectId+'\',\''+divClassName+'\',\''+divIdName+'\');" id="del_'+array_type.length+'"><img src="images/rem.gif" alt="rem" title="Remove" /></a> ');
	new Insertion.After(newselect,' <a href="javascript:addSelect(0,\''+selectDefaultChoice+'\',\''+selectName+'\',\''+selectId+'\',\''+selectClassName+'\',\''+divClassName+'\',\''+divIdName+'\');"><img src="images/add.gif" alt="add" title="Add" /></a>');
	
	$(selectId+array_type.length).addClassName(selectClassName);
	
}

function delSelect(number,selectDefaultChoice,selectId,divClassName,divIdName){
	//Remove target div
	$(divIdName+number).remove();
	
	//Then decrements all the id and name to match
	$$('.'+divClassName).each(function(element){
		var iter = element.id.gsub(divIdName, '');
		if(iter>number){
			//reach the elements
			var delLink = $('del_'+iter);
				delLink.setAttribute('id','del_'+parseInt(iter-1));
				delLink.setAttribute('href',"javascript:delSelect("+parseInt(iter-1)+",'"+selectDefaultChoice+"','"+selectId+"','"+divClassName+"','"+divIdName+"');");
			var selectElement = $(selectId+iter);
				selectElement.options[0].text = selectDefaultChoice+parseInt(iter-1)+" :";
				selectElement.setAttribute('id',selectId+parseInt(iter-1));
			var divElement = $(divIdName+iter);
				divElement.setAttribute('id',divIdName+parseInt(iter-1));
		}
	});
}

/**
*
*/
function multiToString(formName,selectClassName,outputName){
	var array_multi = document.getElementsByClassName(selectClassName);
	var stringOutput = "";
	
	for(i=0;i<array_multi.length;i++){
		if(array_multi[i].value!=0 && array_multi[i].value!="0"){
			if(stringOutput != "") stringOutput+= ",";
				stringOutput+= array_multi[i].value;
		}
	}
	
	$(outputName).value = stringOutput;
	
	document.forms[formName].submit();
}

/**
* Assigne l'option selected selon la value (et non l'index!!!) passé
* @param selectid, valeur
*/
function Select_Value_Set(Select, Value) {  
	  for(index = 0; index < Select.length; index++) {
	   if(Select[index].value == Value)
	     Select.selectedIndex = index;
	   }
}

/**
*
*/
function afficheCentre(id){
	windowCenter(id);
	$(id).show();
	windowCenter(id);
}

/**
*
*/
function showLoad(){
	if(!$('divLoad')){
		var objBody = document.getElementsByTagName("body").item(0);
		var objOverlay = document.createElement("div");
		objOverlay.style.display = 'none';
		var divLoad = document.createElement('div');
				
		objBody.appendChild(objOverlay);
					
		objOverlay.setAttribute('id','overlay');
		var arrayPageSize = getPageSize();
		$('overlay').setStyle({'z-index':'100','background-color':'#cbfaa9','width':arrayPageSize[0]+'px','height':arrayPageSize[1]+'px','position':'absolute','top':'0px','left':'0px'});
				
		new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.7 });
				
		objBody.appendChild(divLoad);
				
		divLoad.setAttribute('id','divLoad');
		$('divLoad').setStyle({'background-color':'#fff','color':'#000','position':'absolute','text-align':'center','padding':'10px','border':'1px solid #000','z-index':'101'});	
		$('divLoad').update('<img src="images/loading.gif" alt="loading" title="" id="loadingIcon"  style="margin-right:10px;" /> Loading...');
				
		afficheCentre('divLoad');
}				
	else{
		var arrayPageSize = getPageSize();
		$('overlay').setStyle({'z-index':'100','background-color':'#0082d1','width':arrayPageSize[0]+'px','height':arrayPageSize[1]+'px','position':'absolute','top':'0px','left':'0px'});
		new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.7 });
		afficheCentre('divLoad');
	}
}

/**
*
*/
function sendMail(){
		//var filename="../scripts_async/gestionCollection.php?"+params.toQueryString();
		var filename="up_SDOrder_sendMail.cfm";
		
		var ajax = new Ajax.Request(filename,
		{method: 'post',
			onSuccess: function (requete){	
				alert('The mail has been sent');
			}
		});
}

function setCookie(cookieName, cookieValue){
	document.cookie = escape(cookieName) + '=' + escape(cookieValue);
};
				
function getCookie(cookieName) {
	
	var cookieValue = '';
	var posName = document.cookie.indexOf(escape(cookieName) + '=');
	if (posName != -1) {
		var posValue = posName + (escape(cookieName) + '=').length;
		var endPos = document.cookie.indexOf(';', posValue);
		if (endPos != -1) cookieValue = unescape(document.cookie.substring(posValue, endPos));
		else cookieValue = unescape(document.cookie.substring(posValue));
	}
	return (cookieValue);
};