function Field() {
}
Field.prototype._init = function(oVals) {
  this.event_listeners = {}
  this.custom_events = []
  for (var arg in oVals) {
    switch(arg) {
      case "id" :
      case "name" :
        this[arg] = oVals[arg]
        break
      default :
        this.setAttribute(arg,oVals[arg])
    }
  }
}
Field.prototype.addCustomEventListener = function(sEvent, fCallback) {
  if (this.custom_events.indexOf(sEvent) != -1) {
    if (!(sEvent in this.event_listeners)) {
      this.event_listeners[sEvent] = []
    }
    this.event_listeners[sEvent].push(fCallback)
  } else {
    addEventListener(this, sEvent , fCallback, true)
  }
}
Field.prototype.removeCustomEventListener = function(eElement, sEvent, fCallback) {
  if (this.custom_events.indexOf(sEvent) != -1) {
    var evl = this.event_listeners[sEvent]
    for (var i=0, j=evl.length;  i < j;i++) {
      if (evl[i] == fCallback) {
        this.event_listeners.splice(i,1)
        break
      }
    }
  }
}
Field.prototype.sendCustomEvent = function(sEvent) {
   if (sEvent in this.event_listeners) { 
    var evl = this.event_listeners[sEvent]
    for (var i=0, j=evl.length; i < j; i++) {
      (evl[i])({"target":this});
    }
  }
}
Field.prototype._needsAppend = function() {
  return true
}

function setFieldValue(fld, value) {
  if ("setValue" in fld) {
    fld.setValue(value)
  } else {
    fld.value = value
  }
}

function getFieldValue(fld) {
  if ("getValue" in fld) {
    return fld.getValue()
  } else {
    return fld.value
  }
}

var mouse_coords
function mouseMove(ev) {
  ev = ev || window.event
  mouse_coords=mouseCoords(ev)
}
function mouseCoords(ev){
  if("pageX" in ev){
    return {x:ev.pageX, y:ev.pageY};
  }
  return {
    x:ev.clientX + body.scrollLeft - body.clientLeft,
    y:ev.clientY + body.scrollTop  - body.clientTop
  };
}
document.onmousemove=mouseMove


function ComplexField() {
  Field.apply(this, arguments)
}
ComplexField.prototype = new Field
ComplexField.prototype._init = function(oVals) {
  this.event_listeners = {}
  this.custom_events = []
  this.own_events=[]
  for (var arg in oVals) {
    switch(arg) {
      case "id" :
        this[arg] = oVals[arg]
        break
      case "name" :
        this.name = oVals.name
      default :
        this.setMainFieldAttribute(arg,oVals[arg])
    }
  }
}
ComplexField.prototype.setMainFieldAttribute = function(sName, sValue) {
  if ("setMainFieldAttribute" in this.main_field) {
    this.main_field.setMainFieldAttribute(sName, sValue)
  } else {
    switch (sName) {
      case "name" :
        this.main_field[sName] = sValue
        break
      default :
        this.main_field.setAttribute(sName,sValue)
    }
  }
}
ComplexField.prototype.setValue = function(val) {
  setFieldValue(this.main_field, val)
  if ("setLinkedValues" in this.main_field) {
    this.main_field.setLinkedValues()
  }
}
ComplexField.prototype.getValue = function() {
  return getFieldValue(this.main_field)
}
ComplexField.prototype.setInitialValue = ComplexField.prototype.setValue
 
ComplexField.prototype.linkToDictionnary = function(dic,key, formatter) {
  this.main_field.linkToDictionnary.apply(this.main_field, arguments)
}
ComplexField.prototype.addCustomEventListener = function(sEvent, fCallback) {
  if (this.own_events.indexOf(sEvent) != -1) {
    Field.prototype.addCustomEventListener.call(this, sEvent, fCallback)
  } else {
    addEventListener(this.main_field, sEvent, fCallback)
  }
}

