var BrowserDetect = {
  init: function () {
    this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
    this.version = this.searchVersion(navigator.userAgent)
      || this.searchVersion(navigator.appVersion)
      || "an unknown version";
    this.OS = this.searchString(this.dataOS) || "an unknown OS";
  },
  searchString: function (data) {
    for (var i=0;i<data.length;i++)  {
      var dataString = data[i].string;
      var dataProp = data[i].prop;
      this.versionSearchString = data[i].versionSearch || data[i].identity;
      if (dataString) {
        if (dataString.indexOf(data[i].subString) != -1)
          return data[i].identity;
      }
      else if (dataProp)
        return data[i].identity;
    }
  },
  searchVersion: function (dataString) {
    var index = dataString.indexOf(this.versionSearchString);
    if (index == -1) return;
    return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
  },
  dataBrowser: [
    {
      string: navigator.userAgent,
      subString: "Chrome",
      identity: "Chrome"
    },
    {   string: navigator.userAgent,
      subString: "OmniWeb",
      versionSearch: "OmniWeb/",
      identity: "OmniWeb"
    },
    {
      string: navigator.vendor,
      subString: "Apple",
      identity: "Safari",
      versionSearch: "Version"
    },
    {
      prop: window.opera,
      identity: "Opera"
    },
    {
      string: navigator.vendor,
      subString: "iCab",
      identity: "iCab"
    },
    {
      string: navigator.vendor,
      subString: "KDE",
      identity: "Konqueror"
    },
    {
      string: navigator.userAgent,
      subString: "Firefox",
      identity: "Firefox"
    },
    {
      string: navigator.vendor,
      subString: "Camino",
      identity: "Camino"
    },
    {    // for newer Netscapes (6+)
      string: navigator.userAgent,
      subString: "Netscape",
      identity: "Netscape"
    },
    {
      string: navigator.userAgent,
      subString: "MSIE",
      identity: "Explorer",
      versionSearch: "MSIE"
    },
    {
      string: navigator.userAgent,
      subString: "Gecko",
      identity: "Mozilla",
      versionSearch: "rv"
    },
    {     // for older Netscapes (4-)
      string: navigator.userAgent,
      subString: "Mozilla",
      identity: "Netscape",
      versionSearch: "Mozilla"
    }
  ],
  dataOS : [
    {
      string: navigator.platform,
      subString: "Win",
      identity: "Windows"
    },
    {
      string: navigator.platform,
      subString: "Mac",
      identity: "Mac"
    },
    {
         string: navigator.userAgent,
         subString: "iPhone",
         identity: "iPhone/iPod"
      },
    {
      string: navigator.platform,
      subString: "Linux",
      identity: "Linux"
    }
  ]

};

BrowserDetect.init();

function $(id){
  return document.getElementById(id);
  //return element
}

function $$(id){
  return document.getElementById(id).style;
  //return element style
}

function hover(element) {
  element.style.cursor = "pointer";
  element.style.color = "#EE4523";
}

function hover2(element) {
  element.style.cursor = "pointer";
  element.style.color = "#EE4523";
}

function normal(element) {
  element.style.cursor = "auto";
  element.style.color = "#6699cc";
}

function normal2(element) {
  element.style.cursor = "auto";
  element.style.color = "#111";
}

function hoverCursor(element) {
  element.style.cursor = "pointer";
}

function normalCursor(element) {
  element.style.cursor = "auto";
}

function hoverButton(element,height) {
  element.style.cursor = "pointer";
  element.style.backgroundPosition = ("0px -" + height + "px");
}

function normalButton(element) {
  element.style.cursor = "auto";
  element.style.backgroundPosition = "0px 0px";
}

/*
function forceFullLength(minHeight) {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) { //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) { //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) { //IE 4 compatible
    myHeight = document.body.clientHeight;
  }

  //alert(myHeight);
  if (myHeight > minHeight) $$('main_content').height = (myHeight - 146) + 'px';
}
*/

