//-----------------------------------------------------------//
//                                                           //
// Version: 2.01, Author: Karol Kolodziej, Date: 15Nov, 2006 //
//                                                           //
//-----------------------------------------------------------//


//--------------------//
//                    //
// FORM RELATED STUFF //
//                    //
//--------------------//

function submitFormOnConfirm(formID, msg)
{
    var form = document.getElementById(formID);
    if (confirm(msg))
      form.submit();
    else
      return false;
}


function gotoUrlOnConfirm(url, msg)
{
    if (confirm(msg))
      location.href = url;
    else
      return false
}


function submitForm(formID, formAction, msg)
{
    var form = document.getElementById(formID);
    if (typeof(formAction) == "undefined")
      return;
    if (typeof(msg) != "undefined")
      if (!confirm(msg)) 
        return;
    form.action = formAction;
    form.submit();
}


function removeQueryString(formID, params)
{
    var form = document.getElementById(formID);
    var formAction = form.action;
    var indx = formAction.indexOf("?");
    if (indx == -1) return;
    
    if (typeof(params) == 'undefined')
    {
      form.action = form.action.substr(0, indx); return;
    }
    
    var paramsOld  = formAction.substr(indx + 1);
    var aParamsOld = convertParamsToSimpleArray(paramsOld, "&");
    var aParamsNew = convertParamsToSimpleArray(params, "&");
    var aParamsOut = new Array(); 
    
    for (var incr = 0; incr <= aParamsOld.length - 1; incr++)
    {
      if (aParamsOld[incr][0] != "")
        if (getSimpleArrayValue(aParamsNew, aParamsOld[incr][0]) == "")      
          aParamsOut.push(aParamsOld[incr]);
    }    

    formAction = formAction.substr(0, indx);
    
    formAction += "?" + (aParamsOut.join("&")).replace(/,/g,"=");
    form.action = formAction;
    
    return form.action;
}


function addQueryString(formID, params)
{
    var form = document.getElementById(formID); 
    var formAction = form.action;
    var paramsOld = "";
    var indx = formAction.indexOf("?");
    if (indx > -1)
      paramsOld = formAction.substr(indx + 1);
      
    var aParamsOut = addParams(paramsOld, params);
    
    if (indx > -1)
      formAction = formAction.substr(0, indx);
    
    formAction += "?" + (aParamsOut.join("&")).replace(/,/g,"=");
    form.action = formAction;
    
    return form.action;
}


function addQueryStringFieldData(formID, paramName, aFieldIDS, delim)
{
    if (typeof(delim) == "undefined") delim = "|";
    
    var fieldVals = "";
    for (var incr = 0; incr <= aFieldIDS.length - 1; incr++)
    {
      var field  = document.getElementById(aFieldIDS[incr]);
      if (field == null) continue;
      
      fieldVals += getFieldValue(field) + delim; 
    }
    if (fieldVals == "") return document.getElementById(formID).action;
    
    fieldVals = fieldVals.substr(0, fieldVals.length - 1);
    
    return addQueryString(formID, paramName + "=" + fieldVals)
}


//-------------------//
//                   //
// CSS MANIPULATION  //
//                   //
//-------------------//

function clearCssProperties(strCss)
{
   if(strCss == "") return "";
   
   var aCssProperty = strCss.split(";");
   var strCssOut = "";
   var incr = 0;
   for (incr = 0; incr < aCssProperty.length; ++incr)
   {
     var aStrCss = aCssProperty[incr].split(":"); 
     strCssOut += aStrCss[0] + ":;"
   }
   strCssOut = strCssOut.substr(0, strCssOut.length - 1);
   return strCssOut;
}


function toggleElementVisibility(id)
{
   var style_display = document.getElementById(id).style.display.toLowerCase();
   setElementCss(id, "display:" + (style_display == "block" ? "none" : "block"));
}


function setElementCss(id, strCss)
{
   var elem = document.getElementById(id);
   _setElementCss(elem, strCss)
}