var xHttpAlertRequest;
function makeAlertRequest(sUrl, fCallback) {
  xHttpAlertRequest = false;
  bRequestActive = true;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    xHttpAlertRequest = new XMLHttpRequest();
    if (xHttpAlertRequest.overrideMimeType) {
      xHttpAlertRequest.overrideMimeType('text/xml');
    }
  } else if (window.ActiveXObject) { // IE
    try {
      xHttpAlertRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xHttpAlertRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
     }
  }
  if (!xHttpAlertRequest) {
    alert('Cannot create XMLHTTP instance');
    return false;
  }
  xHttpAlertRequest.onreadystatechange = fCallback;
  xHttpAlertRequest.open('GET', sUrl, true);
  xHttpAlertRequest.send(null);
}
function getAJAXAlerts(aForce) {
  window.setTimeout(getAJAXAlerts,C_ALERTS_JS_REFRESH*1000)
  if (!bAlertOn) {
    makeAlertRequest(
         "/index.php/admin/get_alerts?" + 
         (aForce ? "&force=1" : ""), displayAlerts,false);
  }
}
function hideAlerts() {
  _("alerts_off_div").style.display="block"
  _("alerts_on_div").style.display="none"
  hidePopup() 
}
function displayAlerts() {
  if (xHttpAlertRequest.readyState == 4) {
    if (xHttpAlertRequest.status == 200) {
      var xResponse = xHttpAlertRequest.responseXML.documentElement
      if (xResponse.getAttribute("has_alerts")=="1") {
        _("alerts_on_div").style.display="block"
        _("alerts_off_div").style.display="none"
      }
    }
  }  
  bRequestActive = false
}
function displayAlertsPopup() {
  var xResponse = xHttpAlertRequest.responseXML.documentElement
  inlinePopup(xResponse.getAttribute("title"),
      getHtmlFromXml(xResponse),alertFormSubmitted)  
}
function alertFormSubmitted() {
  submitAlertForm()
  return false
}
function submitAlertForm(sRedirectUrl) {
  if (sRedirectUrl==null) {
    sRedirectUrl=""
  }
  if(_("syal_has_fields").value=="1") {
    _("redirect_url").value=sRedirectUrl
    postAJAXForm(_("frm_sy_alert"), "/index.php/admin/get_alerts?force=1", onAlertFormSubmitted)
  } else {
    if (sRedirectUrl) {
      window.location = sRedirectUrl
    } else {
      hidePopup()
    }
  }
}
function onAlertFormSubmitted(xResponse) {
   if (xResponse.documentElement.getAttribute("error")=="1") {
     displayAlertsPopup()
   } else {
     if (xResponse.documentElement.getAttribute("redirect_url")!="") {
       window.location = xResponse.documentElement.getAttribute("redirect_url")
    } else {
       hideAlerts()
     }
  }
}


if (!("indexOf" in Array.prototype)) {
  Array.prototype.indexOf = function(el) {
    var ret=-1;
    for (var i=0,j=this.length; i < j; i++) {
      if (this[i]===el) {
        ret = i
        break
      }
    }
    return ret
  }
}
Array.prototype.equals = function(array2) {
  var ret
  if (array2.length == this.length) {
    ret = true;
    for (var i=0, j=this.length; i < j ; i++) {
      if (array2.indexOf(this[i]) == -1) {
        ret=false
        break
      }
    }
  } else {
    ret = false
  }
  return ret
}

function CountrySelect() {
  Field.apply(this, arguments)
}
CountrySelect.prototype = new Field

