/*
	This function enables us to add mutliple scripts to
	different events from different files.
*/
function addEvent(obj, evType, fn) {
 	if (obj.addEventListener) {
   		obj.addEventListener(evType, fn, true);
   		return true;
 	} else if (obj.attachEvent) {
   		var r = obj.attachEvent("on"+evType, fn);
   		return r;
	} else {
		return false;
	}
}

function prepareFocus() {

	if (!document.getElementsByTagName) return false;
	
	// We get all of the input and select tags and look
	// for one that has a class of "autofocus"
	var inputFields = document.getElementsByTagName('input');
	var selectFields = document.getElementsByTagName('select');
	
	// Check all of the tags for the presence of an "autofocus" class
	for (i=0;i<inputFields.length;i++) {
		if (inputFields[i].className.indexOf('autofocus')!=-1) {
			inputFields[i].focus();
			break;
		}
	}
	for (i=0;i<selectFields.length;i++) {
		if (selectFields[i].className.indexOf('autofocus')!=-1) {
			selectFields[i].focus();
			break;
		}
	}
}

function prepareFooter() {

	var theTimeout;
	var timeoutLength = 300;

	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;

	// Get a reference to the ClickMotive link in the footer
	var footerLink = document.getElementById("clickMotiveLink");

	if (footerLink) {
		// Create the div that will be shown and hidden
		var audienceLinks = document.createElement("div");

		// Create the unordered list
		var linkList = document.createElement("ul");
	
		// Create the list item & anchor for the car buyers link
		var buyersListItem = document.createElement("li");
		var buyersLink = document.createElement("a");
		var buyersText = document.createTextNode("Car Buyer Privacy");
		buyersLink.Id = buyersLink;
		buyersLink.Target = "_blank";
		buyersLink.href = "http://www.clickmotive.com/buyers/";
		buyersLink.className = "smallWindow";
		buyersLink.appendChild(buyersText);
		buyersListItem.appendChild(buyersLink);
		linkList.appendChild(buyersListItem);
	
		// Create the list item & anchor for the dealers link
		var dealersListItem = document.createElement("li");
		var dealersLink = document.createElement("a");
		var dealersText = document.createTextNode("Dealer Signup");
		dealersLink.Id = dealersLink;
		dealersLink.Target = "_blank";
		dealersLink.href = "http://www.clickmotive.com/dealers/";
		dealersLink.className = "largeWindow";
		dealersLink.appendChild(dealersText);
		dealersListItem.appendChild(dealersLink);
		linkList.appendChild(dealersListItem);
		
		// Insert the ul into the div and the div into the markup after link
		audienceLinks.appendChild(linkList);
		footerLink.parentNode.insertBefore(audienceLinks, footerLink);
		audienceLinks.id = "audienceLinks";
		// If we mouse over the audience links, we want to clear the timeout that hides them

		// Add the mouseover effects to the ClickMotive link and audience links div
		footerLink.onmouseover = function() {
			/* If we hover over the link, we want to prevent the div from disappearing by clearning 
			   the timeout */
			clearTimeout(theTimeout);
			showAudienceLinks();
		}
		footerLink.onmouseout = function() {
			/* When we move out of the link, we want to wait a moment before hiding the div in case
			   the mouse moves over the div */
			theTimeout = setTimeout("hideAudienceLinks()",timeoutLength);
		}
		audienceLinks.onmouseover = function() {
			/* If we hover over the div, we want to prevent it from disappearing by clearning 
			   the timeout */
			clearTimeout(theTimeout);
		}
		audienceLinks.onmouseout = function() {
			/* When we move out of the div, we want to wait a moment before hiding the div in case
			   the mouse moves over the div */
			theTimeout = setTimeout("hideAudienceLinks()",timeoutLength);
		}
	
		// We want the audienceLinks to be hidden by default
		audienceLinks.style.display = 'none';
	}
}

function showAudienceLinks () {

	// Display the audience links
	var audienceLinks = document.getElementById('audienceLinks');
	if (audienceLinks.style.display == 'none') {
		// Get a reference to the ClickMotive link in the footer
		var footerLink = document.getElementById("clickMotiveLink");

		// Set the positioning values for the div relative to the link
		//alert(footerLink.offsetTop + " " + footerLink.offsetLeft + " " + footerLink.offsetHeight);
		var leftCorner = (footerLink.offsetLeft-14) + 'px';
		var bottomCorner = (footerLink.offsetHeight) + 'px';
		audienceLinks.style.bottom = bottomCorner;
		audienceLinks.style.left = leftCorner;

		// Now that it's positioned correctly, we can display it
		audienceLinks.style.display = '';
	}
}

function hideAudienceLinks () {
	// Hide the audience links
	var audienceLinks = document.getElementById('audienceLinks');
	if (audienceLinks.style.display == '') {
		audienceLinks.style.display = 'none';
	}
}

function openWindow (targetUrl, windowSize) {
	var windowParameters = "";
	var windowName = "";
	
	if (windowSize.substr(0,13) == "customWindow:")
	{
	    windowName = "customWindow";
	    var temp = new Array();
	    temp = windowSize.split(':');
	    if (temp.length > 1)
	    {
	        windowParameters = temp[1];
	    }
	    else
	    {
	        windowParameters = "width=350,height=250,resizable=1,scrollbars=1,status=1";
	    }	    
	}
	else
	{
	    switch (windowSize) {
	        case 'thumbWindow':
			    windowName = "thumbWindow";
			    windowParameters = "width=350,height=250,resizable=1,scrollbars=1,status=1";
			    break;
		    case 'smallWindow':
			    windowName = "smallWindow";
			    windowParameters = "width=400,height=500,resizable=1,scrollbars=1,status=1";
			    break;
		    case 'mediumWindow':
			    windowName = "mediumWindow";
			    windowParameters = "width=500,height=600,resizable=1,scrollbars=1,status=1";
			    break;
		    case 'largeWindow':
			    windowName = "largeWindow";
			    windowParameters = "width=800,height=600,location=1,menubar=1,resizable=1,scrollbars=1,status=1,titlebar=1,toolbar=1";
			    break;
		    default:
 			    windowName = "largeWindow";
			    windowParameters = "width=790,height=600,location=1,menubar=1,resizable=1,scrollbars=1,status=1,titlebar=1,toolbar=1";
			    break;		
	    }
	} 
	oNewDoc = window.open(targetUrl, windowName, windowParameters );
	oNewDoc.focus();
}

function preparePopups () {

	if (!document.getElementsByTagName) return false;
	
	var links = document.getElementsByTagName('a');

	for (i=0;i<links.length;i++) {

		if (links[i].className.indexOf("Window")>-1) {
			links[i].onclick = function() {
				openWindow(this.href, this.className);
				return false;	
			}
		}
	}
}

function ZipCodeTextBoxValidate(sender, arguments)
{       
    if(isNaN(arguments.Value) || (arguments.Value.length != 5))
    {
        arguments.IsValid = false;
    }
    else
    {
        arguments.IsValid = true;
    }
}

addEvent(window, 'load', prepareFooter);
addEvent(window, 'load', preparePopups);
addEvent(window, 'load', prepareFocus);
