
function justifyMenu() {
    var menuItems  = $$("#homeMenu li");
    var menuWidth  = $("homeMenu").getWidth();
    var totalWidth = 0;

    menuItems.each(function(e) {
        totalWidth += e.getWidth();
    });

    var margin = (menuWidth - 4 - totalWidth) / (menuItems.length - 1);
    margin = parseInt(margin);

    menuItems.each(function(e) {
        e.setStyle({ marginRight: margin + 'px' });
    });

    menuItems[menuItems.length - 1].setStyle({ marginRight: '0' });
}


/***********************************************
* Drop Down/ Overlapping Content- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function getposOffset(overlay, offsettype){
var totaloffset=(offsettype=="left")? overlay.offsetLeft : overlay.offsetTop;
var parentEl=overlay.offsetParent;
while (parentEl!=null){
totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
parentEl=parentEl.offsetParent;
}
return totaloffset;
}

function overlay(curobj, subobjstr, opt_position){
if (document.getElementById){
var subobj=document.getElementById(subobjstr)
subobj.style.display=(subobj.style.display!="block")? "block" : "none"
var xpos=getposOffset(curobj, "left")+((typeof opt_position!="undefined" && opt_position.indexOf("right")!=-1)? -(subobj.offsetWidth-curobj.offsetWidth) : 0) 
var ypos=getposOffset(curobj, "top")+((typeof opt_position!="undefined" && opt_position.indexOf("bottom")!=-1)? curobj.offsetHeight : 0)

subobj.style.left=xpos+"px"
subobj.style.top=ypos+"px"
return false
}
else
return true
}

function overlayclose(subobj){
document.getElementById(subobj).style.display="none"
}

/******************* Text Tool Bar ********************************/

function lien(champ) {
  var ie=document.getElementById && document.all
  var ns6=document.getElementById && !document.all
	if (ie) {
  	var str = document.selection.createRange().text;
  } else {
  	var str = document.getSelection();
  }
  that = document.entryform [champ];
  that.focus();
  var lien = prompt("URL:","http://");
  if (lien != null) {
    var sel = document.selection.createRange();
	sel.text = "<a href=\"" + lien + "\">" + str + "</a>";
  }
  return;
}


function format(f, champ)
{
	var str = '';
	var sel = '';
	that = document.entryform [champ];
  that.focus();
	if (window.getSelection)
	{
		str = window.getSelection();
		sel = window.getSelection();
		sel = "<" + f + ">" + str + "</" + f + ">";
	}
	else if (document.getSelection)
	{
		str = document.getSelection();
		sel = document.getSelection();
		sel = "<" + f + ">" + str + "</" + f + ">";
	}
	else if (document.selection)
	{
		str = document.selection.createRange().text;
		sel = document.selection.createRange();
		sel.text = "<" + f + ">" + str + "</" + f + ">";
	}
	else return;
	alert(str + ' -' + sel);
	
  return;
}

function addTag(t,txtareaid){ 

	var isFF = false; 
	var textSelected = false; 
	if(navigator.userAgent.toLowerCase().indexOf("firefox") > 0){ 
		isFF = true; 
	} 

	var myArea = document.getElementById(txtareaid); 
	var begin,selection,end; 
	if (isFF == true){ 
			
		if (myArea.selectionStart!= undefined) {  
			begin = myArea.value.substr(0, myArea.selectionStart);  
			selection = myArea.value.substr(myArea.selectionStart, myArea.selectionEnd - myArea.selectionStart);  
			end = myArea.value.substr(myArea.selectionEnd); 
			if (selection.length > 0){ 
				textSelected = true; 
			} 
		} 
	}else{ 
		if (window.getselection){ 
			selection = window.getselection(); 
		}else if (document.getSelection){ 
			selection = document.getSelection(); 
		}else if (document.selection){ 
			selection = document.selection.createRange().text; 
		} 
		var startPos = myArea.value.indexOf(selection); 
		var endPos = myArea.value.indexOf(selection) + selection.length; 
		begin = myArea.value.substr(0,startPos); 
		end = myArea.value.substr(endPos, myArea.value.length); 
		if (selection.length > 0) {
			textSelected = true; 
		}

	}

	if(textSelected == true){ 
		switch (t){ 
			case "underline": 
				startTag = "<u>"; 
				endTag = "</u>"; 
				break; 
			
			case "bold":
				startTag = "<b>";
				endTag = "</b>";
				break;
			
			case "italic":
				startTag = "<i>";
				endTag = "</i>";
				break;
				
			case "blockquote":
				startTag = "<blockquote>";
				endTag = "</blockquote>";
				break;
			
			case "link":
			  var lien = prompt("URL:","http://");
				startTag = "<a href=\"" + lien + "\">";
				endTag = "</a>";
				break;
		}
		myArea.value = begin + startTag + selection + endTag + end;
		//myArea.focus();
	}else{
		alert("No text selected.\nNo tags added");
	}
}

/******************* End Text Tool Bar ********************************/

/******************* DDL functions ********************************/

