function gbl_addLoadEvent(func) 
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') 
  {
    window.onload = func;
  } 
  else 
  {
    window.onload = function() 
    {
      oldonload();
      func();
    }
  }
}

function externalLinks() 
{ 
	if (!(document.getElementsByTagName)) return; 
	var anchors = document.getElementsByTagName('a');
	for (var i=0; i<anchors.length; i++) 
	{
		var anchor = anchors[i];
		if(anchor.getAttribute('href') && anchor.getAttribute('rel') == 'external')
		{
			anchor.target = "_blank";
		}
	}
}

//In javascript, for some reason, the base tag is not used.
//This function addresses this problem in a limited way.
function gbl_FixRelativeURL(strRelativeURL){	
	if (strRelativeURL.indexOf('http://') > -1) {
		return strRelativeURL;
	}
	else if(document.all && document.all.tags("base") && document.all.tags("base")[0])
		return document.all.tags("base")[0].href + strRelativeURL;
	else
		return strRelativeURL;
}

function gbl_OpenFeatureWindow(url) {
	var width='900';
	var height='700';
	if (url.indexOf('DMTToolsRedirect.aspx') > -1) {
		if (url.indexOf('ProductID=39') > -1) {
			width='950';
		}
		else if (url.indexOf('ProductID=40') > -1) {
			width='950';
		}
	}
	gbl_OpenNewWindow(gbl_FixRelativeURL(url),'','height='+height+', width='+width+', left=150, top=10, screenx=150, screeny=10, toolbar=yes, scrollbars=yes,location=yes, resizable=yes, menubar=yes, status=yes');
}

function gbl_OpenNewWindow(strWinURL, strWinName, strOptions){
	var win;	
	if(strOptions == ''){
		strOptions = 'scrollbars=yes,location=yes,toolbar=yes,resizable=yes,menubar=yes,status=yes'
	}
	win = window.open(strWinURL, strWinName, strOptions);
	win.focus();
}

function gbl_addLoadEvent(func) 
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function') 
  {
    window.onload = func;
  } 
  else 
  {
    window.onload = function() 
    {
      oldonload();
      func();
    }
  }
}

function gbl_Confirm(strItem){
	if(confirm(strItem)){
		return true;
	}
	else{
		return false;
	}
}
function gbl_ConfirmDelete(strItem){
	if(confirm('Are you sure you want to delete this ' + strItem + '?')){
		return true;
	}
	else{
		return false;
	}
}

function AddClass(obj, classname) {
    if(!(obj==null)) {
		if (obj.className.length == 0) {
			obj.className = classname;
		}
		else {
			if (!(obj.className.match(new RegExp(classname, "g")))) {
				obj.className += ' ' + classname;	
			}
		}
    }
}
function RemoveClass(obj, classname) {
    /*October 3, 2005 Note_RC:
      I came across a bug when using this function that if you called the function to remove a class
      that wasn't there, and the object using the said class had a transparent background, IE would
      not "repaint" the screen.*/
    if(!(obj==null)) {
	    if (obj.className.match(new RegExp(classname, "g"))) {
		    obj.className = obj.className.replace(new RegExp(classname, "g"), "");
	    }
    }
}
function SetClass(obj, classname) {
    if(!(obj==null)) {
		obj.className = classname;
    }
}

//need the empty implementation so that other browsers will not throw an error
function HoverOn()	{
}
function HoverOff(){
}

//opens and closes the Eligible Models Menu when the button is clicked
function ToggleMenu(frameid) 
{ 
      var currentfr = document.getElementById(frameid);
      if (!currentfr.getElementsByTagName) return;
       
      var anchors = currentfr.getElementsByTagName("ul"); 
      for (var i=0; i<anchors.length; i++) 
      {    
            var anchor = anchors[i]; 
            if(anchor.style.display == "none")
            {
                anchor.style.display = "block"; 
            }
            else
            {
                anchor.style.display = "none"; 
            }
      } 
}

//Checks the query string to see which model page the user is on, and
//highlights that model name in the side menu
function CheckMenu()
{
	var currentModel = getQueryValue("model");

    //since the eligible models section defaults to the eclipse, the
    //eclipse menu item is highlighted by default
	switch (currentModel)
	{
	    case "eclipse":
	    document.getElementById(10).childNodes[0].style.color = "red";
	    break
	    case "spyder":
	    document.getElementById(11).childNodes[0].style.color = "red";
	    break
	    case "lancer":
	    document.getElementById(12).childNodes[0].style.color = "red";
	    break
	    case "galant":
	    document.getElementById(13).childNodes[0].style.color = "red";
	    break
	    case "endeavor":
	    document.getElementById(14).childNodes[0].style.color = "red";
	    break
	    case "outlander":
	    document.getElementById(15).childNodes[0].style.color = "red";
	    break
	    case "sportback":
	    document.getElementById(16).childNodes[0].style.color = "red";
	    break
	    case "sportbackralliart":
	    document.getElementById(17).childNodes[0].style.color = "red";
	    break
	    case "rvr":
	    document.getElementById(18).childNodes[0].style.color = "red";
	    break
	}
}

