function UserAgent() {
 if ( navigator.userAgent.indexOf("Gecko") == -1 ) {
 return "ie"
 } else {
  if ( navigator.userAgent.indexOf("Safari") == -1 ) {
   return "moz"
  } else {
   return "saf"
  }
 }
}
function trim(str){
   return str.replace(/^\s*|\s*$/g,"");
}
// easier shortcuts for dhtml

function ChangeElementClass(elmid, newclass) {
 document.getElementById(elmid).className = newclass
}

function ChangeElementSrc(elmid, newsrc) {
 document.getElementById(elmid).src = newsrc
}

function ChangeElementValue(elmid, newval) {
 document.getElementById(elmid).value = newval
}

function ChangeElementHtml(elmid, newval) {
 document.getElementById(elmid).innerHTML = newval
}

function ChangeElementPosition(elmid, posx, posy) {
 elem = document.getElementById(elmid)
 elem.style.top = posy
 elem.style.left = posx
}

function ResizeImage(/* DOM Img object */ oImg, /*int */ maxWidth,  /*int */ maxHeight, /* boolean */ centerImage) {
	var width=oImg.clientWidth;
	var height=oImg.clientHeight;
	var newWidth=maxWidth;
	var newHeight=maxHeight;
	var leftMargin=0;
	var topMargin=0
	if (Math.max(width,height) == width) {
		// width is image greater dimension
		var reduce = width / maxWidth;
		newHeight = height / reduce;
		topMargin = (maxHeight - newHeight) / 2;
	} else {
		// height is image greater dimension
		var reduce = height / maxHeight;
		newWidth = width / reduce;
		leftMargin = (maxWidth - newWidth) / 2;
	}
	var style = "width:" + newWidth + "px; height:" + newHeight + "px;";
	if (centerImage) {
		style += " margin-top:" + topMargin + "px; margin-left:" + leftMargin + "px;";
	}
	oImg.setAttribute("style", style);
}

// Return inner screen size (width)
function GetWidth(){
if ( UserAgent() == "ie"){
  return document.body.clientWidth
 } else {
  return window.innerWidth
 }
}

// load an new url in the page 
function Go(url) {
 document.location=url
}

// Crossbrowser Event Handlers
// see http://www.scottandrew.com/weblog/articles/cbs-events
//

function addEvent(obj, evType, fn, useCapture){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, useCapture)
    return true
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn)
    return r
  } else {
    alert("Handler could not be attached")
  }
} 

function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture)
    return true
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn)
    return r
  } else {
    alert("Handler could not be removed")
  }
}

function EvalUrl(url) {
document.body.style.cursor = 'wait'; 
 var xmlhttp = ""
 try {
  xmlhttp = new XMLHttpRequest()
  //netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead")
 } catch (e) {
  var MSXML_XMLHTTP_PROGIDS = new Array(
   'MSXML2.XMLHTTP.5.0',
   'MSXML2.XMLHTTP.4.0',
   'MSXML2.XMLHTTP.3.0',
   'MSXML2.XMLHTTP',
   'Microsoft.XMLHTTP'
   )

  var success = false
  for (var i=0; i < MSXML_XMLHTTP_PROGIDS.length && !success; i++) {
   try {
    xmlhttp = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i])
    success = true
   } catch (e) {}
  }
 }

 xmlhttp.open('GET', url, true)

 xmlhttp.onreadystatechange = function() {
  if ( xmlhttp.readyState == 4 ) {
   eval(xmlhttp.responseText);
   document.body.style.cursor = 'default'; 
  }
 }

 // xmlhttp.setRequestHeader('Accept-Charset','UTF-8')
 // xmlhttp.setRequestHeader('Accept-Language','en-us')
 // xmlhttp.setRequestHeader('Connection','Close')

 xmlhttp.send(null)

}

