/**
 * Custom JS Functions
 */
 
// ** global variables ** //

/**
 * Create a shortcut to jQuery, calling no conflict due to our custom $ function
 * {@link http://docs.jquery.com/Using_jQuery_with_Other_Libraries}
 */
var $j = jQuery.noConflict();

/**
 * Function used to toggle our product browse/attribute search menu
 * @param {string} mode
 * @param {boolean} save Optional
 */
function toggle_menu(mode, save) {
	if (!save) {var save = false;}

	switch (mode) {
		case 'hide':
			if ($('side-pane')) {$('side-pane').style.display = 'none';}
			if ($('hide')) {$('hide').style.display = "none";}
			if ($('show')) {$('show').style.display = "";}	
			if ($('content')) {$('content').style.width = "980px";}
			break;
		case 'show':
			if ($('side-pane')) {$('side-pane').style.display = 'none';}
			if ($('hide')) {$('hide').style.display = "";}
			if ($('show')) {$('show').style.display = "none";}
			if ($('content')) {$('content').style.width = "980px";}
			break;			
	}

	//update our cookie if applicable
	if (save) {
		save_product_browse_cookie(mode);
	}
}

/**
 * Function used to validate form - uses our own custom attributes for determinig required fields, types to validate, etc.
 * @param {object} the_form
 * @return {boolean}
 */
function validate_form(the_form) {
	the_form = $(the_form);

	//default, all fields are required
	for (i = 0; i < the_form.length; i++) {
		if (the_form[i].id != '' && the_form[i].type != 'hidden') {
			if (the_form[i].type == 'select-one') {
				if (the_form[i].getAttribute('required') == 'required' && !the_form[i].selectedIndex > 0) {
					alert(($('lbl_' + the_form[i].id) ? $('lbl_' + the_form[i].id).innerHTML : the_form[i].id) + ' is required.');
					the_form[i].focus();
					return false;
				}
			} else if (the_form[i].type == 'radio' || the_form[i].type == 'checkbox' && the_form[i].getAttribute('required') == 'required') {
				//we need to ensure that at least on of our radio options is checked
				var radio_group = the_form[i].name;
				var checked = false;
				var inputs = the_form.getElementsByTagName('input');
				for (var i = 0, length = inputs.length; i < length; i++) {
					if (inputs[i].checked) {
						checked = true;
					}
				}
	
				if (!checked) {
					alert(($('lbl_' + radio_group) ? $('lbl_' + radio_group).innerHTML : radio_group) + ' is required.');
					return false;
				}
			} else {
				if (the_form[i].getAttribute('required') == 'required') {
					switch (the_form[i].getAttribute('datatype')) {
						case 'integer':
								if (trim(the_form[i].value) == '' || !is_integer(the_form[i].value)) {
										alert(($('lbl_' + the_form[i].id) ? $('lbl_' + the_form[i].id).innerHTML : the_form[i].id) + ' must be an integer value.');
										the_form[i].focus();
										return false;
								}
								break;
						case 'float':
								if (trim(the_form[i].value) == '' || !is_float(the_form[i].value)) {
										alert(($('lbl_' + the_form[i].id) ? $('lbl_' + the_form[i].id).innerHTML : the_form[i].id) + ' must be a float value.');
										the_form[i].focus();
										return false;
								}
								break;
						case 'currency':
								if (trim(the_form[i].value) == '' || !is_currency(the_form[i].value)) {
										alert(($('lbl_' + the_form[i].id) ? $('lbl_' + the_form[i].id).innerHTML : the_form[i].id) + ' must be a currency value.');
										the_form[i].focus();
										return false;
								}
								break;
						case 'email':
								if (trim(the_form[i].value) == '' || !is_email(the_form[i].value)) {
										alert('Please enter a valid email address for ' + ($('lbl_' + the_form[i].id) ? $('lbl_' + the_form[i].id).innerHTML : the_form[i].id) + '.');
										the_form[i].focus();
										return false;
								}
								break;
						case 'date':
								if (trim(the_form[i].value) == '' || !is_date(the_form[i].value)) {
										alert('Please enter a valid date for ' + ($('lbl_' + the_form[i].id) ? $('lbl_' + the_form[i].id).innerHTML : the_form[i].id) + ' in the format (mm/dd/yyyy).');
										the_form[i].focus();
										return false;
								}
								break;
						case 'zip_code':
								if (trim(the_form[i].value) == '' || !is_postalcode(the_form[i].value)) {
										alert('Please enter a valid postal code for ' + ($('lbl_' + the_form[i].id) ? $('lbl_' + the_form[i].id).innerHTML : the_form[i].id) + '.');
										the_form[i].focus();
										return false;
								}
								break;
						case 'phone':
								if (trim(the_form[i].value) == '' || !is_phone(the_form[i].value)) {
										alert('Please enter a valid phone number for ' + ($('lbl_' + the_form[i].id) ? $('lbl_' + the_form[i].id).innerHTML : the_form[i].id) + '.');
										the_form[i].focus();
										return false;
								}
								break;
						case 'string':
						default:
								if (trim(the_form[i].value) == '' || !is_string(the_form[i].value)) {
										alert(($('lbl_' + the_form[i].id) ? $('lbl_' + the_form[i].id).innerHTML : the_form[i].id) + ' is required.');
										the_form[i].focus();
										return false;
								}
								break;
					}//switch (the_form[i].getAttribute('datatype'))
				}//if (the_form[i].getAttribute('required') == 'required')
			}//if (the_form[i].type == 'select-one')
		}//if (the_form[i].id != '' && the_form[i].type != 'hidden')
	}//for (i = 0; i < the_form.length; i++)

	return true;
}

/**
 * JavaScript Dropdown Menu with Multi Levels
 * Original code taken from {@link http://www.leigeber.com/2008/11/drop-down-menu/}
 */
var menu=function(){
	var t=15,z=50,s=6,a;
	function dd(n){this.n=n; this.h=[]; this.c=[]}
	dd.prototype.init=function(p,c){
		a=c; var w=document.getElementById(p), s=w.getElementsByTagName('ul'), l=s.length, i=0;
		for(i;i<l;i++){
			var h=s[i].parentNode; this.h[i]=h; this.c[i]=s[i];
			h.onmouseover=new Function(this.n+'.st('+i+',true)');
			h.onmouseout=new Function(this.n+'.st('+i+')');
		}
	}
	dd.prototype.st=function(x,f){
		var c=this.c[x], h=this.h[x], p=h.getElementsByTagName('a')[0];
		clearInterval(c.t); c.style.overflow='hidden';
		if(f){
			p.className+=' '+a;
			if(!c.mh){c.style.display='block'; c.style.height=''; c.mh=c.offsetHeight; c.style.height=0}
			if(c.mh==c.offsetHeight){c.style.overflow='visible'}
			else{c.style.zIndex=z; z++; c.t=setInterval(function(){sl(c,1)},t)}
		}else{p.className=p.className.replace(a,''); c.t=setInterval(function(){sl(c,-1)},t)}
	}
	function sl(c,f){
		var h=c.offsetHeight;
		if((h<=0&&f!=1)||(h>=c.mh&&f==1)){
			if(f==1){c.style.filter=''; c.style.opacity=1; c.style.overflow='visible'}
			clearInterval(c.t); return
		}
		var d=(f==1)?Math.ceil((c.mh-h)/s):Math.ceil(h/s), o=h/c.mh;
		c.style.opacity=o; c.style.filter='alpha(opacity='+(o*100)+')';
		c.style.height=h+(d*f)+'px'
	}
	return{dd:dd}
}();
