// JavaScript Document
var req = false;

var submit_count = 0;
	function submit_once() {
		if( submit_count == 0) {
			submit_count++;
			return true;
		}
		return false;
	}

var startList = function() {
	if (document.all&&document.getElementById) {
		function menulist(navRoot) {
			var i, node;
			if ( typeof ( navRoot ) != 'undefined' ) {
				for (i=0; i<navRoot.childNodes.length; i++) {
					node = navRoot.childNodes[i];
					if (node.nodeName=="UL") {
						menulist(node);
					}
					if (node.nodeName=="LI") {
						menulist(node);
						node.onmouseover=function() {
							for (var j=0; j<this.childNodes.length; j++) {
								inode = this.childNodes[j];
								if ( inode.nodeName=="UL" || inode.nodeName=="H2" ) { inode.className += " over"; }
							}
						}
						node.onmouseout=function() {
							for (var j=0; j<this.childNodes.length; j++) {
								inode = this.childNodes[j];
								if ( inode.nodeName=="UL" || inode.nodeName=="H2" ) { inode.className=this.className.replace(" over", ""); }
							}
						}
					}
				}
			}
		}
		var navRoot = document.getElementById('main_nav');
		if(navRoot){
			menulist(navRoot);
		}
	}
}

var _mf_onload_handlers = [];
function add_onload(func) {
    _mf_onload_handlers[_mf_onload_handlers.length] = func;
}
window.onload = function() {
    for ( var i = 0; i < _mf_onload_handlers.length; i++ ) {
        var func = _mf_onload_handlers[i];
        if ( typeof( func ) == 'function' ) {
            func();
        } else if ( typeof( func ) == 'string' ) {
            eval( func );
        }
    }
}
window.add_onload(startList);

//  OPEN POPUP WINDOW
var popupList = {};
function openWindow( winName, url, w, h, scroll ) {

	//  Close other instances of this window.
	if ( popupList[winName] != null && typeof( popupList[winName] ) == "object" && !popupList[winName].closed ) {
		popupList[winName].close();
		popupList[winName] = null;
	}

	//  Setup the new window.
	popupList[winName] = window.open( url, winName, "height=" + h + ",width=" + w + ",channelmode=0,dependent=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=0,scrollbars=" + scroll + ",status=1,toolbar=0","pop" );

	//  Open the window.
	if ( popupList[winName].opener == null ) {
		popupList[winName].opener = window;
	}
	if ( navigator.appName == 'Netscape' ) {
		popupList[winName].focus;
	}
}

function show_shipping_details() {
	openWindow( 'Flavors', '/gifts/shipping/details.mfc', 400, 550, '1' );
}

//  DIV TOGGLER
function togleDiv( id, type, force ) {
	var ele = document.getElementById( id );

	//  Is this a home page offer?
	//  If so, hide all other home page offers.
	if ( typeof ( var_name ) != 'undefined' ) {
		if( id.substr( 0, 11 ) == "home_offer_" ) {
			var check;
			for( i = 0; i < home_page_offers.length; i++ ) {
				if( "home_offer_" + home_page_offers[i] != id ) {
					check = document.getElementById( "home_offer_" + home_page_offers[i] );
					if( check ) check.style.display = "none";
				}
			}
		}
	}

	if( force != "" ) {
		ele.style.display = force;
	} else {
		if( ele.style.display == "none" || ele.style.display == "" ) ele.style.display = type;
		else ele.style.display = "none";
	}
}

function submitenter(myfield,e) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13)
	{
		myfield.form.submit();
		return false;
	}
	else
		return true;
}

var site_searching = false;
var site_sub_searching = false;
function siteSearch() {
	if( site_sub_searching == true ) {
		var fld = document.getElementById( 'search_field' );
	} else {
		var fld = document.getElementById( 'search_box' );
	}
	if( fld && fld.value != "" && site_searching == false ) {
		site_searching = true;
		return true;
	}
	return false;
}