// fetch URL content into javascript dom element
//
// url     :  url to fetch 
// elmid   :  id of the html element to set result on
// type    :  type of value to return ( 'html' or  'value' , default to 'html')
//
function FetchUrlInto(url, elmid, type) {

 if ( ! type ) {
  type = 'html'
 }

 var xmlhttp = ""
 try {
  xmlhttp = new XMLHttpRequest()
  //netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead")
 } catch (e) {
  var MSXML_XMLHTTP_PROGIDS = new Array(
   'MSXML2.XMLHTTP.5.0',
   'MSXML2.XMLHTTP.4.0',
   'MSXML2.XMLHTTP.3.0',
   'MSXML2.XMLHTTP',
   'Microsoft.XMLHTTP'
   )

  var success = false
  for (var i=0; i < MSXML_XMLHTTP_PROGIDS.length && !success; i++) {
   try {
    xmlhttp = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i])
    success = true
   } catch (e) {}
  }
 }

 xmlhttp.open('GET', url, true)

 xmlhttp.onreadystatechange = function() {
  if ( xmlhttp.readyState == 4 ) {
   acceptResponse(xmlhttp, elmid, type)
  }
 }

 // xmlhttp.setRequestHeader('Accept-Charset','UTF-8')
 // xmlhttp.setRequestHeader('Accept-Language','en-us')
 // xmlhttp.setRequestHeader('Connection','Close')

 xmlhttp.send(null)

}

function acceptResponse(xmlhttp, eid, type) {
 var elem = document.getElementById(eid)
 if ( type == 'html' ) {
  elem.innerHTML  = xmlhttp.responseText
 } else {
  elem.value  = xmlhttp.responseText
 }
}
function Exception (message) {
	this.message = message;
	
	this.toString = function () {
		return this.message;
	}
}
var __XMLHTTP__ = null;

function XMLRequest (url, type) {
	this.url = url;
	this.type = type ? type : 'html';
	this.handler = null;
	
	this.setHandler = function (handler) {
		this.handler = handler;
	}
	
	this.makeRequest = function () {
		success = false;
	 	xmlhttp = this.getXmlHttpConnector(); 	
		if (xmlhttp != null) {
		 	xmlhttp.open('GET', url, true)
			// xmlhttp.setRequestHeader('Accept-Charset','UTF-8')
	 		// xmlhttp.setRequestHeader('Accept-Language','en-us')
	 		// xmlhttp.setRequestHeader('Connection','Close')
		 	xmlhttp.onreadystatechange = this.handler.getResponseWaiter();
			xmlhttp.send(null);	
		} else { alert ("Error on opening connection");}

	 }
	 
	 this.getXmlHttpConnector = function () {
	 	if (__XMLHTTP__ == null) {
	 		var success = false;
		 	try {
		  		__XMLHTTP__ = new XMLHttpRequest();
		  		success = true;
		  		//netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead")
		 	} catch (e) {
		  		var MSXML_XMLHTTP_PROGIDS = new Array(
			   	'MSXML2.XMLHTTP.5.0',
			   	'MSXML2.XMLHTTP.4.0',
			   	'MSXML2.XMLHTTP.3.0',
				'MSXML2.XMLHTTP',
		   		'Microsoft.XMLHTTP');
		   		
	   			
		  	
			  	for (var i=0; i < MSXML_XMLHTTP_PROGIDS.length && !success; i++) {
			   		try {
			    		__XMLHTTP__ = new ActiveXObject(MSXML_XMLHTTP_PROGIDS[i])
			    		success = true
			   		} catch (e) {
			   		}
			   	}
		   	}
		   	if (!success) {
				throw new Exception ("No XMLHTTP connexion available in your browser");
			}
		} else {
			success = true;
		}
		
		
		return __XMLHTTP__;
	 }
}

// update image associated to input file
// do not work with mozilla without signed scripts !!
//
function UpdateImageInput(obj, target) {
 if ( UserAgent() == "moz" ) {
  pimg2="template/default/images/newimage.gif"
 } else {
  pimg1 = obj.value
  pimg2 = "file://"+pimg1
 }
 //alert(pimg2);
 ChangeElementSrc(target, pimg2)
}

function containsValue (/* object */ key) {
	var i = 0; 
	for (i = 0; i < this.length; i++) {
		if (this[i] = key) {
			return true;
		}
	}
	return false;
}

// adding containsValue method to javascript standard Array
Array.prototype.containsValue = containsValue

