/**
 *
 *    NAME: api.js
 *    AUTHOR: Sapient
 *    PURPOSE: Global JavaScript variables / functions.
 *
 *    VERSION: $Id: api.js,v 1.13 2006/02/10 15:33:35 hrishik Exp $
 */



//---------------------------------------------------------
//	GLOBAL VARIABLES
//----------------------------------------------------------

/**
 * Legacy Sapient crap.
 *
 */
function GetBr()
{
	var agt = navigator.userAgent.toLowerCase();
	var browser = navigator.appName.toLowerCase();
	var isNav = (browser=="netscape");
	if (agt.indexOf("msie") !=-1) { br = 'ie' }
	var isMac = (agt.indexOf("mac") != -1);
	if (isMac)	{
		platform='mac'
	} else {
		platform='pc'
	}
	if (isNav && agt.indexOf("netscape6") != -1) {
		ver=agt.substr(agt.indexOf("netscape6")).split("/")[1];
		br='nn6';

	} else if (isNav) {
		ver=(isNav)?parseFloat(navigator.appVersion):agt.split(";")[1].substr(6) ;
		 br='nn4'
	}
	return br;
    return platform;
}

//---------------------------------------------------------
//	FUNCTIONS
//----------------------------------------------------------

var DEFAULT_WINDOW_JS_OPTIONS = "width=700,height=500,toolbar=yes,location=yes,menubar=yes,resizable=yes,status=yes,scrollbars=yes";
var windowArray = new Array();
var windowIndexArray = new Array();

function windowHelper(theAnchor,theWindowName)
{
  var theWindow = null;

  for (i=0; i<windowIndexArray.length; i++) {
     if (windowIndexArray[i] == theWindowName)
        theWindow = windowArray[i];
  }

  if ( theWindow == null || theWindow.closed )
  {
     windowIndexArray[windowArray.length + 1] = theWindowName;
     windowArray[windowArray.length + 1] = window.open(theAnchor,theWindowName, DEFAULT_WINDOW_JS_OPTIONS);
  } else {
     theWindow.focus();
  }

  return false;
}

function defaultWindowOpener(theURL,theWindowName)
{
    return windowOpener(theURL,theWindowName,DEFAULT_WINDOW_JS_OPTIONS);
}

function windowOpener(theURL,theWindowName,jsoptions)
{
   var theWindow = null;
   var createNew = true;   
   if( windowArray[theWindowName] )
   {
      theWindow = windowArray[theWindowName];
      if( theWindow != null && !theWindow.closed )
      {
        theWindow.location.href = theURL;
        createNew = false;
      }
   }

   if(createNew)
   {
      theWindow = window.open(theURL,theWindowName,jsoptions);
      windowArray[theWindowName] = theWindow;
   }

   theWindow.focus();
   return false;
}


//---------------------------------------------------------

function Submitter(formName)
{
  document.forms[formName].submit();
}

function LoginSubmitter(formName)
{
   var cookieValue = ReadCookie("JSESSIONID");
   var action = document.forms[formName].action;
   var index = action.indexOf(";jsessionid=");
   var length = action.length;
   //alert("index = " + index);
   //alert("len = " + length);
   //alert(action);
   var newaction = "";
   if(index == -1) {
       newaction = document.forms[formName].action + ";jsessionid=" + cookieValue;
   }else{
       if(length == index+12){
           newaction = document.forms[formName].action +  cookieValue;
       }else{
           newaction = action;
       }
   }
   //alert("newaction = " + newaction);
   document.forms[formName].action = newaction;
   document.forms[formName].submit();
}

function ReadCookie(cookieName) {
    var theCookie=""+document.cookie;
    var ind=theCookie.indexOf(cookieName);
    if (ind==-1 || cookieName=="") return "";
    var ind1=theCookie.indexOf(';',ind);
    if (ind1==-1) ind1=theCookie.length;
    var cookieValue = unescape(theCookie.substring(ind+cookieName.length+1,ind1));
    return cookieValue;
}


