var DOM = false;
var NS4 = false;
var IE = false;
var IE4 = false;
var Mac = false;
var IE4M = false;
var arrImages = [['No Images', '']]
var blnFormSubmit = false;

function ClearDirtyFlag ()
{
	blnFormSubmit = true;
}

function CheckDirtyFlagOnUnload ()
{
	document.onkeydown = CatchKeyDown;
	document.onclick = CatchOnClick;
	window.onbeforeunload = CatchOnBeforeUnLoad;
}

function IsFieldExcluded (Name)
{
	var strExcluded = document.forms[0].ExcludedFromDirtyCheck.value;

	// If there is no field name the do not exclude it
	if (Name.length == 0 || Name == null) { return false; }
		
	if (Name.indexOf("$") > 0)
	{
		Name = Name.substr(Name.indexOf("$")+1);
	}

	if (strExcluded.indexOf("$" + Name.toUpperCase() + "$") >= 0)
	{
		return true;
	}
	
	return false;
}

function CatchOnClick ()
{
	var strName = window.event.srcElement.name + "";
	var strPrefix = strName.substr(0, 3);
	var intStart = strName.lastIndexOf("$");
	var strExcluded = document.forms[0].ExcludedFromDirtyCheck.value;

	// Check to see if the field is excluded	
	if (IsFieldExcluded(strName)) { return; }
	
	// If the user cliked a link and the link is a script
	// then clear the dirty flag, this code needs to be here
	// to support post backs and popup windows
	if (typeof(window.event.srcElement.href) == "string")
	{
		var strHref = window.event.srcElement.href.toLowerCase();
		
		// I had to add this code to because of the images for the phone editing
		if (strHref.indexOf(".gif") >= 0)
		{
			// This code will ignore the phone images post back events, for
			// some reason the href is the image name and not the javascript call
			if (window.event.srcElement.name.toLowerCase().indexOf("_ignore") >= 0)
			{
				blnFormSubmit = true;
			}
		}
		else if (strHref.indexOf("javascript") >= 0)
		{
			blnFormSubmit = true;
		}
	}
	
	if (intStart > 0)
	{
		strPrefix = strName.substr(intStart+1, 3);
	}
	
	// See it the control is a text box by checking the profix
	switch (strPrefix)
	{
		case "ddl":
		case "lst":
		case "chk":
		case "rad":
			document.forms[0].DirtyFlag.value = "1";
			break;
			
		default:
			// Do Nothing
			break;
	}
}

function CatchKeyDown ()
{
	var strName = window.event.srcElement.name + "";
	var strPrefix = strName.substr(0, 3);
	var intStart = strName.lastIndexOf("$");

	// Check to see if the field is excluded	
	if (IsFieldExcluded(strName)) { return; }
	
	// Ignore special keys
	switch (window.event.keyCode)
	{
		case 9:		// Tab
		case 33:	// Page Up
		case 34:	// Page Down
		case 35:	// End
		case 36:	// Homw
		case 37:	// Left Arrow
		case 38:	// Up Arrow
		case 39:	// Right Arrow
		case 40:	// Down Arrow
		case 45:	// Insert
		case 112:	// F1
		case 113:	// F2
		case 114:	// F3
		case 115:	// F4
		case 116:	// F5
		case 117:	// F6
		case 118:	// F7
		case 119:	// F8
		case 120:	// F9
		case 121:	// F10
		case 122:	// F11
		case 123:	// F12
			return;
			break;

		default: 
			// DO nothing
			break;
	}
	
	if (intStart > 0)
	{
		strPrefix = strName.substr(intStart+1, 3);
	}

	// See it the control is a text box by checking the profix
	switch (strPrefix)
	{
		case "txt":
		case "ddl":
		case "lst":
		case "chk":
		case "rad":
			blnDirty = true;
			document.forms[0].DirtyFlag.value = "1";
			break;
			
		default:
			// Do Nothing	
	}
}

function CatchOnBeforeUnLoad ()
{
	var eventTarget = document.all("__EVENTTARGET");
	var tabClicked = document.all("TABCLICKED");
	var strTarget;
	
	if (eventTarget != null)
	{
	  strTarget = document.all("__EVENTTARGET").value;
	
	  if (strTarget.length > 0) { return; }
	}
	
	
  if (tabClicked != null)
  {
    if (tabClicked.value+"" == "1") { return; }
  }

	if (document.forms[0].DirtyFlag.value == "1" && !blnFormSubmit)
	{
		window.event.returnValue = "You have made modifications, but have not saved them. If you proceed your changes will not be saved.";
	}
}

function SwapImage(Name)
{
	var objImage;
	var strImage;
	var intStart;
	var strSuffix;
	
	// Get the image object
	objImage = document.getElementById(Name);

	// Get curent image name
	strImage = objImage.src + "";
	strImage = strImage.toLowerCase();
	
	// If the on image is displayed the display the off image
	if (strImage.lastIndexOf("_off.") > 0)
	{
		intStart = strImage.lastIndexOf("_off.");
		strImage = strImage.substr(0, intStart) + "_on." + strImage.substr(intStart + 5)
		strSuffix = "on";
	}
	else if (strImage.lastIndexOf("_on.") > 0)
	{
		intStart = strImage.lastIndexOf("_on.");
		strImage = strImage.substr(0, intStart) + "_off." + strImage.substr(intStart + 4)
		strSuffix = "off";
	}

	objImage.src =  strImage;
	return;
}

