//  MacroMedia javascript functions for image rollovers
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function showHide(ITEM_ID)	{ 
  if (ITEM_ID) {
    if (window.document.getElementById(ITEM_ID).style.display == "block") {
      window.document.getElementById(ITEM_ID).style.display = "none"; 
    } else { 
      window.document.getElementById(ITEM_ID).style.display = "block"; 
    } 
  }		
  return false;
}

function showHideDiv(div_id) {
  var yScroll = 0;
  if(div_id) { 
    var thisDiv = window.document.getElementById(div_id);
    var dimmerDiv = window.document.getElementById('dimmer');
    // get the y scroll offset
    var yScroll = document.body.scrollTop;  // IE only, not sure what to do with others.
    // get the window size
    var windowHeight = document.body.clientHeight;
    var windowWidth = document.body.clientWidth;
    // calculate the horizon line
    var midLine = (windowHeight/2) + yScroll;
    // set the background color
    //var bgcolor = document.body.style.background.value;
    //alert(bgcolor);
    // thisDiv.style.backgroundColor = "#000000";
    // alert(yScroll);
    // calculate the top and left
    // var topEdge = thisDiv.offsetHeight/2;
    // var leftEdge = thisDiv.offsetWidth/2;
    // set the style 
    //var top = parseInt(midLine-(thisDiv.offsetHeight/2));
    //var left = windowWidth; // parseInt((windowWidth/2)-(thisDiv.offsetWidth/2));
    // display
    if (thisDiv.style.visibility == "visible") {
      thisDiv.style.display = "none";
      thisDiv.style.visibility = "hidden"; 
      dimmerDiv.style.visibility="hidden";
    } else {
      dimmerDiv.style.height = getDocHeight();
      dimmerDiv.style.width = document.body.clientWidth;
      dimmerDiv.style.visibility="visible";
      dimmerDiv.style.zindex=1;
      thisDiv.style.display = "block";
      thisDiv.style.visibility = "visible";
      var top = thisDiv.offsetHeight;
      var left = thisDiv.offsetWidth;
      // alert('top: '+top+', left: '+left);
      thisDiv.style.top=midLine-(thisDiv.offsetHeight/2) + 'px';
      thisDiv.style.left=(windowWidth/2)-(thisDiv.offsetWidth/2) + 'px';
      thisDiv.style.zindex=1000;       
    }     

  }
  return false;
}

function getDocHeight() {
    var D = document;
    return Math.max(
        Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
        Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
        Math.max(D.body.clientHeight, D.documentElement.clientHeight)
    );
}

function loadcurr() { 
  var currencypref = GetCookie('currency');
  if(!currencypref) currencypref = 0;
  setcurr(currencypref);
}

function selectcurr(mySelect) { 
  var currencypref = mySelect.options[mySelect.selectedIndex].value;
  setcurr(currencypref);
}

function setcurr(currNum) { 
  SetCookie ('currency', currNum, exp);
  var a = document.getElementsByTagName( 'span' );
  var count = 0;
  for( i=0; i<a.length; i++ ) {
    if( a[i].id && a[i].id=="rate" ) {
      var rateitem = a[i];
      changecurr(count, rateitem, currNum);
      count++;
    }
  }
    // make sure the select box changes.
  curSel = document.getElementById( 'currency' );
  curSel.options[currNum].selected = true;
}

function changecurr(objNum,rateObj,currNum) {
  // if it's digits only, add the curr symbol.
  var newCurr;
  var re = new RegExp(/^\d+$/);
  if(re.test(rates[objNum][currNum])) {
    newCurr = currencies[currNum]+" "+rates[objNum][currNum];
  } else { 
    newCurr = rates[objNum][currNum];
  }
  rateObj.innerHTML = newCurr;
}