function getWindowHeight() {
  var myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function getWindowWidth() {
  var myWidth = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
  }
  return myWidth;
}
function createXMLHttpRequestObject() {
  // xmlHttp will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // try to instantiate the native XMLHttpRequest object
  try
  {
    // create an XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
    }
    catch(e) { }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

function loadExternalData(source_url,target_id,callbackfunction){
  var httpobject = createXMLHttpRequestObject();
  var url=source_url;
   var params = "";

  httpobject.open("POST", url , true);
  httpobject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  httpobject.setRequestHeader("Content-length", params.length);
  httpobject.setRequestHeader("Connection", "close");

  httpobject.onreadystatechange = function() {//Call a function when the state changes.
    if((httpobject.readyState == 4 && httpobject.status == 200)) {
      $(target_id).innerHTML=httpobject.responseText;
      callbackfunction();
    }
  }

  httpobject.send(params);
  
}

function submitFormAJAX(form_id,source_url,target_id,callbackfunction){
  //$(target_id).innerHTML = "<img src=\"../img/loader21.gif\" border=\"0\"> saving data...";
  
  var httpobject = createXMLHttpRequestObject();
  var url=source_url;
   var params = "form=main_form&";

  var myForm = $(form_id);
    params += getParameters(myForm);
  httpobject.open("POST", url , true);
  httpobject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  httpobject.setRequestHeader("Content-length", params.length);
  httpobject.setRequestHeader("Connection", "close");
  httpobject.send(params);

  httpobject.onreadystatechange = function() {//Call a function when the state changes.
    if((httpobject.readyState == 4 && httpobject.status == 200) || navigator.userAgent.indexOf('Safari') > 0) {
      $(target_id).innerHTML=httpobject.responseText;
      callbackfunction();
    }
  }

}

function getParameters(obj) {
   var getstr = "";
   for (var i=0; i<obj.childNodes.length; i++) {
    //alert(obj.childNodes[i].tagName);
      if (obj.childNodes[i].tagName == "TEXTAREA") {
         var sel = obj.childNodes[i];
         getstr += sel.name + "=" + obj.childNodes[i].value + "&";
      }
      if (obj.childNodes[i].tagName == "INPUT") {
         if (obj.childNodes[i].type == "text" || obj.childNodes[i].type == "hidden" || obj.childNodes[i].type == "password") {
            getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
         }
         if (obj.childNodes[i].type == "checkbox") {
            if (obj.childNodes[i].checked) {
               getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
            } else {
               getstr += obj.childNodes[i].name + "=&";
            }
         }
         if (obj.childNodes[i].type == "radio") {
            if (obj.childNodes[i].checked) {
               getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
            }
         }
      }   
      if (obj.childNodes[i].tagName == "SELECT") {
         var sel = obj.childNodes[i];

     for(j = 0; j < sel.options.length; j++) { 
      if(sel.options[j].selected) { 
            getstr += sel.name + "=" + sel.options[j].value + "&";
      } 
    }
    
    }
      if (obj.childNodes[i].tagName == "DIV") {
         getstr += getParameters(obj.childNodes[i]);
      }
      if (obj.childNodes[i].tagName == "UL") {
         getstr += getParameters(obj.childNodes[i]);
      }
      if (obj.childNodes[i].tagName == "LI") {
         getstr += getParameters(obj.childNodes[i]);
      }
      if (obj.childNodes[i].tagName == "P") {
         getstr += getParameters(obj.childNodes[i]);
      }
      
   }

   return getstr;
}


function getElementArrayById(baseIdentifier) {
    var allWantedElements = [];
    var idMod = 0;
    while($(baseIdentifier + '_' + (idMod + 1))) { // will stop when it can't find any more
        allWantedElements.push($(baseIdentifier + '_' + (idMod + 1)));
    idMod++;
    }
    return allWantedElements;
}

function getBaseName(raw_name) {
  u_pos = raw_name.lastIndexOf('_');
  base_name = raw_name.substring(0,u_pos);
  return base_name;
}

function changecss(theclass,element,value) {
  var cssRules;
  if (document.all) {
    cssRules = 'rules';
  } else if (document.getElementById) {
    cssRules = 'cssRules';
  }

  for (var S = 0; S < document.styleSheets.length; S++) {
    for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
      if (document.styleSheets[S][cssRules][R].selectorText == theclass) {
        document.styleSheets[S][cssRules][R].style[element] = value;
      }
    }
  }
}

function goToURL(url) {
  self.location = url;
}

function goToURLNewWin(url) {
  var new_win = window.open (url,"newwin","width=1100,height=800,status=1,toolbar=1,scrollbars=1");
}


function getContentFromIframe(iFrameID) {
  var content = "";
  var myIFrame = $(iFrameID);  
  content = myIFrame.contentWindow.document.body.innerHTML;  
  return content;
}


function trim(stringToTrim) {
  return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
  return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
  return stringToTrim.replace(/\s+$/,"");
}

function checkEmail(inputvalue){
    var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    if(pattern.test(inputvalue)){         
    return true;   
    }else{   
    return false; 
    }
}

function checkZip(inputvalue){
    var zipCodePattern = /^\d{5}$|^\d{5}-\d{4}$/;
    return zipCodePattern.test(inputvalue);
}

function checkPhone(inputvalue){
    var phonePattern = /^\d{10}$/;;
    return phonePattern.test(inputvalue);
}

function getKeyNum(event) {
  var keynum;
  if(window.event) { // IE
    keynum = event.keyCode;
  } else if(event.which) { // Netscape/Firefox/Opera
    keynum = event.which;
  }
  return keynum;
}

function checkEnter(event,submit_function){ 
  var keynum = getKeyNum(event);
  if(keynum == 13){ //if generated character code is equal to ascii 13 (if enter key)
    submit_function(); //submit the form
  }
}

function getTextAreaCursorPosition(textarea_id) {
  return $(textarea_id).selectionStart;
}

function checkFormSubmitEnter(event, form_id) {
  checkEnter(event, function(){$(form_id).submit();})
}

function verticallyCenterElement(element_id, element_height) {
  var window_height = getWindowHeight();
  var element_offset = ((window_height / 2) - (element_height/2));
  if (element_offset < 0) element_offset = 5; //default
  $$(element_id).top = (parseInt(element_offset) + "px");
}

function getElementWidth(element_id) {
  return YAHOO.util.Dom.getRegion(element_id).width;
}

function getElementHeight(element_id) {
  return YAHOO.util.Dom.getRegion(element_id).height;
}

YAHOO.util.Event.onDOMReady(init);

