var ENTER = 13;

// Hack for older browser support
if (document.all && !document.getElementById) {
    document.getElementById = function(id) {
         return document.all[id];
    }
}

function $(id) {
	return document.getElementById(id);
}

/**
 * Add: onKeyPress="submitOnEnter(this, event)" to your form fields
 *      to add submit-on-enter functionality.
 */
function submitOnEnter(field, event) {
	if (window.event) event = window.event;
	var key = event.keyCode;
	
	if (key == ENTER) {
		field.form.submit();
		return false;
	}
	
	return true;
}

/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

function swapOut(element, index) {
	document.getElementById(element).src = rollimage[index + 1].src;
	return true;
}

function swapBack(element, index) {
	document.getElementById(element).src = rollimage[index].src;
	return true;
}

		//DEFINE SSO
		function SSO( id ){
			this.id = id
			this.question1;
			this.question2;	
			
			this.setQuestion1 = function( answer ){
				this.question1 = answer;
			}
			
			this.getQuestion1 = function(){
				return this.question1;
			}
			
			this.setQuestion2 = function( answer ){
				this.question2 = answer;
			}
			
			this.execute = function(){
				var url = '/re_myhomes_merge.jsp?';
				if( this.question1 )
					url += 'action=merge';
				else
					url += 'action=inactive';
				
				if( this.question2 )
					url += '&use=ool';
				else
					url += '&use=oa';
					
				window.location = url;
			}
	
		}
		
		//create an sso
		var sso = null;
		
		function doSSO( id ){
			sso = new SSO( id );
			document.getElementById( 'oolid' ).innerHTML = sso.id;
			if (document.getElementById( 'vod_frame' )) {
				document.getElementById( 'vod_frame' ).src = '/re_home_vod_include2.jsp?autoStart=false';
			}
			document.getElementById( 'mergeConfirmContainer' ).style.visibility = 'visible';			
		}

		function showBlock( theId ){
			document.getElementById( theId ).style.display = 'block';
		} 
		
		function swapQuestion1(){
			if( sso != null && sso.getQuestion1() == 0 )
				swapOut('myautos_no', 2);
			else
				swapBack('myautos_no', 2);
		}
	//************ Global Layer Functions **************************
	//************ Browser Detection *******************************
	var n4 = (document.layers)? true:false;
	var ie = (document.all)? true:false;
	var n6 = (document.getElementById)? true:false;
	if (ie) n6=false;
	 //************************* Moving Layers ***********
	function setPosition(lay1,fx,fy) {
		if (n4) document.layers[lay1].moveTo(fx,fy);
		if (ie) {
			document.all[lay1].style.pixelLeft=fx;
			document.all[lay1].style.pixelTop=fy;
		}
		if (n6) {
			document.getElementById(lay1).style.left=fx+"px";
			document.getElementById(lay1).style.top=fy+"px";
		}
	}
	//*********************** Must be called onload or inbody ********
	function captureMouseEvents() {
		if (n4) {
			window.captureEvents(Event.MOUSEMOVE);
			window.onMouseMove = MouseMove;
		}
		if (ie) {document.onmousemove = MouseMove;}
		if (n6) {document.addEventListener('mousemove',MouseMove,false);}
	}
	var mouseX=0; var mouseY=0;
	function MouseMove(e) {
	    if (n4) {mouseX=e.pageX; mouseY=e.pageY;}
	    if (ie) {
	    	mouseX=window.event.x+document.body.scrollLeft;
			mouseY=window.event.y+document.body.scrollTop;		
		}
	    if (n6) {
	    	mouseX=e.clientX+self.pageXOffset;;
			mouseY=e.clientY+self.pageYOffset;
		}
	}
	function showDiv(showDiv, hideDivs) {
		if (hideDivs && hideDivs.length > 0) {
			for (var i=0; i<hideDivs.length; i++) {
				$(hideDivs[i]).style.visibility = 'hidden';
			}
		}
		$(showDiv).style.visibility = 'visible';
	}
	function hideDiv(hideDiv) {
		$(hideDiv).style.visibility = 'hidden';
	}

	function isReadyForMerge() {
		var link = location.href;
		return (link.indexOf('o=t') > 0);
	}
	function setOmniturePageName(pageName) {
		s_pageName=pageName;
		s_dc(s_account);
		if(s_code) {s_d.write(s_code);}
 	}
	function updateSSOPageName() {
		var link = location.href;
		if (link.indexOf('a=t') > 0) {
			//sso auto login
			setOmniturePageName(s_pageName + ' SSO');
		} else if (link.indexOf('a=u') > 0) {
			//sso auto login
			setOmniture(s_pageName + ' SSO Unique');
		} else if (isReadyForMerge()) {
			//sso merge
			setOmniturePageName(s_pageName + ' Merge Popup');
		}
	}

	function showlay(name) {
		document.getElementById(name).style.visibility='visible';
	}

	function hidelay(name) {
		document.getElementById(name).style.visibility='hidden';
	}