CountrySelect.prototype._createBaseElement = function() {
  return document.createElement("select")
}
CountrySelect.prototype._init = function(oAttrs) {
  this.custom_events = []
  this.event_listeners = {}
  if ("possible_values" in oAttrs) {
    HTMLSelectElement.prototype.populate.call(this, oAttrs.possible_values)
    delete oAttrs.possible_values
  }
  if ("state_select" in oAttrs) {
    this.setStateSelect(oAttrs.state_select)
    delete oAttrs.state_select
  }
  if ("state2_input" in oAttrs) {
    this.state2_input = ((typeof oAttrs.state2_input) == "string") 
                      ? _(oAttrs.state2_input) : oAttrs.state2_input
  }
  if ("default_values" in oAttrs) {
    this.default_values = oAttrs.default_values
    delete oAttrs.default_values
  }
  Field.prototype._init.call(this, oAttrs)
}
CountrySelect.prototype.setStateSelect = function(state_select) {
  if ((typeof state_select) == "string") {
    state_select = _(state_select)
  } 
  this.state_select = state_select
  addEventListener(this,"change",(function(fld) { return function(ev) {
        fld.setState()
        fld.setError()
      }})(this))
  addEventListener(this.state_select,"change",(function(fld) { return function(ev) {
        fld.setError()
      }})(this))
}
CountrySelect.prototype.setState = function() {
  if ("state_select" in this) {
    if (!("orig_classname_sts" in this)) {
      this.orig_classname_sts = this.state_select.className  
    }
    if ((this.value == "CA") || (this.value == "US")) {
      this.state_select.removeAttribute("disabled")
      this.state_select.className = this.orig_classname_sts 
    } else {
      this.state_select.className = this.orig_classname_sts + " disabled"
      setFieldValue(this.state_select, "")
      this.state_select.setAttribute("disabled", "disabled")
    }
  }
  if ("state2_input" in this) {
    if ((this.value == "CA") || (this.value == "US")) {
      setFieldValue(this.state2_input,"")
      this.state2_input.style.visibility="hidden"
    } else {
      this.state2_input.style.visibility="visible"
    }
  }
}
CountrySelect.prototype.setError = function() {
  if ("state_select" in this) {
    this.error = ((this.value == "CA") || (this.value == "US")) && 
                 ((this.state_select.value == "") || 
                  (this.state_select.value == "__") ||
                  (this.state_select.value == "--"))
    if (!("orig_color" in this)) {
      this.orig_color = this.style.color
    } 
    this.style.color = this.error ? "red" : this.orig_color
    this.state_select.style.color = this.error ? "red" : this.orig_color
  }
}
CountrySelect.prototype.getError = function() {
  return this.error 
}
CountrySelect.prototype.setInitialValue = function(val) {
  if ((val == null) && ("default_values" in this)) {
    val = this.default_values.pays
    if ("state_select" in this) {
      setFieldValue(this.state_select, this.default_values.province)
    }
    if ("state2_input" in this) {
      setFieldValue(this.state2_input, this.default_values.province2)
    }
  }
  setFieldValue(this, val)
  this.setState()
}

var rIsoDateRegexp = new RegExp(/^([0-9]{4})-0?([0-9]{1,2})-0?([0-9]{1,2})(?: 0?([0-9]{1,2}):0?([0-9]{1,2}):0?([0-9]{1,2}))?$/)
var rIsoTimeRegexp = new RegExp(/([0-1]?[0-9]|2[0-3])[^0-9]*((?:[0-6]?[0-9])?)/i)

function getLocaleDate(dDate, bGetDay) {
  if (bGetDay == null) {
    bGetDay = false
  }
  return (bGetDay ? T_WEEKDAYS[dDate.getDay()] + " " : "") + 
         dDate.getDate() + " " + T_MONTHS[dDate.getMonth()] + " " + dDate.getFullYear()
}
function getLocaleDateTime(dDate) {
  return getLocaleDate(dDate) + " " + getLocaleTime(dDate)
}
function getLocaleTime(dDate) {
  return zeroPad(dDate.getHours()) + ":" + zeroPad(dDate.getMinutes())
}

function getIsoDateFromDate(dDate, bFull) {
  if (bFull == null) {
    bFull = true
  }
  return dDate.getFullYear() + "-" + zeroPad(dDate.getMonth()+1) + "-" + zeroPad(dDate.getDate()) + 
    (bFull ? " " + zeroPad(dDate.getHours()) + ":" + zeroPad(dDate.getMinutes()) + ":" + zeroPad(dDate.getSeconds())
           : "")
}
function getDateFromIsoDate(sDate) {
  var r
  if (rIsoDateRegexp.test(sDate)) {
    var aParts = rIsoDateRegexp.exec(sDate) 
    if (aParts[4]) {
     r = new Date(aParts[1],aParts[2]-1,aParts[3],aParts[4],aParts[5],aParts[6])
    } else {
     r = new Date(aParts[1],aParts[2]-1,aParts[3],0,0,0)
   }
  } else {
    r = new Date()
    r.setHours(0)
    r.setMinutes(0)
    r.setSeconds(0)
    r.setMilliseconds(0)
  }
  return r
}
function parseDate(sDate, bFull) {
  if (bFull == null) {
    bFull = false
  }
  return getIsoDateFromDate(getDateFromIsoDate(sDate), bFull)
}