function checkMenu(menuName) {
	//document[menuName].src = eval([menuName] + "_off.src");

		if ( menuOpen == null) {
		} else if (menuOpen == menuName){
		} else {
		SetCurrentMenu(menuName)

		}
	}

function closer()

{

	for (var n = 0; n < menuArray.length; n++)
	{
		menuName=menuArray[n].name;
        	SwitchVis(menuName,'hidden');
 	}
}

function SetCurrentMenu(menuName) {
	//document[menuName].src = eval([menuName] + "_on.src");

	if (currentMenu == menuName) {
		SwitchVis(menuName,'hidden')


	} else if (currentMenu == null){

		SwitchVis(menuName,'visible');
		currentMenu = menuName
		menuOpen = menuName

	} else {
		SwitchVis(currentMenu,'hidden');
		SwitchVis(menuName,'visible');
		currentMenu = menuName
		menuOpen = menuName
	}
}


function SwitchVis(theParent,visState) {
	GetBr();

	GetTheObj(theParent);
	eval("theObj." + style + "visibility ='" + visState + "'")

	if (visState == 'hidden') {
		currentMenu = null
		menuOpen = null
	}
}


function SwitchImg(imageName,state) {
	 if (document.images) {
			imgSource = document[imageName].src;
			if (state == "on") {
				onState = imgSource.replace(/_off/i,"_on");
  				//onState = imageName + "_on.gif";
				document[imageName].src = onState;
			} else //while (currentMenu != null)
				{
				offState = imgSource.replace(/_on/i,"_off");
				//offState = imageName + "_off.gif"
				document[imageName].src = offState;
			}
		}
	}

function SwitchInputImg(inputName,state) {
	imgSource = inputName.src
	if (state == "on") {
		onState = imgSource.replace(/_off/i,"_on");
  		//onState = imageName + "_on.gif";
		inputName.src = onState;
	} else //while (currentMenu != null)
	{
		offState = imgSource.replace(/_on/i,"_off");
		//offState = imageName + "_off.gif"
		inputName.src = offState;
	}
}

function GetTheObj(theItm,theWrp) {
	GetBr()
	if (br == 'ie') {
		theObj = eval("document.all." + theItm)
		style = "style."
	} else if (br == 'nn4') {
		if(theWrp) {
			theObj = eval("document.layers." + theWrp + ".layers. " + theItm )
			style = ""
		} else {
			theObj = eval("document.layers." + theItm)
			style = ""
		}
	} else if (br == 'nn6'){
		theObj = eval("document.getElementById(theItm)")
		style = "style."
	}

	return theObj
	return style
}

function SwitchBg(theColor,theItm,theWrp) {
theObj = GetTheObj(theItm,theWrp)
if (br == 'nn4') {

		theObj.Color = theColor

	} else {
	theObj.style.Color = theColor
	}
}

function WriteValues() {
	GetBr();
	args = "/js/menu_" + br + platform + "Values.js";
	document.write('<script src="' + args + '"></script>');

}



function Write(args) {
	document.write(args)
}


function wndOpenEmailPage(URL, hT, scl)
{
	 s2=new String(hT);
 	x=s2.charAt(0);

 	if(x=='u')
  		hT=400;

	if(hT=='yes')
 	{	 hT=400;
  		scl='yes';
 	}

	if(scl=="")
 		scl="no";

	y="toolbar=0,width=391,height=" + hT+ ",scrollbars=" + scl;
 	wndHandle=window.open(URL,'pop_up',y);

}

function wndOpen(URL,hT,scl)

{
	 s2=new String(hT);
 	x=s2.charAt(0);

 	if(x=='u')
  		hT=400;

	if(hT=='yes')
 	{	 hT=400;
  		scl='yes';
 	}

	if(scl=="")
 		scl="no";

	y="toolbar=0,width=380,height=" + hT+ ",scrollbars=" + scl;
 	wndHandle=window.open(URL,'pop_up',y);
	wndHandle.focus()

}