function AW_BustFrames() {
	if( self.parent && self.parent.frames ) {
		if (self.parent.frames.length!=0) {
			self.parent.location.replace(document.location.href);
		}
	}
}

function any_event( name, params ) {
	if( name != '') {
		var img = new Image(1,1);
		img.src = "/et/"+ name +"?"+ params;
		return img.height;
	}
}

function addHrefParam( url, name, value ) {

	var query_str = "";
	if( name && value ) {
		if( url.indexOf( "?" ) > 0 ) {
			query_str = "&" + name + "=" + value;
		} else {
			query_str = "?" + name + "=" + value;
		}
	}

	url = url + query_str;
	window.location = url;
	return false;
}

function toggle_prod_image(id) {

	var ele = document.getElementById( id );
	// array of elements that should have their display be the opposite of the large prod image. (select box peeks through in IE)
	var oppisite_elements = new Array('type0_flavors', 'type1_flavors', 'type2_flavors','sent_selection');

	// hide large image ( show other elements)
	if ( typeof ( ele ) != 'undefined' ) {
		if( ele.style.display == 'block' ) {

			ele.style.display = 'none';
			if( oppisite_elements.length > 0 ) {
				var i = 0;
				while( i < oppisite_elements.length ) {
					ele = document.getElementById( oppisite_elements[i] );
					if( ele ) {
						ele.style.display = 'block';
					}
					i++;
				}
			}

		// show large image ( hide other elemetns)
		} else {

			ele.style.display = 'block';
			if( oppisite_elements.length > 0 ) {
				var i = 0;
				while( i < oppisite_elements.length ) {
					ele = document.getElementById( oppisite_elements[i] );
					if( ele ) {
						ele.style.display = 'none';
					}
					i++;
				}
			}
		}

	}
}

// COMMON XML HTTPS REQUEST STUFF
function appendToSelect(select, value, content) {
	var opt;
	opt = document.createElement("option");
	opt.value = value;
	opt.appendChild(content);
	select.appendChild(opt);
}

function perform_xml_request(url, data, callback){
	req = create_XMLHttpRequest_object()
	if(!req) {
		return;
	}
	req.onreadystatechange = callback;
	req.open("GET", url, true);
	req.setRequestHeader ('Content-Type','application/x-www-form-urlencoded');
	req.send("");

}

function create_XMLHttpRequest_object(){

	req = false;
	try {
		req = new XMLHttpRequest();
	} catch(e) {
		try{
			//req = new ActiveXObject("Microsoft.XMLHTTP");
			req = new ActiveXObject("MSXML2.XMLHTTP.3.0");
		} catch(e) {
			req = false;
		}
	}
	return req;
}

function check_req_status(){
	// must have readyState of 4 and status of 200
	if(req.readyState == 4 && req.status == 200){
		return true;
	}
	return false;
}

//make Array.indexOf work in IE
if (!Array.indexOf) {
	Array.prototype.indexOf = function(obj) {
			for (var i = 0; i < this.length; i++) {
				if (this[i] == obj) {
					return i;
				}
			}
			return -1;	//not found
		}
}

//  GET ELEMENTS OF A SPECIFIC CLASS
function getElementsByStyleClass( className ) {
	var all = document.all ? document.all :
		document.getElementsByTagName('*');
	var elements = new Array();
	for (var e = 0; e < all.length; e++)
		if (all[e].className == className)
	elements[elements.length] = all[e];
	return elements;
}

function track_site_event( async, event_idx, value ) {

	var url = "/track/gif.mfc?id=" + parseInt( event_idx ) + "&value=" + escape( value );

	if( async || async.length > 0 ) {
		async = true;
	} else {
		async = false;
	}

	var html = $.ajax({
		url: url,
		async: async
	}).responseText;

	if( html ) {
		return true;
	}

	return false;
}
