/*

utils.js / v0.9  -- seamless web javascript utility functions


*/

var normal_color = "#000";
var search_field_class = 'SearchFieldWithMessage';
var streetInput = 'street';

// attach this to the onfocus of an INPUT or TEXTAREA, passing the 'this' object as the argument
function textFieldStoreAndClear(el) {
	el.originalText = (el.originalText == null) ? el.value : el.originalText;
	if (el.value == el.originalText) el.value = '';

	if (el.className == '') {
		el.originalColor = (el.originalColor == null) ? el.style.color : el.originalColor;
		if (el.style.color == el.originalColor) el.style.color = normal_color;
	}
}


// attach this to the onblur of an INPUT or TEXTAREA, passing the 'this' object as the argument
function textFieldRestore(el) {
	el.value = (el.value == '') ? el.originalText : el.value;
	if (el.className == search_field_class || el.name == streetInput) {
		el.style.color = (el.value == el.originalText) ? el.originalColor : el.style.color;
	}
}


var nav_off_regex = /(.+)[0](\.gif)$/;

function initNavMouseovers(nav_id) {

	if (getDomObj(nav_id)) {
		var nav_images = Array();
		nav_images = DOM_OBJECTS[nav_id].getElementsByTagName('img');

		for (var i=0; i< nav_images.length; i++) {
			if (!nav_off_regex.test(nav_images[i].src)) continue;

			nav_images[i].std_img = new Image();
			nav_images[i].std_img.src = nav_images[i].src;

			nav_images[i].hover_img = new Image();
			nav_images[i].hover_img.src = nav_images[i].src.replace(nav_off_regex, "$1" + "1" + "$2");

			nav_images[i].onmouseover = function() { this.src = this.hover_img.src; };
			nav_images[i].onmouseout = function() { this.src = this.std_img.src; };
		}

	}

} // END: initNavMouseovers()



function adjustNeighborhoodSearchArea(select_box) {
	getDomObj('NeighborhoodNewAddress');
	getDomObj('NeighborhoodSearch');

	switch (select_box.options[select_box.selectedIndex].value) {
		case "NEW_ADDRESS" :
			DOM_OBJECTS['NeighborhoodNewAddress'].style.display = 'block';
		break;

		case "NEIGHBORHOOD" :
			DOM_OBJECTS['NeighborhoodNewAddress'].style.display = 'none';
		break;

		case "":
			DOM_OBJECTS['NeighborhoodNewAddress'].style.display = 'none';
		break;

		default :
			DOM_OBJECTS['NeighborhoodNewAddress'].style.display = 'none';
		break;
	}

} // END: adjustNeighborhoodSearchArea()



// debugging function
function debug(msg){
	if (SETTINGS['debug']) {
		if (document.all) {
			alert(msg);
		} else {
			java.lang.System.out.println(msg);
		}
	}
}