function wndOpenWithScroll(URL)
{
 wndHandle=window.open(URL,'pop_up','toolbar=0,width=350,height=500,scrollbars=yes');
}


function wndOpenmap(URL)
{
 wndHandle=window.open(URL,'pop_up','toolbar=0,width=350,height=400');
}

function netscaperesizeFix() {

	if (pageWidth != window.innerWidth || pageHeight != window.innerHeight)
	document.location.reload();
}

function clearText(thefield){
	if (thefield.defaultValue==thefield.value)
	thefield.value = "";
}

function linkOpener(URL)
{
	newwin=window.open(URL,'Window','resizable=yes, scrollbars=yes, toolbar=no, location=no, directories=no, status=no, menubar=no,height=550,width=620');
	if (window.focus) {newwin.focus()}
}

function linkOpenerFull(URL)
{
	newwin=window.open(URL,'Window','resizable=yes, scrollbars=yes, toolbar=yes, location=yes, directories=no, status=no, menubar=yes,height=720,width=820');
	if (window.focus) {newwin.focus()}
}
//----------------------------------------------------------------|

/**
 * Ripped this off WebMonkey...
 * http://hotwired.lycos.com/webmonkey/reference/javascript_code_library/wm_ckin_full/?tw=reference&category=dhtml
 */
function WM_checkIn(WM_id) {

/*
WM_checkIn()
Takes the ID of a positioned HTML element and returns an object reference.

Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)

Author: Taylor
Author Email: taylor@wired.com
Author URL: http://www.taylor.org/

Usage: WM_checkIn('id')
*/

  if(!WM_id) return;
  // First we initialize all the variables.
  var theObj,ss,sr,i,j,WM_layers=new Array();
  // This chunk handles the IE portion of the checkIn code.
  if (document.all) {
    // This checks to see if the inline style declaration has
    // a position property associated with it. If not, it will
    // scan the global stylesheets for the ID.
    if((document.all[WM_id].style.position != 'absolute') && (document.all[WM_id].style.position != 'relative')){
      // This little loop I'm very proud of, because it's kinda
      // slick and I wrote it all myself. It loops through all
      // global stylesheets and all the rules in each stylesheet,
      // tests for the selected ID, then returns that as the object.
      for (ss=0 ; ss < document.styleSheets.length; ss++) {
        for (sr=0 ; sr < document.styleSheets(ss).rules.length; sr++) {
          if (document.styleSheets(ss).rules(sr).selectorText == '#' + WM_id) {
            theObj = document.styleSheets(ss).rules(sr).style;
            break;
          }
        }
      }
    } else {
      // This works the same as in the light version, so you can
      // use inline styles.
      theObj = document.all[WM_id].style;
    }
  } else if(document.layers) {
    // Now we're in Netscapeland. The main problem here
    // is finding the object in a maze of hierarchy.
    // I wish I could say that I'm proud of this code,
    // because it's really slick. Unfortunately, I ripped
    // it off from Macromedia Dreamweaver's drag layer code
    // (with permission, of course :-)
    // Dreamweaver/Configuration/Behaviors/Actions/Drag Layer.htm
    // It works wonderfully and solves the problem.
    WM_layers = new Array();
    with (document) {
      for (i=0; i<layers.length; i++) WM_layers[i]=layers[i]; {
        for (i=0; i<WM_layers.length; i++) {
          if (WM_layers[i].document && WM_layers[i].document.layers) {
            for (j=0; j<WM_layers[i].document.layers.length; j++) {
              WM_layers[WM_layers.length] = WM_layers[i].document.layers[j];
            }
            if(WM_layers[i].name == WM_id){
              // So if the code matches the name of the layer,
              // return the reference.
              theObj = WM_layers[i];
            }
          }
        }
      }
    }
  }
  return theObj;
}

//----------------------------------------------------------------------------------------------