function parseTime(aTime) {
  if (rIsoTimeRegexp.test(aTime)) {
    var parts = rIsoTimeRegexp.exec(aTime)
    aTime = zeroPad(parts[1]) + ":" + zeroPad(parts[2])
  } else {
    aTime = "00:00"
  }
  return aTime
}
function DateField() {
  ComplexField.apply(this)
}
DateField._index = 0
DateField.prototype = new ComplexField
DateField.prototype._createBaseElement = function() {
  var ret = document.createElement("table")
  ret.className = "noborder_table"
  ret.style.width = "100px"
  ret.modified = false
  var trRow = ret.appendChild(document.createElement("tr"))
  ret.main_field = trRow.appendChild(document.createElement("td")
    ).appendChild(document.createElement("input"))
  ret.main_field.style.width = "80px"
  ret.main_field.setValue = getDelegate(ret.main_field, function(aValue) {
      this.value = aValue.split(" ").shift()
    })
  addEventListener(ret.main_field, "change", function(ev) {
    this.value = parseDate(this.value)
    ret.modified = true
  })
  addEventListener(ret.main_field, "focus", function(ev) {
      if(this.value==T_DATE_FORMAT){ this.value=''; }
    })
  var aLink = trRow.appendChild(document.createElement("td")
    ).appendChild(document.createElement("a"))
  aLink.href = "#"
  DateField._index++
  var fname = "setValue" + DateField._index
  DateField[fname] =  function(val) {
    ret.modified = true
    ret.setValue(val)
  }
  addEventListener(aLink,"click", function(ev) {
      showDatePicker_Func("DateField." + fname, ret.main_field)
      if (ev) {
        ev.preventDefault()
      }
      return false
    })
  var iImage = aLink.appendChild(document.createElement("img"))
  iImage.src = T_GLOBAL_IMAGE_PATH + "calendar.jpg"
  return ret
}

DateField.prototype.setInitialValue = function(val) {
  this.main_field.value = ((val == null) || (val == "NOW"))
                   ? getIsoDateFromDate(new Date(),false)
                   : val
}


function getDelegate(aContext, aFunction) {
  return function() {
    aFunction.apply(aContext, arguments)
  }
}function toggleFieldset(aFieldsetId,aShowFieldsetId,aVisible,aOnToggle) {
  var element = _(aFieldsetId)
  if (aVisible==null) {
    aVisible=(element.style.display=="none")
  }
  makeRequest(
      "/index.php/scripts/set_fieldsettoggle?id=" +
      aFieldsetId + "&visible=" + (aVisible ? "1" : "0"))
  element.style.display = aVisible ? "block" : "none"
  if (aShowFieldsetId != null) {
    _(aShowFieldsetId).style.display = aVisible ? "none" : "block"
  }
  if (aOnToggle != null) {
    aOnToggle(aVisible)
  }
};
var re_email = new RegExp(/^[a-z0-9\-_.]+@[a-z0-9\-_.]+\.[a-z]{2,3}$/i)


function zeroPad(iInteger) {
  iInteger = "" + iInteger
  while (iInteger.length < 2) {
    iInteger = "0" + iInteger
  }
  return iInteger
}


function printEmail(sDomain, sName,sTld) {
  return sName + "@" + sDomain + "." + sTld
}
function parseCurrency(value) {
  value = parseFloat(value)
  return isNaN(value) ? "0.00" : parseFloat(value).toFixed(2)
}
function formatInt(val, default_value) {
  if (default_value == null) {
    default_value = 0
  }
  val = parseInt(val)
  return isNaN(val) ? default_value : val
}
function formatCurrency(val, default_value) {
  if (default_value == null) {
    default_value = 0.0
  }
  val = parseCurrency(val)
  return isNaN(val) ? default_value : val
}
function formatFloat(val, default_value) {
  if (default_value == null) {
    default_value = 0
  }
  val = parseFloat(val)
  return isNaN(val) ?default_value : val
}
function getRange(length,start) {
  var ret = []
  if (start == null) {
    start = 0
  }
  for (var i=0; i < length; i++) {
    ret.push(start + i)
  }
  return ret
}

function getElementsUrl(aElements) {
  var eFrmElement
  var sRet = ""
  for(var i=0; i < aElements.length; i++) {
    eFrmElement = aElements[i]
    switch(eFrmElement.type) {
      case "elements-Array" :
        sRet = sRet + getElementsUrl(eFrmElement)
        break
      case "checkbox" :
      case "option" :
        if (eFrmElement.checked) {
          sRet = sRet + "&" + escape(eFrmElement.name) + "=" + escape(eFrmElement.value)
        }
        break
      default :
        sRet =  sRet + "&" + escape(eFrmElement.name) + "=" + escape(eFrmElement.value)
    }
  }
  return sRet
}
function postAJAXForm(fForm, sUrl, fCallback, bForce) {
  if(sUrl.indexOf("?")==-1) {
    sUrl = sUrl + "?"
  }
  window.setTimeout(function () {
    makeRequest(sUrl, fCallback, "POST", getElementsUrl(fForm.elements))
  },50)
}

