function ltrim(sValue) {
   while(1) {
      if(sValue.substring(0, 1) != " ")
         break;
      sValue = sValue.substring(1, sValue.length);
   }
   return sValue;
}

function rtrim(sValue) {
   while(1) {
      if(sValue.substring(sValue.length - 1, sValue.length) != " ")
         break;
      sValue = sValue.substring(0, sValue.length - 1);
   }
   return sValue;
}

function trim(sValue) {
  if (typeof sValue != "string") { return sValue; }
  var sTemp = ltrim(sValue);
  return rtrim(sTemp);
}

function validateEmail(sEmail)
{
  var exclude   =/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
  var check     =/@[\w\-]+\./;
  var checkend  =/\.[a-zA-Z]{2,6}$/;
  sEmail = trim(sEmail.value);

  if(sEmail == "") {
    return false;
  }

  if(((sEmail.search(exclude) != -1) ||
    (sEmail.search(check)) == -1) ||
    (sEmail.search(checkend) == -1)) {
      return false;
  }
  return true;
}

function showHideLeftMenu(){
  var letfMenuTD = document.getElementById("left_menu_td");

  if( letfMenuTD.style.display != null) {
    if( letfMenuTD.style.display == '' || letfMenuTD.style.display == 'block' ){
      letfMenuTD.style.display = 'none';
      setCookie("ubix_left_menu_hide", "true");
    }
    else {
      letfMenuTD.style.display = 'block';
      deleteCookie("ubix_left_menu_hide");
    }

  }
}

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

/* Validate to input only numbers */
function onlyNumbers(object){
  object.value = object.value.replace(/[\D\.\,]+/gi,"");
}

/* Validate to input only float numbers */
function onlyFloatNumbers(object){
  reg = /[^0-9.,]/g;
  object.value =  object.value.replace(reg,"");
}
var ajaxSubQst;

function loadSubQuestions(_qstId, _qqeId){

  ajaxSubQst = new sack();
  var _id = "SUB_QST_"+ _qstId;
  var contentDiv = document.getElementById( _id );

  if(contentDiv != null) {
    contentDiv.innerHTML = " ";
  }

  ajaxSubQst.setVar("qstId", _qstId);
  ajaxSubQst.setVar("qqeId", _qqeId);
  document.body.style.cursor = "wait";
  ajaxSubQst.requestFile = "loadSubQuestions.do";

  ajaxSubQst.onCompletion = function () {
    var _id = "SUB_QST_"+ _qstId;
    var contentDiv = document.getElementById( _id );
    document.body.style.cursor = "default";
    if(_qqeId == '-1') {
      contentDiv.innerHTML = "";
    }
    else {
      contentDiv.innerHTML = ajaxSubQst.response;
    }
  }
  ajaxSubQst.runAJAX();
}

function isNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;


   for (i = 0; i < sText.length && IsNumber == true; i++)
      {
      Char = sText.charAt(i);
      if (ValidChars.indexOf(Char) == -1)
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}


var ajaxSetTestData;
function setTestData(_chk, qsyId, curId, rpsId) {
  showProgressaBar("Processing");
  document.body.style.cursor = "wait";

  ajaxSetTestData = new sack();

  ajaxSetTestData.setVar("qsyId", qsyId);
  ajaxSetTestData.setVar("rpsId", rpsId);
  ajaxSetTestData.setVar("curId", curId);
  ajaxSetTestData.setVar("checked", _chk.checked);

  ajaxSetTestData.requestFile = "setTestData.do";

  ajaxSetTestData.onCompletion = function () {
    hideProgressBar();
    document.body.style.cursor = "default";
  }

  ajaxSetTestData.runAJAX();
}

var ajaxSalesPersonst;
function loadSalesPersonsForComunity(_qqeId, _divId){

  ajaxSalesPersonst = new sack();
  var contentDiv = document.getElementById( _divId );

  if(contentDiv != null) {
    contentDiv.innerHTML = " ";
  }

  ajaxSalesPersonst.setVar("qqeId", _qqeId);
  document.body.style.cursor = "wait";
  ajaxSalesPersonst.requestFile = "loadSalesPersonsForComunity.do";

  ajaxSalesPersonst.onCompletion = function () {
    var contentDiv = document.getElementById( _divId );
    document.body.style.cursor = "default";
    if(_qqeId == '-1') {
      contentDiv.innerHTML = "";
    }
    else {
      contentDiv.innerHTML = ajaxSalesPersonst.response;
    }
  }
  ajaxSalesPersonst.runAJAX();
}

function MM_openBrWindow(theURL,winName) {
    var w = 800;
    var h = 600 ;
    if (window.screen) { 
        w = window.screen.availWidth; 
        h = window.screen.availHeight; 
    } 
  window.open(theURL,winName,'status=1,resizable=1,width=' + (w-80) + ',height=' + (h-80) + ',top=10,left=10');
}

var ajaxCountryStates;
function loadCountryRegions(_countryFld, _fieldName){
    
  ajaxCountryStates = new sack();
  var contentDiv = document.getElementById('states_div');
  
  if(contentDiv != null) {
    contentDiv.innerHTML = " ";
  }
  
  var countryId = _countryFld.value;
  
  ajaxCountryStates.setVar("countryId", countryId);
  if( _fieldName ) {
    ajaxCountryStates.setVar("fieldName", _fieldName);
  }
  document.body.style.cursor = "wait";
  ajaxCountryStates.requestFile = "loadCountryRegions.do";

  ajaxCountryStates.onCompletion = function () {
    var _contentDiv = document.getElementById('states_div');
    document.body.style.cursor = "default";
    _contentDiv.innerHTML = ajaxCountryStates.response;
  }
  ajaxCountryStates.runAJAX();
}


var ajaxSalesPersonsForSalesUnit;
function loadSalesPersonsForSalesUnit(_companyId, _salesUnitFld, _fieldName){
    
  ajaxSalesPersonsForSalesUnit = new sack();
  var contentDiv = document.getElementById('sales_person_div');
  
  if(contentDiv != null) {
    contentDiv.innerHTML = " ";
  }
  
  var salesUnitId = _salesUnitFld.value;
  
  ajaxSalesPersonsForSalesUnit.setVar("salesUnitId", salesUnitId);
  ajaxSalesPersonsForSalesUnit.setVar("companyId", _companyId);
  
  if( _fieldName ) {
    ajaxSalesPersonsForSalesUnit.setVar("fieldName", _fieldName);
  }
  document.body.style.cursor = "wait";
  ajaxSalesPersonsForSalesUnit.requestFile = "loadSalesPersonsForSalesUnit.do";

  ajaxSalesPersonsForSalesUnit.onCompletion = function () {
    var _contentDiv = document.getElementById('sales_person_div');
    document.body.style.cursor = "default";
    _contentDiv.innerHTML = ajaxSalesPersonsForSalesUnit.response;
  }
  
  ajaxSalesPersonsForSalesUnit.runAJAX();
}
