//  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 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 + "=" + 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="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/escorts.cgi";  // where you want to go today
//    url=url+"?a=login";      // what you want to send
//    url=url+"&sid="+Math.random();  // cache buster
  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");

//    alert("posting to "+url+" with "+params);
  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) { 
	// drop the cookie
	var cookieVals = document.getElementById('username').value+"\&"+responseCodes[3]+"\&"+responseCodes[2];
	SetCookie('userData', cookieVals);		// name, session, id
	// alert('set cookie');
	// redirect to destination
	document.location.href=document.getElementById('login_resource').value;
	// alert('redirected');
    }

  }
} 

// 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;
  }
}


// fucntion to show or hide a modal window div and dim the rest of the page
function animateDiv(div_id,tx,ty) {
  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;
    // 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";
      thisDiv.style.zindex = 1000;
      // get mouse location to start the animation
      pos = getMousePos();
      start_x = pos[0];
      start_y = pos[1];
      // start_y = thisDiv.offsetHeight;
      // start_x = thisDiv.offsetWidth;
      
      var tgtHeight = ty; // thisDiv.offsetHeight;
      var tgtWidth = tx; // thisDiv.offsetWidth;
      end_y = midLine-(tgtHeight/2); // + 'px';
      end_x = (windowWidth/2)-(tgtWidth/2); // + 'px';
      thisDiv.style.top = start_y + 'px';
      thisDiv.style.left = start_x + 'px';
      thisDiv.style.height = 0;
      thisDiv.style.width = 0;
      
      
      animateObj = new Animator({
        transition: Animator.makeEaseIn(2),
        duration: 500
      });
      //  alert(tgtHeight+', '+tgtWidth+', '+start_y+', '+end_y+', '+start_x+', '+end_x);
      animateObj.addSubject(new NumericalStyleSubject(thisDiv, 'top', start_y, end_y));
      animateObj.addSubject(new NumericalStyleSubject(thisDiv, 'left', start_x, end_x));
      animateObj.addSubject(new NumericalStyleSubject(thisDiv, 'width', 0, tgtWidth));
      animateObj.addSubject(new NumericalStyleSubject(thisDiv, 'height', 0, tgtHeight));
      animateObj.play();
      // thisDiv.style.top=midLine-(thisDiv.offsetHeight/2) + 'px';
      // thisDiv.style.left=
      // thisDiv.style.zindex=1000;    
      
      
    }     
  }
  return false;
}

function getMousePos(e) {
  var posx = 0;
  var posy = 0;
  if (!e) var e = window.event;
  if (e.pageX || e.pageY)
  {
  posx = e.pageX;
  posy = e.pageY;
  }
  else if (e.clientX || e.clientY)
  {
  posx = e.clientX + document.body.scrollLeft;
  posy = e.clientY + document.body.scrollTop;
  }
  return [posx,posy];  
}

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

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)
    );
}

include('/scripts/animator.js');