document.createJSField = function(field_class, oAttrs) {
  if (oAttrs == null) {
    oAttrs = {}
  }
  var ret = field_class.prototype._createBaseElement()
  document.setFieldType(ret, field_class, oAttrs)
  return ret
}
document.setFieldType = function (oField, field_class, oAttrs) {
  if (!(oField instanceof field_class)) {
    for (member in field_class.prototype) {
      oField[member] = field_class.prototype[member]
    }
  }
  oField._init(oAttrs)
}

function submitIfReturn(ev, fCallback) {
  (getEventCallback(function(ev, fCallback) {
    if (ev.keyCode==13) {
      fCallback()
    }
  }))(ev, fCallback)
}function setDivContents(dDiv, tContents) {
  while (dDiv.childNodes.length > 0) {
    dDiv.removeChild(dDiv.firstChild)
  }
  dDiv.appendChild(document.createTextNode(tContents))
}
function getHtmlFromXml(xNode) {
  var xCNode
  var ret = ""
  for(var i=0; i < xNode.childNodes.length ; i++)  {
    xCNode =xNode.childNodes[i]
    switch(xCNode.nodeType) {
      case 1 :
      case 9 :
        ret += getHtmlFromXml(xCNode)
        break;
      case 3 :
      case 4 :
        ret += xCNode.nodeValue
    }
  }
  return ret
}
function disableSelection(eElement) {
    eElement.onselectstart = function() {
        return false;
    };
    eElement.unselectable = "on";
    eElement.style.MozUserSelect = "none";
    eElement.style.cursor = "default";
}

function getFrameDimensions() {
  var ret = {};
  if (self.innerHeight) // all except Explorer
  {
    ret.width = self.innerWidth;
    ret.height = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight)
    // Explorer 6 Strict Mode
  {
    ret.width = document.documentElement.clientWidth;
    ret.height = document.documentElement.clientHeight;
  }
  else if (document.body) // other Explorers
  {
    ret.width = document.body.clientWidth;
    ret.height = document.body.clientHeight;
  }
  return ret
}

function getPosition(obj) {
  var ret = { left:0, top:0 }
  if ((obj != null) && obj.offsetParent) {
    ret.left = obj.offsetLeft
    ret.top = obj.offsetTop
    while (obj = obj.offsetParent) {
      ret.left += obj.offsetLeft
      ret.top += obj.offsetTop
    }
  }
  return ret;
}
function _(id) {
  return document.getElementById(id)
}

function addEventListener(eElement,sEvent, fCallback, bNoCustom) {
  fCallback = getEventCallback(fCallback)
  if ((bNoCustom !== true) && ("addCustomEventListener" in eElement)) {
    eElement.addCustomEventListener(sEvent, fCallback, false)
  } else if ("addEventListener" in eElement) {
    eElement.addEventListener(sEvent, fCallback, false)
  } else {
    eElement.attachEvent("on" + sEvent, fCallback)
  }
}
function getEventCallback(fCallback) {
  if ("event" in window) {
    var fOrigCallback = fCallback
    fCallback = function() {
      var ev = window.event
      ev.target = ev.srcElement
      var a = arguments
      arguments[0] = ev
      fOrigCallback.apply(null,a)
    }
  }
  return fCallback
}
var onload_callbacks = []
function onLoadCallbacks() {
  for (var i=0, j=onload_callbacks.length; i<j; i++) {
    (onload_callbacks[i])()
  }
}

var bAlertOn = false;