//a function that grabs the first instance of the query string 
//parameter given by "name" and returns it's value
function getQueryValue( name )
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
	
    if( results == null )    
	    return "";  
    else    
	    return results[1];
}

//checks to see if the current user's password matches the supplied one
//objFormElement - form input containing password to check
function usr_checkCurrentPassword(objFormElement){		
	var strPassword;
	var strErrorCode;	
	
	strPassword = fv_getFormElementValue(objFormElement);	
				
	strErrorCode = gbl_DoActionWebService('User/CheckCurrentPassword.aspx', 'password=' + escape(strPassword));
	
	if(strErrorCode == '305'){
		return true;
	} else {
		return false;
	}
}
//checks to see if the proposed new user's username has not already been assigned to another user
//objFormElement - form input containing username to check
//arrOtherArguments[0] - form input containing company id of new user
function usr_checkDMTLoginAvailable(objFormElement, arrOtherArguments){
	var strLogin;
	var strCompanyID;
	
	strLogin=fv_getFormElementValue(objFormElement);	
	strCompanyID=fv_getFormElementValue(arrOtherArguments[0]);	
	
	gbl_strLoginAvailableErrorCode=gbl_DoActionWebService('User/CheckDMTLoginAvailable.aspx', 'login=' + escape(strLogin) + '&companyid=' + escape(strCompanyID));

	if(gbl_strLoginAvailableErrorCode == '306' || gbl_strLoginAvailableErrorCode == '357'){
		return true;
	} else {
		return false;
	}
}
function usr_checkPartnerLoginAvailable(objFormElement, arrOtherArguments){
	var strLogin;
	var strCompanyID;
	
	strLogin=fv_getFormElementValue(objFormElement);	
	strCompanyID=fv_getFormElementValue(arrOtherArguments[0]);	
	
	gbl_strLoginAvailableErrorCode=gbl_DoActionWebService('User/CheckPartnerLoginAvailable.aspx', 'login=' + escape(strLogin) + '&companyid=' + escape(strCompanyID));

	if(gbl_strLoginAvailableErrorCode == '306' || gbl_strLoginAvailableErrorCode == '357'){
		return true;
	} else {
		return false;
	}
}
function usr_checkCompanyDomainExists(objFormElement){
	if(gbl_strLoginAvailableErrorCode!='357'){
		return true;
	} else {
		return false;
	}		
}

//	- returns error code from response (string)
//	- may return blank error code on no response
// TODO: add robust error handling
function gbl_DoActionWebService(strURL, strQueryString){
	
	var xmlhttp = gbl_GetXMLHttpRequestObject();
	xmlhttp.open('POST', gbl_FixRelativeURL(strURL), false);	
	xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	xmlhttp.setRequestHeader('Content-length', strQueryString.length);
	xmlhttp.send(strQueryString);
	
	var intNumChildren;
	var objDom= gbl_GetXMLDomObject();
	
	objDom.resolveExternals=false;
	objDom.validateOnParse=false;
	objDom.async=false;	
	var isOpera=(navigator.userAgent.indexOf("Opera")!=-1)?true:false;
	var isMac=(navigator.userAgent.indexOf("Mac")!=-1);

    if((document.all && !isOpera && (!isMac || navigator.appVersion.indexOf("MSIE 4")==-1)))
    {	//if IE
		objDom.loadXML(xmlhttp.responseText);
	}else{ 
	    //else if netscape/opera/safari
		objDom  = xmlhttp.responseXML;
	}
	
	if(objDom){
		intNumChildren = objDom.documentElement.childNodes.length;		
		if(intNumChildren>0){
			var nodeSelected=objDom.documentElement.firstChild;				
			return nodeSelected.firstChild.nodeValue;
		} else {
			return '';
		}
	}else {
		return '';
	}
}
function gbl_GetXMLHttpRequestObject(){

	var obj;
	try{ //to get the mozilla httprequest object
		obj = new XMLHttpRequest();
	}catch(e){
		try{ //to get MS HTTP request object
			obj=new ActiveXObject("Msxml2.XMLHTTP.4.0");
		}catch(e){
			try{ //to get MS HTTP request object
				obj=new ActiveXObject("Msxml2.XMLHTTP")
			}catch(e){
				try{// to get the old MS HTTP request object
					obj = new ActiveXObject("microsoft.XMLHTTP"); 
				}catch(e){
					throw new Error("No XML requestor found.");
				}
			}
		}
	}
	return obj;
}
function gbl_GetXMLDomObject(){
		if(document.implementation && document.implementation.createDocument){
			return document.implementation.createDocument("", "doc", null);
		} else {
			return new ActiveXObject("Msxml2.DOMDocument");
		}		
}