function _setElementCss(elem, strCss)
{
   if(typeof(elem) == 'undefined') return;
   if(strCss == "") return;
   
   var aCssProperty = allTrim(strCss).split(";");
   var aCssPropertyNew = elem.style.cssText.split(";");
   var incr = 0;
   for (incr = 0; incr < aCssProperty.length; ++incr)
   {
     var aStrCss = allTrim(aCssProperty[incr].replace(/'/g,"")).split(":"); 
     
     var boolFound = false;
     for (incr2 = 0; incr2 < aCssPropertyNew.length; ++incr2)
     {
       var aStrCssNew = allTrim(aCssPropertyNew[incr2].replace(/'/g,"")).split(":");
       if (aStrCss[0].toLowerCase() == aStrCssNew[0].toLowerCase())
       {  
         aCssPropertyNew[incr2] = aStrCssNew[0] + ":" + aStrCss[1];
         boolFound = true; break;
       }
     }
     if (boolFound == false) 
       if ((aCssPropertyNew != null) && (allTrim(aCssPropertyNew[aCssPropertyNew.length - 1]) == ""))
         aCssPropertyNew[aCssPropertyNew.length - 1] = aCssProperty[incr];
       else
         aCssPropertyNew.push(allTrim(aCssProperty[incr]));
     
     if (aStrCss[0].toLowerCase() == "display") 
       eval("elem.style." + aStrCss[0] + "='" + aStrCss[1] + "'");
   }
   elem.style.cssText = aCssPropertyNew.join(";");
}


//-------------------//
//                   //
// XPATH FUNCTIONS   //
//                   //
//-------------------//

function getElementXPath(elem)
{
  var xPath = "";
  var elemSibling = elem;
  var elemPrevSibling = elem;
  var incr = 0;
  
  while ((typeof(elemSibling) != "undefined") && (elemSibling != null))
  {
    var elemTmp = elemPrevSibling.previousSibling;
    if (elemTmp != null)
    { 
      elemPrevSibling = elemTmp;
      if (elemPrevSibling.tagName == elemSibling.tagName) 
        incr++;
    }
    else
    {  
      xPath = ("/" + elemSibling.tagName + "[" + incr + "]") + xPath; 
      if (elemSibling.tagName.toLowerCase() == "form")
        break;
      elemSibling = elemSibling.parentNode;  
      elemPrevSibling = elemSibling;
      incr = 0;  
    }  
  }
  return xPath;
}


function getElementContainer(elem, containerTagName)
{
  var elemSibling = elem;
  var elemPrevSibling = elem;
  var incr = 0;
  
  while ((typeof(elemSibling) != "undefined") && (elemSibling != null))
  {
    var elemTmp = elemPrevSibling.previousSibling;
    if (elemTmp != null)
    { 
      elemPrevSibling = elemTmp;
      if (elemPrevSibling.tagName == elemSibling.tagName) 
        incr++;
    }
    else
    {  
      if (typeof(elemSibling.tagName) == "undefined")
        return null;
      if (elemSibling.tagName.toLowerCase() == containerTagName)
        return elemSibling;
      elemSibling = elemSibling.parentNode;  
      elemPrevSibling = elemSibling;
      incr = 0;  
    }  
  }
  return null;
}


//-------------------//
//                   //
// UTILITY FUNCTIONS //
//                   //
//-------------------//

function showElement(id)
{
   var elem = document.getElementById(id);
   var safari = document.childNodes && !document.all && !navigator.taintEnabled && !accentColorName;
   
   if  (safari)
   {
     if (elem.style.visibility.toLowerCase() == "hidden")
       elem.style.visibility = "visible";
     var attribVal = elem.getAttribute("width_height");
     if (attribVal == null) return;
    
     var aAttribVal = attribVal.split(";");
     if (aAttribVal.length != 2) return;
   
     elem.style.width  = aAttribVal[0];
     elem.style.height = aAttribVal[1];
     
     return;
   }
   
   elem.style.display = "block";
}


function hideElement(id)
{
   var elem = document.getElementById(id);
   
   var safari = document.childNodes && !document.all && !navigator.taintEnabled && !accentColorName;
   
   if (safari)
   {
     elem.setAttribute("width_height", elem.style.width + ";" + elem.style.height);
     elem.style.visibility = "hidden";
     elem.style.width  = "1px";
     elem.style.height = "1px";
     return;
   } 
   
   elem.style.display = "none";
}


function showListElement(aId, index)
{
    if (typeof(index) == "undefined")
    {
        index = 0;
        var elemFirst = document.getElementById(aId[0]);
        var attribVal = elemFirst.getAttribute("list_index");
        if (attribVal != null)
          index = parseInt(attribVal);
        index++;
        if (index >= aId.length)
          index = 0;
        elemFirst.setAttribute("list_index", index);
          
    }
    for (var incr = 0; incr <= aId.length - 1; incr++)
    {
        if (incr == index) showElement(aId[incr]);
        else hideElement(aId[incr]);
    }
}


function initSlideshow(aId, interval)
{
  for (var incr = 0; incr <= aId.length - 1; incr++)
    hideElement(aId[incr]);
  showElement(aId[0]);
  setInterval(function() { showListElement(aId) }, interval);
}


function setElementsInnerHtml(aId, aHtml, boolForceVisible)
{
    for (var incr = 0; incr <= aId.length - 1; incr++)
    {
        setElementInnerHtml(aId[incr], aHtml[incr], boolForceVisible);
    }
}


function setElementInnerHtml(id, html, boolForceVisible)
{
    var elem = document.getElementById(id);
    if (elem == null) return;
    if (boolForceVisible)
      if (elem.style.display.toLowerCase() == "none")
        elem.style.display = "block";
    elem.innerHTML = html;
}


function setElementValue(id, val)
{
    var elem = document.getElementById(id);
    if (elem == null) return;
    elem.value = val;
}


function getFieldValue(field)
{
    if (field == null) return null;
    
    var val = "";
    if(field.type == "select-one")
    {			 
	  if (field.selectedIndex > -1)
	    val = field.options[field.selectedIndex].value;
    }
    else if (typeof(field.length) != "undefined")
    {
        if (field.type == "select-multiple")
        {    
            for (incr = 0; incr <= field.options.length - 1; incr++)
            {
              if (field.options[incr].selected == true)
                val += field.options[incr].value + "#"; 
            }
            if (val.length > 0) val = val.substr(0, val.length - 1);
        }
        else
            for (incr = 0; incr <= field.length - 1; incr++)
            {
              val = field[incr].checked ? field[incr].value : ""; 
              if (val != "") break;
            }
    }
    else if (field.type == "checkbox")
    	val = field.checked ? field.value : ""; 
    else if (field.type == "textarea")
	val = field.value.replace(/[\r\n]+/g, "");
    else
    	val = field.value;
    
    val = allTrim(val);  
    return val;
}


function toggleCheckboxOnFieldValue(field, chkID)
{
   var chk = document.getElementById(chkID);
   chk.checked = allTrim(field.value) != "";
}


function clearFieldsOnCheckboxClicked(chk, formID, fields, msg, boolReverseAction)
{
  boolReverseAction = typeof(boolReverseAction) != "undefined" ? boolReverseAction : false; 
  
  if ((typeof(msg) != "undefined") && (msg != "") && (boolReverseAction ? !chk.checked : chk.checked) && !confirm(msg))
  {  
    chk.checked = !boolReverseAction;
    return;
  }

  if ((boolReverseAction && !chk.checked) || (!boolReverseAction && chk.checked))
  {
    var form = document.getElementById(formID);
    var aFields = fields.split("#");
    for (var incr = 0; incr < aFields.length; incr++)
    {
      var field = form.elements[aFields[incr]];
      field.value = "";
    }
  }
}


function copyFieldsOnCheckboxClicked(chk, formID, fieldsFrom, fieldsTo, msg, boolRestorePrevValue)
{
  if ((typeof(msg) != "undefined") && (msg != "") && (chk.checked) && !confirm(msg))
  {  
    chk.checked = false;
    return;
  }
  
  var form = document.getElementById(formID);
  var aFieldsFrom = fieldsFrom.split("#");
  var aFieldsTo   = fieldsTo.split("#");
  for (var incr = 0; incr < aFieldsFrom.length; incr++)
  {
    var fieldFrom = form.elements[aFieldsFrom[incr]];
    var fieldTo   = form.elements[aFieldsTo[incr]];
    if (fieldTo.getAttribute("orig_value") == null)
      fieldTo.setAttribute("orig_value", getFieldValue(fieldTo)); 
    if (chk.checked)
    {  
      fieldTo.setAttribute("prev_value", getFieldValue(fieldTo)); 
      fieldTo.value = getFieldValue(fieldFrom);
    }
    else
    {
      if ((typeof(boolRestorePrevValue) != "undefined") && (boolRestorePrevValue == true))
        fieldTo.value = fieldTo.getAttribute("prev_value"); 
      else
        fieldTo.value = fieldTo.getAttribute("orig_value"); 
    }
  }
}


function addParams(paramsOld, params)
{
    var aParamsOld = convertParamsToSimpleArray(paramsOld, "&");
    var aParamsNew = convertParamsToSimpleArray(params, "&");
    var aParamsOut = new Array(); 
    
    for (var incr = 0; incr <= aParamsOld.length - 1; incr++)
    {
      if (aParamsOld[incr][0] != "")
        if (getSimpleArrayValue(aParamsNew, aParamsOld[incr][0]) == "")      
          aParamsOut.push(aParamsOld[incr]);
    }
    
    for (var incr = 0; incr <= aParamsNew.length - 1; incr++)
    {
      aParamsOut.push(aParamsNew[incr]);
    }
    return aParamsOut;
}


function getElement(elemID)
{
  return document.getElementById(elemID);
}


function getSelectValue(sel, appendVal)
{
  var val = getSelectOption(sel).value;
  if (val == "-1")
    val = "";
  if ((typeof(appendVal) != "undefined") && val != "")
    val = appendVal + val;
  return val;
}


function getSelectText(sel)
{
  return getSelectOption(sel).text;
}


function getSelectOption(sel)
{
  return sel.options[sel.selectedIndex];
}


function loadChildSelect(selOptionIndx, childID, array, defaultOption)
{
  var childSelect = document.getElementById(childID);
  childSelect.length = 0;
  
  var incr = -1;
  var boolDefaultOption = typeof(defaultOption) != "undefined"; 
  if (boolDefaultOption)
  {
     childSelect.options[++incr] = new Option(defaultOption.split("#")[1], defaultOption.split("#")[0]); 
  }
  for (row = 0; row < array.length; ++row)
    if (selOptionIndx == array[row][0])
    {  
      if (!boolDefaultOption || ((array[row][1] + "#" + array[row][2]) != defaultOption))
        childSelect.options[++incr] = new Option(array[row][2], array[row][1]); 
    }	
}


function convertParamsToSimpleArray(params, delim)
{
  var arr    = params.split(delim);
  var arrOut = new Array(arr.length);
  for (var incr = 0; incr <= arrOut.length - 1; incr++)
  {
    var paramName  = (arr[incr].split("="))[0];
    var paramValue = (arr[incr].split("="))[1];
    arrOut[incr] = new Array(2);
    arrOut[incr][0] = paramName;
    arrOut[incr][1] = paramValue;
  }	
  return arrOut;
}


function createMultiArray(rows, columns)
{
  var arr = new Array(rows);
  for (var incr = 0; incr <= rows - 1; incr++)
  {
    arr[incr] = new Array(columns);
    for (incr2 = 0; incr2 <= columns - 1; incr2++)
    {
      arr[incr][incr2] = "";
    }
  }
  return arr;
}


function getMultiArrayValue(array, searchArg, columnSearched, boolReturnLast)
{
  if (array == null) return null;
  var columns = 1;
  var arrOut = null;
  for (var incr = 0; incr < array.length; ++incr)
  {    
      if (incr == 0) columns = array[incr].length; 
      if (searchArg == array[incr][columnSearched - 1])
      {
         arrOut = array[incr]; 
         if ((typeof(boolReturnLast) == "undefined") || !boolReturnLast)
           return arrOut;
      }
  }
  return arrOut != null ? arrOut : createMultiArray(1, columns)[0];
}


function getSimpleArrayValue(array, searchArg, boolReturnNullStringOnNotFound)
{
  var arr = getMultiArrayValue(array, searchArg, 1, true); 
  var textOut = arr == null ? "" : arr[1];
  if (boolReturnNullStringOnNotFound && (textOut == ""))
    textOut = "null";
  return textOut;
}


function countObjectProperties(obj)
{
   var incr = 0;
   for (var key in obj) incr++;
   return incr;
}


function getFileNameFromPath(path)
{
  var indx = path.lastIndexOf("/");
  if (indx == -1)
    indx = path.lastIndexOf("\\");
  var fileName = path.substr(indx + 1);
  return fileName;
}


function getFileExtension(fileName)
{
  var indx = fileName.lastIndexOf(".");
  var fileExt = fileName.substr(indx + 1);
  return fileExt;
}


function getElementAttributeValue(elem, name)
{
  name = name.toLowerCase();
  for (var incr = 0; incr <= elem.attributes.length; incr++)
  {
    var attrib = elem.attributes[incr];
    if (typeof(attrib) == 'undefined') continue;
    if (attrib.name.toLowerCase() == name)
    {  
      return attrib.value;  
    }
  }
  return "";
}


function checkPositiveInteger(field, msg, boolClearOnError) 
{
    var val = field.value;
    if (isNaN(val) || (!isNaN(val) && ((val.indexOf(".") > -1) || (val.indexOf("-") > -1))))
    {
        if ((typeof(msg) != "undefined") && (msg != ""))
          alert(msg);
        if ((typeof(boolClearOnError) != "undefined") && (boolClearOnError == true))
          field.value = "";
        return false;
    }
    return true;
}


function checkPositiveDecimal(field, decimalPlaces, msg, boolClearOnError) 
{
    var val = field.value;
    if (isNaN(val) || (!isNaN(val) && (((val.indexOf(".") > -1) && val.substr(val.indexOf(".")).length > (decimalPlaces + 1)) || (val.indexOf("-") > -1))))
    {
        if ((typeof(msg) != "undefined") && (msg != ""))
          alert(msg);
        if ((typeof(boolClearOnError) != "undefined") && (boolClearOnError == true))
          field.value = "";
        return false;
    }
    return true;
}


function gotoUrlOnConfirm(url, msg)
{
    if (confirm(msg))
      location.href = url;
}


function fetchNumericValue(field, fieldFromID, expr)
{
    var fieldFrom = document.getElementById(fieldFromID);
    if (fieldFrom == null) return;
    
    var val = fieldFrom.value;
    if ((val == "") || isNaN(val)) return;

    if (typeof(expr) != "undefined")
      val = eval("parseInt(val) " + expr);
    
    field.value = val;
}


function clearFieldOnValueEmpty(field, val, msg)
{
  if (val == null) return true;
  
  if (val == "")
  {
    if ((typeof(msg) != "undefined") && (msg != ""))
      alert(msg);
    field.value = "";
    return false;
  }
  return true;
}


function clearFieldOnError(field, msg, boolError)
{
  boolError = typeof(boolError) != "undefined" ? boolError : true;
  if (boolError == false) return true;
  
  if ((typeof(msg) != "undefined") && (msg != ""))
    alert(msg);
  field.value = "";
  return false;
}


function restoreFieldOnError(field, msg, boolRestorePrevValue, boolError)
{
  boolError = typeof(boolError) != "undefined" ? boolError : true;
  if (boolError == false)
  {
    if (field.getAttribute("orig_value") == null)
      field.setAttribute("orig_value", field.value); 
    field.setAttribute("prev_value", field.value);
    return true; 
  }
  
  if ((typeof(msg) != "undefined") && (msg != ""))
    alert(msg);
  
  if ((typeof(boolRestorePrevValue) != "undefined") && (boolRestorePrevValue == true))
    field.value = field.getAttribute("prev_value") == null ? "" : field.getAttribute("prev_value"); 
  else
    field.value = field.getAttribute("orig_value") == null ? "" : field.getAttribute("orig_value"); 
  return false;
}


function parseOutNumber(text, textOnEmpty)
{
    if (typeof(textOnEmpty) != "undefined")
      if ((text == null) || (text == ""))
      {
        return textOnEmpty;
      }
    var textOut = "";
    var chr = "";
    for (var incr = 0; incr <= text.length-1; incr++)
    {
      chr = text.substr(incr, 1);
      if (!isNaN(chr)) textOut += chr; 
    }
    return textOut;
}


function leftTrim(text)
{
  var textOut = text;
  var chr = "";
  var incr = -1;
  while (incr++ <= text.length - 1)
  {
    chr = text.substr(incr, 1);
    if (chr == " ") textOut = textOut.substr(1);
    else break;
  }
  return textOut;
}


function rightTrim(text)
{
  var textOut = text;
  var chr = "";
  var incr = text.length;
  while (incr-- >= 0)
  {
    chr = text.substr(incr, 1);
    if (chr == " ") textOut = textOut.substr(0, textOut.length - 1);
    else break;
  }
  return textOut;
}


function allTrim(text)
{
  return rightTrim(leftTrim(text));
}


//-----------------------//
//                       //
// ASYCN CALLS FUNCTIONS //
//                       //
//-----------------------//

function sendWebRequest(url, boolAsync)
{
  if ((typeof(url) == "undefined") || (url == "")) return "";
  
  var xmlhttp=false;
  var resp = "";
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  // JScript gives us Conditional compilation, we can cope with old IE versions.
  // and security blocked creation of the objects.
  try 
  {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) 
  {
   try 
   {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (E) 
   {
     xmlhttp = false;
   }
  }
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
  {
   try 
   {  
     xmlhttp = new XMLHttpRequest();
   } 
   catch (e) 
   {
     xmlhttp=false;
   }
  }

  if (!xmlhttp && window.createRequest) 
  {
   try 
   {
      xmlhttp = window.createRequest();
   } 
   catch (e) 
   {
     xmlhttp=false;
   }
  }
  
  boolAsync = typeof(boolAsync) != "undefined" ? boolAsync : false;
  
  xmlhttp.open("GET", url, boolAsync);
  xmlhttp.onreadystatechange=function()
  {
    if (xmlhttp.readyState == 4)
    {
      if (xmlhttp.responseText == "")
      {
        alert("Asynch call error1");
        return "";
      }
      resp = xmlhttp.responseText; 
    }
  }
  xmlhttp.send(null);
  return resp;
}


function setElementInnerHtmlFromAsyncWebRequest(url, elemID)
{
  if ((typeof(url) == "undefined") || (url == "")) return "";
  
  var xmlhttp=false;
  var resp = "";
  /*@cc_on @*/
  /*@if (@_jscript_version >= 5)
  // JScript gives us Conditional compilation, we can cope with old IE versions.
  // and security blocked creation of the objects.
  try 
  {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (e) 
  {
   try 
   {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   } catch (E) 
   {
     xmlhttp = false;
   }
  }
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
  {
   try 
   {  
     xmlhttp = new XMLHttpRequest();
   } 
   catch (e) 
   {
     xmlhttp=false;
   }
  }

  if (!xmlhttp && window.createRequest) 
  {
   try 
   {
      xmlhttp = window.createRequest();
   } 
   catch (e) 
   {
     xmlhttp=false;
   }
  }
  
      xmlhttp.open("GET", url, true);
  xmlhttp.onreadystatechange=function()
  {
    if (xmlhttp.readyState == 4)
    {
      if (xmlhttp.responseText == "")
      {
        alert("Asynch call error");
        return "";
      }
      resp = xmlhttp.responseText; 
      setElementInnerHtml(elemID, resp, false);
    }
  }
  xmlhttp.send(null);
}