// this is way simple... don't try and use it for anything more than: EL[#|.]IdOrClassName
function parseCSSSelector(selector) {
	var css_regex = /^(\w+)([#.])?(\w+)?$/;
	var result = css_regex.exec(selector);

	var class_or_id = (result[2] == ".") ? ".className" : ".id";
	if (result[2] == "") class_or_id = null;

	return [result[1], class_or_id, result[3]];
} // END: parseCSSSelector


// =============================================================================

var popped_windows = null;

function popWin(window_type) {
	var win_width = 500;
	var win_height = 400;
	var win_url = null;

	switch (window_type) {
		case 'USER_SEARCH':
			win_url = './popup.search-results.html';
		break;

		case 'ADD_USER':
			win_url = './popup.recently-allocated-users.html';
		break;

		case 'EDIT_CC_INFO':
			win_url = './popup.edit-credit-card-info.html';
		break;

		case 'PASSWORD_REMINDER':
			win_url = './popup.password-reminder.html';
		break;

		default:
		break;
	};

	if (win_url) {
		var x = window.open(win_url ,'win_' + window_type, 'width=' + win_width + ',height=' + win_height + ',toolbars=no,scrolling=yes,scrollbars=yes');
	} else {
		alert('no URL set for window type ' + window_type);
	}

}



function gotoURL(url) {
	window.location.href = url;
} // END: gotoURL


// =============================================================================


var page_onload_events = [];

function PageOnloadEvent(expression, priority) {
	this.expression = expression;
	this.priority = priority; // will eventually use this if ordering becomes a problem
	this.return_value = null;
} // END: OnloadEvent

function addToOnloadHandler(expression) {
	page_onload_events[page_onload_events.length] = new PageOnloadEvent(expression);
} // END: addToOnloadHandler

window.onload = function() {
	for (var i=0; i < page_onload_events.length; i++) {
		page_onload_events[i].return_value = eval(page_onload_events[i].expression);
	}
}; // END: window.onload


addToOnloadHandler("getDHTMLObjects()");


window.onresize = function() {
	recalcDHTML();
};



// =============================================================================


//	alert("obj.offsetParent" + "\n" +  debugObject(obj.offsetParent));

function debugObject(obj) {
	txt = "";
	txt += obj + "\n";
	txt += " - nodeName =  " + obj.nodeName + "\n";
	txt += " - id =  " + obj.id + "\n";
	txt += " - offsetParent.nodeName =  " + obj.offsetParent.nodeName + "\n";
	txt += " - offsetTop =  " + obj.offsetTop + "\n";
	txt += " - offsetLeft =  " + obj.offsetLeft + "\n";
	txt += " - offsetWidth =  " + obj.offsetWidth + "\n";

	return (txt + "\n");
} // END: debugObject


var browser = {
	ua : navigator.userAgent/*.toLowerCase()*/,
	app_name : navigator.appName.toLowerCase(),
	major_version : parseInt(navigator.appVersion),

	isIEwin : function() { return ( /MSIE/i.test(this.ua) && /Windows/i.test(this.ua) ) },
	isIEmac : function() { return ( /MSIE/i.test(this.ua) && /Macintosh/i.test(this.ua) ) },
	isGecko : function() { return ( /Gecko/i.test(this.ua) && /Mozilla\/5\.0/i.test(this.ua) ) },
	isKhtml : function() { return ( /KHTML/i.test(this.ua) ) },

	isWin : function() { return ( /Windows/i.test(this.ua) ) },
	isMac : function() { return ( /Macintosh/i.test(this.ua) ) },
	isLinux : function() { return ( /Linux/i.test(this.ua) ) },

	isIEwin50 : function() { return ( /MSIE\ 5\.0/.test(this.ua) ) },
	isIEwin55 : function() { return ( /MSIE\ 5\.5/.test(this.ua) ) },

	isNS6 : function() { return ( /Netscape6\/6\./.test(this.ua) ) },
	isNS60 : function() { return ( /Netscape6\/6\.0/.test(this.ua) ) },

	// TODO: check against gecko version numbers, either dates or revs

	supportsOpacity : function() { return false; }
};




function debugPosition(obj) {
	alert("" +
 + "\n" + "offsetLeft = " + obj.offsetLeft
 + "\n" + "offsetParent.offsetLeft = " + obj.offsetParent.offsetLeft
// + "\n" + "2nd offsetParent.offsetLeft = " + obj.offsetParent.offsetParent.offsetLeft
// + "\n" + "3rd offsetParent.offsetLeft = " + obj.offsetParent.offsetParent.offsetParent.offsetLeft
// + "\n" + "4th offsetParent.offsetLeft = " + obj.offsetParent.offsetParent.offsetParent.offsetParent.offsetLeft
 + "\n"
 + "\n" + "offsetLeft = " + obj.offsetLeft
 + "\n" + "parentNode.offsetLeft = " + obj.parentNode.offsetLeft
 + "\n" + "2nd parentNode.offsetLeft = " + obj.parentNode.parentNode.offsetLeft
 + "\n" + "3rd parentNode.offsetLeft = " + obj.parentNode.parentNode.parentNode.offsetLeft
 + "\n" + "4th parentNode.offsetLeft = " + obj.parentNode.parentNode.parentNode.parentNode.offsetLeft
 + "\n" + "5th parentNode.offsetLeft = " + obj.parentNode.parentNode.parentNode.parentNode.parentNode.offsetLeft
 + "\n" + "6th parentNode.offsetLeft = " + obj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.offsetLeft
 + "\n"
 + "\n"

 + "\n" + "offsetTop = " + obj.offsetTop
 + "\n" + "offsetParent.offsetTop = " + obj.offsetParent.offsetTop
// + "\n" + "2nd offsetParent.offsetTop = " + obj.offsetParent.offsetParent.offsetTop
// + "\n" + "3rd offsetParent.offsetTop = " + obj.offsetParent.offsetParent.offsetParent.offsetTop
// + "\n" + "4th offsetParent.offsetTop = " + obj.offsetParent.offsetParent.offsetParent.offsetParent.offsetTop
 + "\n"
 + "\n" + "offsetTop = " + obj.offsetTop
 + "\n" + "parentNode.offsetTop = " + obj.parentNode.offsetTop
 + "\n" + "2nd parentNode.offsetTop = " + obj.parentNode.parentNode.offsetTop
 + "\n" + "3rd parentNode.offsetTop = " + obj.parentNode.parentNode.parentNode.offsetTop
 + "\n" + "4th parentNode.offsetTop = " + obj.parentNode.parentNode.parentNode.parentNode.offsetTop
 + "\n" + "5th parentNode.offsetTop = " + obj.parentNode.parentNode.parentNode.parentNode.parentNode.offsetTop
 + "\n" + "6th parentNode.offsetTop = " + obj.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.offsetTop
	);
}
var selects = new Array();
function HideAllSelects() {
    selects = new Array();
    if(document.all){
	for (i = 0; i < document.all.length; i++){
	    if (document.all[i].options) {
		document.all[i].style.visibility = "hidden";
		selects[selects.length] = document.all[i];
	    }
	}
    }
}

function ShowAllSelects() {
    for (i = 0; i < selects.length; i++){
	selects[i].style.visibility = "visible";
    }
    selects = new Array();
}
function ShowPrivacyPopup() {
	    UserSearchWindow = window.open("../public/privacy.html", "UserSearch","menubar=yes,toolbar=no,width=780,height=580,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");	
}
function ShowBrowserSupportPopup() {
	    UserSearchWindow = window.open("../public/browsersupport.html", "UserSearch","menubar=yes,toolbar=no,width=780,height=580,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");	
}
function ShowTermsPopup() {
			    UserSearchWindow = window.open("../public/conditions.html", "UserSearch","menubar=yes,toolbar=no,width=780,height=580,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");	
}
function LaunchUtilityPage(SelectedUtility,ReturnUrl,TargetPage) {
    switch(SelectedUtility) {
	case "Help" :
	UtilityWindow = window.open("Help.m?internalReturnUrl=" + ReturnUrl, "UtilityWindow","toolbar=no,width=800,height=600,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");
	break;
	case "OrderHistory" :
	UtilityWindow = window.open("OrderHistory.m?internalReturnUrl=" + ReturnUrl + "&UtilityTargetPage=" + TargetPage, "UtilityWindow","toolbar=no,width=800,height=600,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");
	break;
    };
}
function LaunchContentPage(SelectedContent,TargetDiv) {
    switch(SelectedContent) {
	case "OurService" :
	ContentWindow = window.open("OurService.m", "ContentWindow","toolbar=no,width=800,height=600,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");
	break;
	case "InTheNews" :
	ContentWindow = window.open("InTheNews.m?activeNewsDiv="+TargetDiv, "ContentWindow","toolbar=no,width=800,height=600,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");
	break;
	case "AboutUs" :
	ContentWindow = window.open("AboutUs.m", "ContentWindow","toolbar=no,width=800,height=600,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");
	break;
	case "ContactUs" :
	ContentWindow = window.open("ContentContactUs.m", "ContentWindow","toolbar=no,width=800,height=600,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");
	break;
	case "LearnMore" :
	ContentWindow = window.open("http://www.seamlessweb.com/public/default.html", "ContentWindow","toolbar=no,width=800,height=600,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");
	break;
	case "EatAtWork" :
	ContentWindow = window.open("../public/solution.html", "ContentWindow","toolbar=no,width=800,height=600,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");
	break;
	case "SignUp" :
	ContentWindow = window.open("../public/memberSignupForm.html", "ContentWindow","toolbar=no,width=800,height=600,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");
    };
}
function LaunchQuickTools(SelectedTool,AddedInfo,VendorName,WhichStep) {
    switch(SelectedTool) {
	case "TellAFriend" :
	QuickToolsWindow = window.open("TellAFriend.m", "QuickToolsWindow","toolbar=no,width=800,height=600,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");
	break;
	case "SuggestARestaurant" :
	AddedInfo = escape(AddedInfo);
	VendorName = escape(VendorName);
	QuickToolsWindow = window.open("SuggestARestaurant.m?SuggestionComments=" + AddedInfo + "&SuggestedRestaurant=" + VendorName, "QuickToolsWindow","toolbar=no,width=800,height=600,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");
	break;

	case "MenuSuggestion" :
	    AddedInfo = escape(AddedInfo);
	    VendorName = escape(VendorName);
	    WhichStep = escape(WhichStep);
	    QuickToolsWindow = window.open("SuggestAMenuItem.m?SuggestionComments=" + AddedInfo + "&SuggestedRestaurant=" + VendorName + "&WhichStep=" + WhichStep, "QuickToolsWindow","toolbar=no,width=800,height=600,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");
	    break;    

	case "EmailUpdates" :
	QuickToolsWindow = window.open("EmailUpdates.m", "QuickToolsWindow","toolbar=no,width=800,height=600,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");
	break;
	case "Affiliate" :
	QuickToolsWindow = window.open("Affiliate.m", "QuickToolsWindow","toolbar=no,width=800,height=600,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes");
	break;
    }
}
function HighlightNavTab(div) {
    document.getElementById(div.id).style.backgroundColor = "#FEF6DC";
}  
function UnhighlightNavTab(div) {
    document.getElementById(div.id).style.backgroundColor = "#C8C6A6";
}
function swapTabToActive(div) {
    document.getElementById(div.id).style.backgroundImage = 'url(../images/Consumer/navigation_active_tab.gif)';
}
function swapTabToInactive(div) {
    document.getElementById(div.id).style.backgroundImage = 'url(../images/Consumer/navigation_inactive_tab.gif)';
}












