var useLanguageSwitcher = true;
var useWidthSwitcher = true;

var aUILanguages = [ ['/admin/en'], ['English'], [ doUILanguageSwitch ], [getCookie('UILanguage')] ];
var aUIWidths = [ ['760px','984px','1236px','100%'], ['800','1024','1280','100%'], [ doUIWidthSwitch ], [getCookie('UIWidth')] ];

function orderBy(frm, sortOrder){
  frm.sortOrder.value = sortOrder;
  frm.submit();
}


function sortBy(frm, sortField){
  frm.sortField.value = sortField;
  frm.submit();
}

function changePage(frm, pageNumber){
  frm.page.value = pageNumber;
  frm.submit();
}

function confirmAction(prompt, okURL, cancelURL) {
  var confirmResult = confirm(prompt);

  if(confirmResult){
    window.location = okURL;
  } 
  else{
    if(cancelURL)
      window.location = cancelURL;
  }
}

function confirmActionSubmit(prompt, theForm) {
  var confirmResult = confirm(prompt);

  if(confirmResult){
    theForm.submit();
  } 
}

function openWindow(url,winname,wd,ht,scroll) {
  if(winname=='')
    winname='newpopup';

  var oWin = window.open(url,winname,'toolbar=0,resizable=0,scrollbars='+ scroll +',left=185,top=125,width='+ wd +',height='+ ht );
  oWin.focus();
  return;
}

function checkAll(theForm, prefix) {
  for (i=0,n=theForm.elements.length;i<n;i++)
    if (theForm.elements[i].id.indexOf(prefix) !=-1)
      theForm.elements[i].checked = true;
}

function unCheckAll(theForm, prefix) {
  for (i=0,n=theForm.elements.length;i<n;i++)
    if (theForm.elements[i].id.indexOf(prefix) !=-1)
      theForm.elements[i].checked = false;
}


function isDateValid(dateStr) {

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var matchArray = dateStr.match(datePat); // is the format ok?

	if (matchArray == null) {
		//alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
		return false;
	}

	month = matchArray[1]; // p@rse date into variables
	day = matchArray[3];
	year = matchArray[5];

	if (month < 1 || month > 12) { // check month range
		//alert("Month must be between 1 and 12.");
		return false;
	}

	if (day < 1 || day > 31) {
		//alert("Day must be between 1 and 31.");
		return false;
	}

	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		//alert("Month "+month+" doesn`t have 31 days!")
		return false;
	}

	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day > 29 || (day==29 && !isleap)) {
		//alert("February " + year + " doesn`t have " + day + " days!");
		return false;
		}
	}

	return true;
}

function isNum(vInt) {
  if (isNaN(vInt))
    return false;
  return true;
}

function isInt(sInt) {
  if (isNum(sInt) && sInt.indexOf('.') == -1)
    return true;
  return false;
}

function isFloat(sFloat) {
  if (isNum(sFloat))
    return true;
  return false;
}

function setUIWidth(sId, sValue){
  var obj = document.getElementById(sId)
  if (obj && obj.style && typeof(sValue)=='string') {
    obj.style.width = sValue;
    setCookie('UIWidth', sValue);
  }
}

function setUILanguage(sId, sValue){
  var obj = document.getElementById(sId)
  if (obj && obj.style && typeof(sValue)=='string') {

    //REMOVE OR COMMENT THE LINE BELOW BEFORE USE
    alert('To switch to location: ' + sValue  + '\n remove this alert from the setUILanguage function');return false;

    location.href='/admin/' + sValue;
    setCookie('UILanguage', sValue);
  }
}

function getCookie(sName){
  var aCookie = document.cookie.split("; ");
  for(var i=0; i < aCookie.length; i++){
    var aCrumb = aCookie[i].split("=");
    
    if(sName == aCrumb[0] && (aCrumb[1] != null)) 
      return unescape(aCrumb[1]);
  }
  return null;
}

function buildCookie(sName, sValue){
  var cookieValue = getCookie(sName);
  if(cookieValue == null)
    cookieValue = '';
  cookieValue = cookieValue + "/" + sValue;
  return cookieValue;
}

function unbuildCookie(sName, sValue){
  var cookieValue = getCookie(sName);
  var cookieLength = typeof(sValue.length) != 'undefined' ? sValue.length : 0;

  if(typeof(cookieValue) != 'undefined'){    
    var re = new RegExp('/' + sValue, "gi");
    return cookieValue.replace(re, '');
  }
  return cookieValue;
}

function setCookie(sName, sValue){
  date = new Date();
  document.cookie = sName + "=" + escape(sValue) + "; path=/;"
}

function doUIWidthSwitch() { 
  setUIWidth('rndContainer', aUIWidths[0][this.iCount]);
}

function doUILanguageSwitch() { 
  //setUILanguage('rndContainer', aUILanguages[0][this.iCount]);
}