function addOption(selObj, value, text) {
    // copy options from array of [value, pair] arrays to select box
    // IE doesn't work if you use the DOM-standard method, however...
    if (typeof(window.clientInformation) != 'undefined') {
        // IE doesn't take the second "before" parameter...
        selObj.add(new Option(value, text));
    } else {
        selObj.add(new Option(value, text), null);
    }
}
function updateInitialCount(selObj) {
    var opts = selObj.options;
    var count = 0;
    for (var loop=0; loop<opts.length; loop++) {
        if (opts[loop].selected) {
            count++;
        }
    }
    return(count);
}
function copyOptions(sourceDDL,destDDL) {

  var selectedCount = updateInitialCount(sourceDDL);    // Just in case onchange hasn't fired

  //destDDL.options.length = 0;
  if (selectedCount) {
    var opts = sourceDDL.options;
    for (var loop=0; loop<opts.length; loop++) {
        if (opts[loop].selected) {
            addOption(destDDL, opts[loop].text, opts[loop].value);            
        }
    }
    var dest = destDDL.options;
    for (var loop=0; loop<dest.length; loop++) {
    	for (var loop2=0; loop2<opts.length; loop2++) {
        if (dest[loop].value == opts[loop2].value) {
            opts[loop2]=null; 
        }
      }
    }
  } 
}
  
 function move(DDL,sens) { 
 	if (DDL.selectedIndex+sens >= 0 && DDL.selectedIndex+sens < DDL.length) {    
 		var t=DDL[DDL.selectedIndex+sens].text;
 		var v=DDL[DDL.selectedIndex+sens].value;
    DDL[DDL.selectedIndex+sens].text=DDL[DDL.selectedIndex].text;
    DDL[DDL.selectedIndex].text=t;
    DDL[DDL.selectedIndex+sens].value=DDL[DDL.selectedIndex].value;
    DDL[DDL.selectedIndex].value=v;
    DDL.selectedIndex=DDL.selectedIndex+sens; 
  } 
}

function selectAllOptions(selStr) {
  var selObj = document.getElementById(selStr);
  for (var i=0; i<selObj.options.length; i++) {
    selObj.options[i].selected = true;
  }
}
/******************* End DDL functions ********************************/


function displayImage(divId,imgPath) {
	var elem;
	elem = document.getElementById(divId);
	elem.src = imgPath;
}

function displayText(divId,text) {
	var elem;
	elem = document.getElementById(divId);
	elem.innerHTML = text;
}

function escapeVal(stringValue,replaceWith){
	stringValue = escape(stringValue);
	
	for(i=0; i<stringValue.length; i++){

		if(stringValue.indexOf("%0D%0A") > -1){
			stringValue=stringValue.replace("%0D%0A",replaceWith)
		}
		else if(stringValue.indexOf("%0A") > -1){
			stringValue=stringValue.replace("%0A",replaceWith)
		}
		else if(stringValue.indexOf("%0D") > -1){
			stringValue=stringValue.replace("%0D",replaceWith)
		}
	}
	
	return unescape(stringValue);
}



/******************** AJAX Stuff **********************/

function writediv(divid,data) {
	document.getElementById(divid).innerHTML = data;
}

function writeimg(divid,data) {
	document.getElementById(divid).src = data;
}

function getImageDetail(imgId,textId,lImageId,wwwroot) {

	var imgDetail = getFile(wwwroot+'/lib/utils/getImageDetail.php?lImageId='+lImageId,imgId,wwwroot);
	var imgDetailArray=imgDetail.split("|");
	writeimg(imgId,imgDetailArray[0]);
	writediv(textId,imgDetailArray[1]);
}

function getFile(fichier,cible,wwwroot) {
	
	//netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserAccess');
	
	if(window.XMLHttpRequest) // FIREFOX
	    xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject) // IE
	    xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
	else
	    return(false);

	xhr_object.open("GET", fichier, false);
	xhr_object.send(null);
	/*if(xhr_object.readyState == 4) return(xhr_object.responseText);
	else return(false);*/
	if((xhr_object.readyState == 4) && (xhr_object.status == 200)) {
		try {
			return(xhr_object.responseText);
		} catch (error) {
			alert("Error: "+error.message+" - Obj Status: "+xhr_object.status);
		}
		// Tout s'est bien passé, on sort
		return;
	}
	// Chargement...
	if(xhr_object.readyState == 1)	{
		document.getElementById(cible).innerHTML = '<img src='+wwwroot+'/lib/img/loading.gif" alt="loading..." border=0/>';
	}
}



function getTemplateObject(lTemplateId,TplType,divId,wwwroot) {

	var TplDetail = getFile(wwwroot+'/lib/utils/getTemplateObject.php?lTemplateId='+lTemplateId+'&TplType='+TplType,divId,wwwroot);
	writediv(divId,TplDetail);
}

function getContainerDDLFromType(lParentId,ContType,divId,wwwroot) {

	var ContDDL = getFile(wwwroot+'/lib/utils/getContainerDDLFromType.php?lParentId='+lParentId+'&ContType='+ContType,divId,wwwroot);
	writediv(divId,ContDDL);
}