function checkKey ()
{
  if (window.event.keyCode == 13)
  {
    // kill the keystroke so that nothing happens
    window.event.keyCode = 0;
  }
}

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 GetGMACCookieMsg() {
  var msg = "Please note that this website uses a cookie to manage your session navigation.\n" + 
            "This cookie is used for management of your current session only and is not used\n" + 
            "for any other purpose. You must allow cookies for this site in order to use the\n" + 
            "features of gmac.com.";  
  return msg;          
}

function CountryChangeCheck (theList, originalValue)
{
  var usValue = "2000062";
  var canadaValue = "2000198";
  var obj;

  originalValue = originalValue + "";
  
  for (i=0;i<theList.length;++i)
  {
    obj = theList.options[i];
    
    if (obj.selected)
    {
      if (
        obj.value+"" != originalValue+"" && 
        ((obj.value+"" == usValue || originalValue == usValue) || (obj.value+"" == canadaValue || originalValue == canadaValue))
          )
      {
      	document.forms[0].submit();
      }
    }
  }
}

function CalculateTotal (controlName, codeDomainID)
{
  var prefix = "";
  var name = "";
  var pos = controlName.lastIndexOf("$") + 1;
  var total = 0;
  var obj;
  var objTotal = null;;
  var value;
  
  // Get the control prefix
  prefix = controlName.substring(0, pos);
  
	// Get Objects To Be Worked On

	for (i=0;i<document.forms[0].length;++i)
	{
	  obj = document.forms[0].elements[i];

    name = obj.name + "";
    
    // if (!confirm(name + " <> " + ":txtTotal_" + codeDomainID)) { return; }
    if (name.indexOf("$txtTotal_" + codeDomainID) > 0)
    {
      // alert(name);
      objTotal = obj;
    }
    else
    {
      if (name.substring(0, pos) == prefix)
      {
        name = name.substring(pos);
        
        if (name.indexOf("$txtTotal") < 0 && name.indexOf("txtSubTotal") < 0 && name.substring(0, 3) == "txt" && name.indexOf("txt" + codeDomainID + "0") >= 0)
        {
          // The Text boxes to be totaled will hav a number after the txt so if an invalid
          // number if returned then do not calculate the value
          value = new Number(name.substring(3));
          
          if (!isNaN(value))
          {
            value = new Number(obj.value);
            
            if (!isNaN(value))
            {
              total += new Number(obj.value);
            }
          }
        }
        
        if (name == "$txtTotal")
        {
          objTotal = obj;
        }
      }
    }
	}

  if (objTotal != null) { objTotal.value = total; }
}

function CalculateSubTotal (controlIDs, subTotalControl)
{
  var total = 0;
  var name = "";
  var ids = controlIDs.split(",");
  var objTotal = null;
  var value;
  
	for (i=0;i<document.forms[0].length;++i)
	{
	  obj = document.forms[0].elements[i];

    name = obj.name + "";
    
    if (name.indexOf(subTotalControl) > 0)
    {
      objTotal = obj;
    }
    else
    {
      for (j=0;j<ids.length;++j)
      {
        if (ids[j] == name)
        {
           value = new Number(obj.value);
          
          if (!isNaN(value))
          {
            total += new Number(obj.value);
          }
        }
      }
    }
	}

  if (objTotal != null) { objTotal.value = total; }
}

//HOME TOGGLE
 function toggleBox(swapTo) {
 	if ( swapTo == 1 ) {
 		document.getElementById('contenttab1').style.display = "block";
 		document.getElementById('contenttab2').style.display = "none";
 		document.getElementById('tab1').src="/GMACSite/images/home/tab-1-on.jpg";
 		document.getElementById('tab2').src="/GMACSite/images/home/tab-2-off.jpg";
 		document.getElementById('tab1').style.cursor="default";
 		document.getElementById('tab2').style.cursor="pointer";
 	}
 	if ( swapTo == 2 ) {
 		document.getElementById('contenttab2').style.display = "block";
 		document.getElementById('contenttab1').style.display = "none";
 		document.getElementById('tab1').src="/GMACSite/images/home/tab-1-off.jpg";
 		document.getElementById('tab2').src="/GMACSite/images/home/tab-2-on.jpg";
 		document.getElementById('tab1').style.cursor="pointer";
 		document.getElementById('tab2').style.cursor="default";
 	}
}
 
function enableToggle()
{
 	document.getElementById('aTab1').href = "javascript:toggleBox('1');";
 	document.getElementById('aTab2').href = "javascript:toggleBox('2');";
}

//SEARCH TEXT
function searchTextFocus() {
	if ( document.getElementById('searchfield').value == "search" ) {
		document.getElementById('searchfield').value = "";
	}
}

function searchTextBlur() {
	if ( document.getElementById('searchfield').value == "" ) {
		document.getElementById('searchfield').value = "search";
	}
		
}