function inlinePopup(sTitle, sContents, fSubmitCallback, fCancelCallback, 
                     iMaxWidth, iMaxHeight, iLeftMargin, iTopMargin) {
  var hasCancelCallback=true
  if (fCancelCallback == null) {
    fCancelCallback = fSubmitCallback
    hasCancelCallback=false
  } 
  if  (iMaxWidth==null) {
    iMaxWidth=C_ALERTS_MAX_WIDTH;
  }
  if  (iMaxHeight==null) {
    iMaxHeight=C_ALERTS_MAX_HEIGHT;
  }
  if  (iLeftMargin==null) {
    iLeftMargin=C_ALERTS_LEFT_MARGIN;
  }
  if  (iTopMargin==null) {
    iTopMargin=C_ALERTS_TOP_MARGIN;
  }
  
  var iHeight = document.all ? document.documentElement.clientHeight : window.innerHeight
  if (iHeight < (2*iTopMargin + 200)) {
    iHeight = 2*iTopMargin + 200
  }
  var iWidth = document.all ? document.documentElement.clientWidth : window.innerWidth
  if (iWidth < (2*iLeftMargin + 200)) {
    iWidth = 2*iLeftMargin + 200
  }
  var iTop = 0
  
  var iSubmitHeight = (fSubmitCallback == null) ?  50 : 200
  var dBack = document.createElement("div")

  dBack.id = "sy_back"
  if (document.all) {
    dBack.style.height = "2000px"
  }

  if (document.all) {
    document.getElementsByTagName("html")[0].style.overflow = "hidden"
  } else {
    document.body.style.overflow = "hidden"
  }
  var dAlertBox = document.createElement("div")
  var iAlertHeight = Math.min(iHeight-(2*iTopMargin),iMaxHeight)
  with (dAlertBox) {
    id = "sy_alertbox"
    style.height = iAlertHeight + "px"
    style.top = Math.round(iTop+ (iHeight-iAlertHeight)/2) + "px"
  }
  document.body.insertBefore(dBack, document.body.firstChild)
  document.body.insertBefore(dAlertBox, document.body.firstChild)
  
    
  var tAlertBox = document.createElement("table")
  with (tAlertBox) {
    style.height = Math.min(iHeight-(2*iTopMargin),iMaxHeight) + "px"
    style.width = Math.min(iWidth-(2*iLeftMargin),iMaxWidth) + "px"
    align = "center"
  }
  dAlertBox.appendChild(tAlertBox)
  var trAlertBox = tAlertBox.insertRow(0)
  var tdAlertBox = trAlertBox.insertCell(0)
  tdAlertBox.vAlign = "top"
  
  var dAlert = document.createElement("div")
  dAlert.id="sy_alert"
  tdAlertBox.appendChild(dAlert)
  
  
  if (sTitle != null) {
    var dAlertHeader = document.createElement("div")
    dAlertHeader.id = "sy_alert_header"
    dAlert.appendChild(dAlertHeader)
    
    var dCloseLink = document.createElement("div")
    dCloseLink.id="sy_alert_close"
    dAlertHeader.appendChild(dCloseLink)
    
    var aCloseLink = document.createElement("a")
    with (aCloseLink) {
      appendChild(document.createTextNode("X"))
      href = "#"
      onclick = (fCancelCallback == null) ? hidePopup : fCancelCallback 
    }
    dCloseLink.appendChild(aCloseLink)  
    var pTitle = document.createElement("p")
    with (pTitle) {
      id = "sy_alert_title"
      style.marginTop="0px"
      innerHTML = sTitle
    }
    dAlertHeader.appendChild(pTitle)
  }
  var wAlertContainer
  if(fSubmitCallback != null) {
    var fAlert = document.createElement("form")
    with (fAlert) {
      id="frm_sy_alert"
      name="frm_sy_alert"
      onsubmit=fSubmitCallback
    }  
    dAlert.appendChild(fAlert)
    wAlertContainer = fAlert
  } else {
    wAlertContainer = dAlert
  }
  var dAlertContents
  if ((typeof sContents) == "string") {
	  dAlertContents = document.createElement("div")
	  with (dAlertContents) {
	    id = "sy_alert_contents"
	    innerHTML = sContents
	    style.height = Math.min(iHeight-(2*iTopMargin)-iSubmitHeight,iMaxHeight-iSubmitHeight) +   "px"
	  }
	} else {
	  dAlertContents = sContents
	}
  wAlertContainer.appendChild(dAlertContents)
  
  if (fSubmitCallback != null) {
    var dAlertSubmit = document.createElement("div")
    dAlertSubmit.id="sy_alert_submit"
    wAlertContainer.appendChild(dAlertSubmit)
    if (hasCancelCallback) {
      var sCancelSubmit = document.createElement("input")
      with (sCancelSubmit) {
        type="button"
        value=T_CANCEL
        onclick=fCancelCallback
      }
      dAlertSubmit.appendChild(sCancelSubmit)
    } 
    var sAlertSubmit = document.createElement("input")
    with (sAlertSubmit) {
      type="submit"
      value=T_SUBMIT
    }
    dAlertSubmit.appendChild(sAlertSubmit)
  }
  
  window.scrollTo(0,0)
  if (document.all) {
    hideSelects()
  }
  bAlertOn = true
}