function getCookieVal (offset) {  
  var endstr = document.cookie.indexOf (";", offset);  
  if (endstr == -1)    
  endstr = document.cookie.length;  
  return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {  
  var arg = name + "=";  
  var alen = arg.length;  
  var clen = document.cookie.length;  
  var i = 0;  
  while (i < clen) {    
  var j = i + alen;    
  if (document.cookie.substring(i, j) == arg)      
    return getCookieVal (j);    
    i = document.cookie.indexOf(" ", i) + 1;    
    if (i == 0) break;   
  }  
  return null;
}

function SetCookie (name, value, exp) {  
  // alert('Set Cookie '+value);
  var argv = SetCookie.arguments;  
  var argc = SetCookie.arguments.length;  
  var expires = (argc > 2) ? argv[2] : null;  
  var path = (argc > 3) ? argv[3] : null;  
  var domain = (argc > 4) ? argv[4] : null;  
  var secure = (argc > 5) ? argv[5] : false;  
  document.cookie = name + "=" + escape(value) + 
  ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
  ((path == null) ? "" : ("; path=" + path)) +  
  ((domain == null) ? "" : ("; domain=" + domain)) +    
  ((secure == true) ? "; secure" : "");
}

function GetXmlHttpObject() {
  var xmlHttp=null;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }
  catch (e) {
    // Internet Explorer
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttp;
}

function dologin()  {
  document.getElementById('login_result').innerHTML=""; 
  document.getElementById('login_result').innerHTML="Logging in...";
  xmlHttp=GetXmlHttpObject()    // instantiates the request
  if (xmlHttp==null) {
    alert ("Your browser does not support AJAX!");
    return;  // you're a freak
  } 
  var url="/cgi-bin/cms.cgi";  // where to go 
  var username = document.getElementById("username").value;
  var password = document.getElementById("password").value;
  var params = "a=login&username="+username+"&password="+password+"&sid="+Math.random();
  
  xmlHttp.onreadystatechange=checkLoginResponse;  // ?
  xmlHttp.open("POST",url,true); // go get it.
  xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-length", params.length);
  xmlHttp.setRequestHeader("Connection", "close");
  xmlHttp.send(params);  // send it.
  return false;
} 

function checkLoginResponse() { // get ready, then suck back the response if the state = 4
  if (xmlHttp.readyState==4) { 
    var responseCodes = xmlHttp.responseText.split(":"); // code, userID, text, session
    document.getElementById('login_result').innerHTML="<span class='errorHeader'>"+responseCodes[2]+"</span>";  // write the response.
    if(responseCodes[0] == 1) { 
      	var cookieVals = document.getElementById('username').value+"\&"+responseCodes[3]+"\&"+responseCodes[2];
      	SetCookie('userdata_sid', responseCodes[3]);		// name, session, id
      	var userdata_data_value = "a:2:{s:11:\"autologinid\"\;s:0:\"\"\;s:6:\"userid\"\;s:1:\""+responseCodes[1]+"\"\;}";
        var oneYearOut = new Date();
      	oneYearOut.setTime(oneYearOut.getTime() + 365*24*60*60*1000);
      	SetCookie('userdata_data', userdata_data_value, oneYearOut, '');
      	var new_location = document.getElementById('login_resource').value;
      	var new_qs = document.getElementById('qs').value;
      	if (new_qs) { new_qs = "?" + new_qs; }
      	var new_uri = new_location + new_qs;      	
      	document.location.href=new_uri;
    }

  }
} 

function dopostback(f,url,result_div_name,cookie_name,validate)  {
  if(cookie_name) {
    var cookie_value = GetCookie(cookie_name);
    var cookie_to_send = cookie_name + "=" + cookie_value;
    alert("setting cookie with the postback to " +cookie_to_send);
  }
  // document.getElementById('login_result').innerHTML="Logging in...";
  // put up the waiting sign.
  //var parentDiv = document.getElementById(f.parentNode.id);
  //parentDiv.className="dimmed";
  xmlHttp=GetXmlHttpObject()    // instantiates the request
  if (xmlHttp==null) {
    alert ("Your browser does not support AJAX!");
    return;  
  } 
  var result_div_obj = document.getElementById(result_div_name);
  var params = getFormValues(f,validate);
  if(params) params = params+"&sid="+Math.random();        
  url = url; //+'?'+params;
  actual_url = url+'?'+params;
  // alert(actual_url);
  xmlHttp.onreadystatechange=postbackResponse(result_div_obj);
  xmlHttp.open("POST",url,true); // go get it.
  xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Connection", "close");
  xmlHttp.setRequestHeader("Cookie","nonemptystring");  
  xmlHttp.setRequestHeader("Cookie",cookie_to_send);  
  xmlHttp.send(params);  // send it.
  
  showWait(result_div_obj);
  return false;
} 

function postbackResponse(result_div_obj) { // get ready, then suck back the response if the state = 4
  return function() {
    if (xmlHttp.readyState==4) {
      var resultNode = xmlHttp.responseXML.documentElement;     //getElementsByTagName("result");
      var statusDesc = "Remote site does not support this function";
            
      if(resultNode) {
        var nextFuncText = "";        
        statusCodeNode = resultNode.getElementsByTagName("status_code");
        statusCode = statusCodeNode[0].firstChild.nodeValue;
        statusDescNode = resultNode.getElementsByTagName("status_desc");
        statusDesc = statusDescNode[0].firstChild.nodeValue;
        nextFuncNode = resultNode.getElementsByTagName("next_function");
        if (nextFuncNode[0]) { nextFuncText = nextFuncNode[0].firstChild.nodeValue; }
       
      }
      
      showResult(result_div_obj,statusDesc);
      // result_div_obj.innerHTML = statusDesc; 
      if(nextFuncText) eval(nextFuncText);
    }
  }
} 

function showWait(thisDiv) { 
  var message_div = document.getElementById('cms_result_message');
  if(message_div) { message_div.parentNode.removeChild(message_div); }
  actionObject_orig_content = thisDiv.innerHTML;
  var content = document.getElementById('cms_waiting_symbol').innerHTML;
  thisDiv.innerHTML = content;
}
function showResult(thisDiv, txt) {
  var result_text_span = document.createElement("div");
  result_text_span.id="cms_result_message";
  result_text_span.innerHTML = txt;
  thisDiv.innerHTML = actionObject_orig_content;
  thisDiv.appendChild(result_text_span);
}

function urlCheck(href_field_object,href_result_field_name,pageID)  { 
  var href_result_object = document.getElementById(href_result_field_name);
  var href = href_field_object.value; 
  href_result_object.innerHTML="checking url...";

  xmlHttp=GetXmlHttpObject()    // instantiates the request
  if (xmlHttp==null) {
    alert ("Your browser does not support AJAX!");
    return;  // you're a freak
  } 
  var url="/cgi-bin/cms.cgi";  // where you want to go today
  var params = "a=url_check&href="+href+"&pageID="+pageID+"&sid="+Math.random();
 
  xmlHttp.onreadystatechange=checkUrlResponse(href_field_object,href_result_object);  
  xmlHttp.open("POST",url,true); // go get it.
  xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
  xmlHttp.setRequestHeader("Content-length", params.length);
  xmlHttp.setRequestHeader("Connection", "close");
  xmlHttp.send(params);  // send it.
  return false;
} 

function checkUrlResponse(href_field_object,href_result_object) { // get ready, then suck back the response if the state = 4
  return function() { 
    if (xmlHttp.readyState==4) { 
      var responseCodes = xmlHttp.responseText.split(":"); // code, userID, text, session
      href_result_object.innerHTML="<span class='errorHeader'>"+responseCodes[2]+"</span>";  // write the response.
      if(responseCodes[0] == 1) { // href is good
        href_result_object.innerHTML="<span class='goodHeader'>url ok</span>";
        href_field_object.style.color="#009900";
      } 
      else if (responseCodes[0] == 0) {
        href_result_object.innerHTML="<span class='errorHeader'>"+responseCodes[2]+"</span>";  // write the response.
        document.getElementById('href'); 
        href_field_object.style.color = "#FF0000";
      }
    }
  } 
}

// IF the body being returned is xml...  Don't know what I want to do here.  I'd have to have something custom on each page.  Not sure if I want to do this.

function stateChangedXML() { 
  if (xmlHttp.readyState==4)  {
    var xmlDoc=xmlHttp.responseXML.documentElement;
    document.getElementById("companyname").innerHTML=
    xmlDoc.getElementsByTagName("compname")[0].childNodes[0].nodeValue;
    document.getElementById("contactname").innerHTML=
    xmlDoc.getElementsByTagName("contname")[0].childNodes[0].nodeValue;
    document.getElementById("address").innerHTML=
    xmlDoc.getElementsByTagName("address")[0].childNodes[0].nodeValue;
    document.getElementById("city").innerHTML=
    xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue;
    document.getElementById("country").innerHTML=
    xmlDoc.getElementsByTagName("country")[0].childNodes[0].nodeValue;
  }
}

function getFormValues(fobj,valFunc) { 
  var str = ""; 
  var valueArr = null; 
  var val = ""; 
  var cmd = ""; 
  for(var i = 0;i < fobj.elements.length;i++)  { 
    switch(fobj.elements[i].type) { 
      case "text": 
        if(valFunc) {
          //use single quotes for argument so that the value of 
          //fobj.elements[i].value is treated as a string not a literal 
          cmd = valFunc + "(" + 'fobj.elements[i].value' + ")"; 
          val = eval(cmd) 
        } 
        str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&"; 
        break;
      case "checkbox":
        if(fobj.elements[i].checked) str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
        break; 
      case "hidden":
        str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
        break; 
      case "select-one": 
        str += fobj.elements[i].name + "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&"; 
        break; 
    } 
  } 
  str = str.substr(0,(str.length - 1)); 
  return str; 
}

function include(fileUrl) { 
  var script = document.createElement("script");
  var head = document.getElementsByTagName('head').item(0);
  script.src = fileUrl;
  head.appendChild(script);
}

include('/cms_scripts/custom_functions.js');