var js = 1.0;
Version = parseInt(navigator.appVersion);
if (navigator.appName == "Netscape")
 js = ((Version >= 4) ? 1.2 : ( (Version == 3) ? 1.1 : 1.0 ));
else
 if (navigator.appVersion.indexOf('MSIE') != -1)
  js = ((Version >= 4) ? 1.1 : 1.0);

var isIE = 0;

function getWidth() {
  if(window.innerWidth) {
    return window.innerWidth;
  } else {
    return document.body.clientWidth;
  }
}

// EventListener
function addListen(obj,listen,func) {
//  if (typeof func != 'function') {
//    alert('Invalid addListen param: ' + typeof func);
//  }
  if (obj == null) {
    alert('Invalid addListen obj');
  }

  if (obj.attachEvent) {
    var name = "on" + listen;
    obj.attachEvent(name,func);
  } else {
    obj.addEventListener(listen,func,false);
  }
}

// OnLoad
function addLoadEvent(func) {
  if (typeof func != 'function') {
    alert('Invalid addLoadEvent param');
  }

  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    };
  }
}

function getXMLHTTP(){
  var A=null;
  try{
    A=new ActiveXObject("Msxml2.XMLHTTP")
  }catch(e){
    try{
      A=new ActiveXObject("Microsoft.XMLHTTP")
    } catch(oc){
      A=null
    }
  }
  ieIE = 0;
  if(!A && typeof XMLHttpRequest != "undefined") {
    A=new XMLHttpRequest()
  } else {
    isIE = 1;
  }
  return A
}

// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix && isIE) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            if(result.firstChild) {
              return result.firstChild.nodeValue;
            } else {
              return("");
            }
        }
    } else {
        return "n/a";
    }
}

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_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 textCounter(field, countfield, maxlimit) {
  if (field.value.length > maxlimit) {
    field.value = field.value.substring(0, maxlimit);
  } else {
    countfield.value = maxlimit - field.value.length;
  }
}

function AJAX_Request(url,callbackFunction,reqType,reqParams) {
  var Req = getXMLHTTP();
  if (Req) {
    // need to assign, for use in the closure
    var self = this;
    Req.onreadystatechange =
    function()
    { // ProcessReqChange
      // only if req shows "loaded"
      if (Req.readyState == 4) {
        // only if "OK"
        if (Req.status == 200) {
          // This is where work is done
          callbackFunction(Req);
        } else {
          alert("There was a problem retrieving the HTTP Response:\n" + "URL: " + url + "\n" + Req.statusText + Req.responseText);
        }
      }
    }; // ProcessReqChange;
    if (reqType == "GET") {
      Req.open("GET", url, true);
      Req.send(null);
    } else if (reqType == "POST" ) {
      Req.open("POST", url, true);
      Req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      Req.setRequestHeader("Content-length", reqParams.length);
      Req.setRequestHeader("Connection", "close");
      Req.send(reqParams);
    }
  } // Req exists
}

function getHTMLSnippet(url,callbackFunction) {
  AJAX_Request(url,callbackFunction,"GET");
}

function makeHTTPPostRequest(url,callbackFunction,reqParams) {
  AJAX_Request(url,callbackFunction,"POST",reqParams);
}

function loadAJAXscripts() {
  // Loop through all script blocks currently in the DOM
  //   If it's ID is AJAX_* and does not end in _Loaded
  //   Eval() it
  // This allows <script> blocks in HTML files loaded via
  //   AJAX to be used
  var scripts = document.getElementsByTagName("script");
  for(var i = 0; i <= scripts.length; i++) {
    var script = scripts[i];
    var scriptID = '';
    if ((script) && (script.id)) {
      var scriptID = script.id;
      var scriptSRC = script.innerHTML;
      if (scriptID.match(/^AJAX_/)) {
	// Yes, this is a script that we manage
	if (!(scriptID.match(/_Loaded$/))) {
          // We haven't loaded this one yet
          scriptID += "_Loaded";
          eval(scriptSRC);
        }
      }
    }
  }
}

function checkLoggedInResp(text) {
  // Test to see if we're still logged in
  var pattern = /^<html>/i; // This works because we either return XML, or HTML Snippets - never complete pages
  if (text.match(pattern)) {
//    alert('No longer logged in');
    window.location="http://www.callzach.com/account/";
    return(false);
  }
  return(true);
}

function el(A) {
  if(document.getElementById) {
    return document.getElementById(A)
  } else {
    if(window[A]) {
      return window[A]
    }
  }
  return null
}