function iframePopup(sTitle, sUrl,iMaxWidth, iMaxHeight, iLeftMargin, iTopMargin) {
  var ifIframe = document.createElement("iframe")
  ifIframe.src=sUrl
  ifIframe.style.width="100%"
  inlinePopup(sTitle,ifIframe,null,null,iMaxWidth, iMaxHeight, iLeftMargin, iTopMargin)
  ifIframe.style.height=(_("sy_alertbox").offsetHeight + 50) + "px"
}

function hidePopup() {
  if (_("sy_alertbox")) {
	  document.body.removeChild(_("sy_back"))
	  document.body.removeChild(_("sy_alertbox"))
	  if (document.all) {
	    document.getElementsByTagName("html")[0].style.overflow = "auto"
	    showSelects()
	  } else {
	    document.body.style.overflow = "auto"
	  }
	  bAlertOn = false
	}
}




function makeRequest(sUrl, fCallback, sMethod, sPostParams, fErrorCallback) {
  var xHttpRequest = false;
  if (sMethod == null) {
    sMethod = "GET"
  }
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
    xHttpRequest = new XMLHttpRequest();
    if (xHttpRequest.overrideMimeType) {
      xHttpRequest.overrideMimeType('text/xml');
    }
  } else if (window.ActiveXObject) { // IE
    try {
      xHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
     }
  }
  if (!xHttpRequest) {
    alert('Cannot create XMLHTTP instance');
    return false;
  }
  xHttpRequest.onreadystatechange = function() {
    if (xHttpRequest.readyState == 4) {
      if (xHttpRequest.status == 200) {
        var xdoc = xHttpRequest.responseXML.documentElement
        switch (xdoc.tagName) {
          case "parsererror" :
            if (fErrorCallback != null) {
              fErrorCallback({"class":"ParseException"})
            }
            break
          case "SynergyException" :
            if (fErrorCallback != null) {
              var obj = {}
              for (var i=0,j=xdoc.attributes.length; i <j ; i++) {
                obj[xdoc.attributes[i].name] = xdoc.attributes[i].value
              }
              fErrorCallback(obj)
            }
            break
          default :
            if (fCallback != null) {
              fCallback(xHttpRequest.responseXML);
            }
        }
      } else if (fErrorCallback != null) {
        fErrorCallback({"class":"ConnectException","status":xHttpRequest.status})
      }
    }
  }
  if (fErrorCallback != null) {
    xHttpRequest.onerror = fErrorCallback
  }
  xHttpRequest.open(sMethod, sUrl, true);
  if (sMethod=='POST') {
    xHttpRequest.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xHttpRequest.setRequestHeader("Content-length", sPostParams.length);
    xHttpRequest.setRequestHeader("Connection", "close");
  }
  xHttpRequest.send(sPostParams);
}
function readJSONResponse(xDoc) {
  var sCode = getHtmlFromXml(xDoc.documentElement)
  return (sCode == "") ? {} : eval("(" + sCode + ")")
}
function makeJSONRequest(sUrl, fCallback, sMethod, sPostParams, fErrorCallback) {
  var fRealCallback = function(xDoc) {
    fCallback(readJSONResponse(xDoc))
  }
  makeRequest(sUrl, fRealCallback, sMethod, sPostParams, fErrorCallback)
}
function makeDicRequest(sUrl, fCallback, sMethod, sPostParams, fErrorCallback) {
  if (sMethod == null) {
    sMethod = "POST"
  }
  if (sPostParams == null) {
    sPostParams = ""
  }
  makeRequest(sUrl, function(xResponse) {
    var d = Dictionnary.readFromXML(xResponse)
    xResponse=null;
    fCallback(d)
  }, sMethod,sPostParams, fErrorCallback)
}

var aHiddenSelects=new Array()
function hideSelects() {
  aHiddenSelects=new Array()
  var aSelects = document.getElementsByTagName("select")
  for(var i=0 ; i < aSelects.length ; i++) {
    if (aSelects[i].style.visibility != "hidden") {
      aHiddenSelects.push(aSelects[i])
      aSelects[i].style.visibility = "hidden"
    }
  }
}
function showSelects() {
  while (aHiddenSelects.length > 0) {
    var oSel = aHiddenSelects.pop().style.visibility="visible"
  }
}
try {
  HTMLSelectElement.prototype
} catch(ex) {
  function HTMLSelectElement() {
  }
}
HTMLSelectElement.prototype.populate = function(dic) {
  if ("Dictionnary" in window) {
    dic = new Dictionnary(dic)
    var item, iter=dic.getIterator()
    while (item = iter.iterate()) {
      this.options[this.options.length] = new Option(item.item, item.key, false, false)
    }
  } else {
    var i = 0
    for(var key in dic) {
      this.options[i] = new Option(dic[key], key)
      i++
    } 
  }
}