function getPostalFee(qty,lCountryId,divId,wwwroot) {
	var data = getFile(wwwroot+'/lib/utils/getPostalFee.php?qty='+qty+'&lCountryId='+lCountryId,divId,wwwroot);
	document.getElementById(divId).value = data;
}


function smoothHeight(id, curH, targetH, stepH, mode) {
  diff = targetH - curH;
  if (diff != 0) {
    newH = (diff > 0) ? curH + stepH : curH - stepH;
    ((document.getElementById) ? document.getElementById(id) : eval("document.all['" + id + "']")).style.height = newH + "px";
    if (smooth_timer) window.clearTimeout(smooth_timer);
    smooth_timer = window.setTimeout( "smoothHeight('" + id + "'," + newH + "," + targetH + "," + stepH + ",'" + mode + "')", 16 );
  }
  else if (mode != "o") ((document.getElementById) ? document.getElementById(mode) : eval("document.all['" + mode + "']")).style.display="none";
}

function togglediv(divId,imgId) {
	if (document.getElementById(divId).style.display == "none") {
		document.getElementById(divId).style.display = "block";
		document.getElementById(imgId).src = '/lib/img/contract.gif';
		//smoothHeight(divId, 0, 300, 10, divId);
	} else {
		document.getElementById(divId).style.display = "none";
		document.getElementById(imgId).src = '/lib/img/expand.gif';
		//smoothHeight(divId, 300, 0, 10, divId);
	}
}

function setBannerImg(wwwroot,imgName) {
	if (imgName == "") {
		document.getElementById('toptable').style.background = "url('" + wwwroot + "/images/index/bgindex01.jpg')";
	} else {
		document.getElementById('toptable').style.background = "url('" + wwwroot + imgName + "')";
	}
} 

function getTemplateUrl(wwwroot,elemValue){
	var url = wwwroot+'/admin/templates.php?mode=update&lTemplateId='+elemValue;
	document.location=url;
}

function crop(img_id, crop_id, x, y, width, height) {  
   $(crop_id).update('<img id="' + crop_id + '_img" src="' +  
       $(img_id).getAttribute('src') + '" style="display:none" />');  
   
   var scale_x = $(crop_id).getWidth() / width;  
   var scale_y = $(crop_id).getHeight() / height;  
   
   $(crop_id).setStyle({  
     position: 'relative',  
     overflow: 'hidden'   
   });  
   
   $(crop_id + '_img').setStyle({  
     position: 'absolute',  
     display: 'block',  
     left: (-x * scale_x) + 'px',  
     top: (-y * scale_y) + 'px',  
     width: ($(img_id).getWidth() * scale_x) + 'px',  
     height: ($(img_id).getHeight() * scale_y) + 'px'  
   });  
} 

function cropimg(imgsrc, crop_id, x, y, width, height) {  
   $(crop_id).update('<img id="' + crop_id + '_img" src="' + imgsrc + '" style="display:none" />');  
   
   var scale_x = $(crop_id).getWidth() / width;  
   var scale_y = $(crop_id).getHeight() / height;  
   
   $(crop_id).setStyle({  
     position: 'relative',  
     overflow: 'hidden'   
   });  
   
   $(crop_id + '_img').setStyle({  
     position: 'absolute',  
     display: 'block',  
     left: (-x * scale_x) + 'px',  
     top: (-y * scale_y) + 'px',  
     width: ($(img_id).getWidth() * scale_x) + 'px',  
     height: ($(img_id).getHeight() * scale_y) + 'px'  
   });  
} 

function resizeImg(id,imgWidth,imgHeight,fileSize) {
	var imgId = 'img_'+id;
	var divId = 'div_'+id;
    if (imgWidth > 400) {
		document.getElementById(imgId).style.width = 400;
		document.getElementById(imgId).style.height = (400*imgHeight)/imgWidth;
	}
	if (imgHeight > 200) {
		document.getElementById(imgId).style.height = 200;
		document.getElementById(imgId).style.width = (200*imgWidth)/imgHeight;
	}

	document.getElementById(divId).innerHTML = imgWidth + ' x ' + imgHeight + ' - (' + fileSize + ' KB)';
}
/*
function correctPNG()
{
	for(var i=0; i<document.images.length; i++) {
		var img = document.images[i]
		var imgName = img.src.toUpperCase()
		if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
			var imgID = (img.id) ? "id='" + img.id + "' " : ""
			var imgClass = (img.className) ? "class='" + img.className + "' " : ""
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
			var imgStyle = "display:inline-block;" + img.style.cssText
			if (img.align == "left") imgStyle = "float:left;" + imgStyle
			if (img.align == "right") imgStyle = "float:right;" + imgStyle
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
			var strNewHTML = "<span " + imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>"
			img.outerHTML = strNewHTML
			i = i-1
		}
	}
}

	window.attachEvent("onload", correctPNG);
*/