function uiSwitcher(aVals) {
  var separator = '';
  var container = document.createElement('div');

  container.className = 'rndSwitcherContainer';

  for (i=0; i<aVals[0].length; i++) {
    var el = document.createElement('span');
    el.id = 'rndSwitcher-' + aVals[0][i];
    el.className = el.id=='rndSwitcher-'+aVals[3][0] ? 'rndSwitcherElementSelected' : 'rndSwitcherElement';
    
    el.iCount = i;
    el.onclick = function() {
      aVals[2][0].call(this);

      if (document.getElementById('rndSwitcher-'+aVals[3][0]))
        document.getElementById('rndSwitcher-'+aVals[3][0]).className = 'rndSwitcherElement'; 
      
      this.className = 'rndSwitcherElementSelected'; 
      aVals[3][0] = aVals[0][this.iCount];
    };
    el.appendChild(document.createTextNode(aVals[1][i]))
    
    container.appendChild(document.createTextNode(separator));
    container.appendChild(el);

    separator = ' | ';
  }
    
  document.getElementById('rndWidgetContainer').appendChild(container);

  return true;
}

function setTrColour(obj) {

  var oCheckbox = obj;
  if (obj.tagName.toLowerCase() == 'input') {
    
    if (obj.parentNode)
      while (obj.tagName.toLowerCase() != 'body' && obj.tagName.toLowerCase() != 'tr')
        obj = obj.parentNode;

    if (obj.className == 'row0' && oCheckbox.checked)
      obj.className = 'row0Sel';
    else if(obj.className == 'row0Sel')
      obj.className = 'row0';
    else if(obj.className == 'row1' && oCheckbox.checked)
      obj.className = 'row1Sel';
    else if(obj.className == 'row1Sel')
      obj.className = 'row1';
  }
}

function toggleMenu(obj) {
  for(i=0; i < obj.childNodes.length; i++) {
    
    if (obj.childNodes[i].className == 'rndNavContentClosed'){
      obj.childNodes[i].className = 'rndNavContentOpen';
      setCookie('menu', buildCookie('menu', obj.id));
    }
    else if (obj.childNodes[i].className == 'rndNavContentOpen') {
      obj.childNodes[i].className = 'rndNavContentClosed';
      setCookie('menu', unbuildCookie('menu', obj.id));
    }
  }
  // Hack to get footer to reposition in mozilla 
  if (document.getElementById('rndFooterContainer')) {
    document.getElementById('rndFooterContainer').style.display = 'none';
    document.getElementById('rndFooterContainer').style.display = 'block';
  }
  // Hack to stop IE shifting by 3px to the left when the menus contract
  if (document.getElementById('rndSideBarOnLeft') && document.getElementById('rndCentralArea'))
    document.getElementById('rndSideBarOnLeft').style.height = document.getElementById('rndCentralArea').offsetHeight;
}

function initMenu() {
  var aCrumb = getCookie('menu');

  if(aCrumb != null){
    var aNanoCrumb = aCrumb.split("/");
    for(var i=0; i < aNanoCrumb.length; i++){
      var aMenuId = aNanoCrumb[i];
      var obj = document.getElementById(aMenuId);
      
      if(obj) {
        for(j=0; j < obj.childNodes.length; j++) {
          if (obj.childNodes[j].className == 'rndNavContentClosed')
            obj.childNodes[j].className = 'rndNavContentOpen';
        }
      }
    }
  }
  // Hack to get footer to reposition in mozilla 
  if (document.getElementById('rndFooterContainer')) {
    document.getElementById('rndFooterContainer').style.display = 'none';
    document.getElementById('rndFooterContainer').style.display = 'block';
  }
  // Hack to stop IE shifting by 3px to the left when the menus contract
  if (document.getElementById('rndSideBarOnLeft') && document.getElementById('rndCentralArea'))
    document.getElementById('rndSideBarOnLeft').style.height = document.getElementById('rndCentralArea').offsetHeight;
 
  return null;
}

function toggleCheckbox(table, colIndex, checked) {
  if (table == null || typeof(table) == 'undefined')
    return;

  for (var i=0; i < table.rows.length; i++) {
    var tr = table.rows[i];
    if (colIndex < 0 || colIndex >= tr.cells.length)
      continue;
    
    var td = tr.cells[colIndex];
    for (var j=0; j<td.childNodes.length; j++) {
      var elem = td.childNodes[j];
      if (elem.tagName == "INPUT" && elem.type == "checkbox") {
        elem.checked = checked;
      }
    }
  }
}

function getParentElementWithTagName(element, tagName) {
  while (element.parentNode != null) {
    element = element.parentNode;

    if (element.tagName == tagName) {
      return element;
    }
  }
  return null;
}

//window.onload = function(){ 
rndPageLoaded = function(){ 
  //if (useWidthSwitcher)
  //  uiSwitcher(aUIWidths);
  //if (useLanguageSwitcher)
  //  uiSwitcher(aUILanguages);
  initMenu();
};

//window.onresize = function(){
rndPageResized = function(){ 
   //var rndcontainer = document.getElementById("rndContainer");
   //rndcontainer.style.display = "none";
   //rndcontainer.style.display = "block"; 
};

    function toggleClientMenu(senderId, id)
    {
        var sender = document.getElementById(senderId);
        var obj = document.getElementById(id);
        if(obj.style.display == '')
        {
            obj.style.display = 'none';
            sender.className = "arrowRight"; 
        }
        else
        {
            obj.style.display = '';
            sender.className = "arrow"; 
        }
    }