var infoDiv
function showInfo(aInfoDivLink, aInfoDivId) {
  function clearInfoTimeout() {
    if (infoDiv.timer != null) {
      clearTimeout(infoDiv.timer)
      infoDiv.timer = null
    }    
  }
  function hideInfo() {
    if (infoDiv != null) {
      clearInfoTimeout()
      infoDiv.style.display = "none"
      infoDiv == null
    }
  }
  function setInfoTimeout() {
    clearInfoTimeout()
    infoDiv.timer = setTimeout(hideInfo,200)
  }

  if ((infoDiv != null) && (infoDiv.id != aInfoDivId)) {
    hideInfo()
  }
  infoDiv = _(aInfoDivId)
  clearInfoTimeout()
  infoDiv.style.display = "block"
  var pos = getPosition(aInfoDivLink) 
  infoDiv.style.left = (
      pos.left + aInfoDivLink.offsetWidth -
      infoDiv.offsetWidth) + "px"
  infoDiv.style.top = (
      pos.top + aInfoDivLink.offsetHeight 
      ) + "px"
  aInfoDivLink.onmouseout = setInfoTimeout
  infoDiv.onmouseover = clearInfoTimeout
  infoDiv.onmouseout = setInfoTimeout
};
String.prototype.toXMLString = function(bNoQuoteReplace) {
  return this.replace(/[<>&"]/g, function(match) {
    switch(match) {
      case "<" : return "&lt;"
      case ">" : return "&gt;"
      case '"' : return bNoQuoteReplace ? '"' : "&quot;"
      case "&" : return "&amp;"
    }
  })
}
String.prototype.toJSString = function() {
  return '"' + 
        this.replace(/\\|\r?\n|\r|\t|"/g,function(match) {
         switch (match) {
           case '"' : return "\\\""
           case "\r" :
           case "\n" :
           case "\r\n" : return "\\n"
           case "\t" : return "\\t"
           default : return ""
         }
        }) + '"'
}
String.prototype.startswith = function(str) { 
  return this.substr(0,str.length) == str
}
String.TRIM_LEFT=1
String.TRIM_RIGHT=2
String.TRIM_BOTH=3
String.prototype.trim=function(sCharacter,iType) {
  var rValue = this + "" //make sure not to work on a reference 
  if (sCharacter==null) sCharacter = " "
  if (iType==null) iType=String.TRIM_BOTH
  if (iType & String.TRIM_LEFT) {
    rValue = rValue.replace(new RegExp("^" + sCharacter + "+"),"")
  }
  if (iType & String.TRIM_RIGHT) {
    rValue = rValue.replace(new RegExp(sCharacter + "+$"),"")
  }
  return rValue
}

function Taxes(oCompany, oUser, dAmount) {
  this.amount = 1*dAmount
  this.taxe1=0.0
  this.taxe2=0.0
  this.taxe3=0.0
  this.ttc_amount = 1*dAmount 
}
Taxes.getTaxes = function(oCompany, oUser, dAmount) {
  var tClass = Taxes
  if (oCompany.pays in Taxes.implementations) {
    tClass =  Taxes.implementations[oCompany.pays]
  } 
  return new tClass(oCompany,oUser,dAmount)
}
Taxes_CA = function(oCompany,oUser,dAmount) {
  Taxes.apply(this,arguments)
  if (oUser.pays=="CA") {
    switch(oUser.province) {
      case "NB" :
      case "NF" :
      case "NS" :
        this.taxe3 = 1*formatCurrency(0.13*dAmount)
        break
      case "QC" :
        if (oCompany.province=="QC") {
          this.taxe2 = 1*formatCurrency(0.07875*dAmount)
        }
      default :
        this.taxe1 = 1*formatCurrency(0.05*dAmount)
        break
        
    }
  }
  this.ttc_amount=1*formatCurrency(this.amount+this.taxe1+this.taxe2+this.taxe3)
}

Taxes.implementations = {
  "CA": Taxes_CA
}

