///////////////////////////////////////
// Main.js - Globally used functions
///////////////////////////////////////

/** 
 * Switchy McLayout - Dynamically
 * Code borrowed from http://www.alistapart.com/articles/switchymclayout
 * Modified to set a cookie instead of change the page class
 */
function setScreenClass(){
	var pclass = getCookie('screenClass');
	var url = window.location.href;
	
  // If mobi hasn't been set and no cookie has been set, set the screen class
  if(getParameter('mobi') == 'true') {
    setCookie("screenClass", 'screen_ultralow', 1000);
  } else if(getCookie('screenClass') == ''){
    var fmt = document.documentElement.clientWidth;
    var cls = (fmt<=240)?'pda_ver':(fmt>240&&fmt<=320)?'pda_hor':(fmt>320&&fmt<=640)?'screen_ultralow':(fmt>640&&fmt<=800)?'screen_low':(fmt>800&&fmt<=1024)?'screen_med':(fmt>1024&&fmt<=1280)?'screen_high':'screen_wide';
    setCookie("screenClass", cls, 1000);
    if(pclass && pclass != cls) {
			window.location.href = url; // only reload if the previous screenClass value is different
    }		
  }
}   
function getCookie(c_name) {
  if (document.cookie.length>0) {
    c_start=document.cookie.indexOf(c_name + "=")
    if (c_start!=-1) { 
      c_start=c_start + c_name.length+1 
      c_end=document.cookie.indexOf(";",c_start)
      if (c_end==-1) c_end=document.cookie.length
      return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
  return "";
}
function setCookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value) +  ";path=/" + ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
} 
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
function getParameter(parameterName ) {
  var queryString = window.location.toString();
  // Add "=" to the parameter name (i.e. parameterName=value)
  parameterName += "=";
  if ( queryString.length > 0 ) {
  
    // Find the beginning of the string
    begin = queryString.indexOf(parameterName);
    // If the parameter name is not found, skip it, otherwise return the value
    if ( begin != -1 ) {
      // Add the length (integer) to the beginning
      begin += parameterName.length;
      // Multiple parameters are separated by the "&" sign
      end = queryString.indexOf ( "&" , begin );
      if ( end == -1 ) {
        end = queryString.length
      }
      // Return the string
      return unescape ( queryString.substring ( begin, end ) );
    }
    // Return "null" if no parameter has been found
    return "null";
  }
} 
addLoadEvent(setScreenClass);

///////////////////////////////////////
// Form functions
///////////////////////////////////////
function cleanNumericField(field) {
  var regex = /[^0-9\.]/
	field.value = field.value.replace(regex, '');  
}
  
 var tabcount = 1;	
 function showTab(tabid,a) {
  // Does tabidreally exist?
  if(document.getElementById(tabid)) {
    //  alert('we have a tab');
    // Hide other tabs
    for(i=0;i<=tabcount;i++) {      
      if(document.getElementById('tab'+i)) {
        document.getElementById('tab'+i).style.display='none';
      }
    }
    // Set all tabs as inactive
    nodes = document.getElementsByTagName("A");
    for (i=0; i<nodes.length; i++) {
      node = nodes[i];
      if (node.parentNode.className=="tabsLi") {
        node.className="";
      }
    }
    // Show the selected tab
    document.getElementById(tabid).style.display='block';
    // Set tab to active
    a.className += "active";
  }
 }	