var lastMenu = -1;

function toggleMenu(menuID) {
	if (lastMenu != menuID) {
		if ($('leftNavMenu'+lastMenu)) {
			new Effect.Parallel(
				[ new Effect.BlindUp('leftNavMenu'+lastMenu, {sync: true}),
				  new Effect.BlindDown('leftNavMenu'+menuID, {sync: true})], {duration: 1, queue:'end'});
		} else {
			new Effect.BlindDown('leftNavMenu'+menuID);
		}
		lastMenu = menuID;
	} else {
		new Effect.BlindUp('leftNavMenu'+menuID);
		lastMenu = -1;
	}
}

var clockID;

function updateClock() {
	if(clockID)
		clearTimeout(clockID);

	var tDate = new Date();
	$('frmClockContainer').innerHTML = 'CRMO Time: ' + tDate.getHours() + ":"
	if (tDate.getMinutes()<10)
		$('frmClockContainer').innerHTML += '0'+tDate.getMinutes()+'';
	else
		$('frmClockContainer').innerHTML += tDate.getMinutes()+'';
	//if (tDate.getSeconds()<10)
	//	$('frmClockContainer').innerHTML += '0'+tDate.getSeconds();
	//else
	//	$('frmClockContainer').innerHTML += tDate.getSeconds();
   
	clockID = setTimeout("updateClock()", 60000);
}

function popMenu(parentID, catID) {
	if(parentID != 0)
	{	

		// $('debug').innerHTML = catID + ' ' + parentID;
		if(parentID == 9999)
		{
			toggleMenu(catID);
			$('cat9999' + catID).className = 'activeLink';
		}
		else
		{
			$('leftNavMenu' + parentID).style.display = '';
			lastMenu = parentID;
			$('cat' + catID).className = 'activeLink';
		}
	}
	else
	{
		$(('menucat' + catID)).className = 'activecat';
	}

}

// email checker
function isEmailFormatCorrect(strAddr) { 
	var ptn = /^[\w\d._%-]+@[\w\d._%-]+\.[\w\d._%-]{2,4}$/;
	return ptn.test(strAddr);
}

function trim(sVal) {
	while(sVal.charAt(sVal.length-1)==' ')
		sVal=sVal.substring(0,sVal.length-1);

	while(sVal.charAt(0)==' ')
		sVal=sVal.substr(1,sVal.length);

	return sVal;
}

// generic error message display
function showError(fld, msg) {
	fld.focus();
	alert(msg);
	return false;
}

// removes spaces from a credit card number
 function removeSpaces(s)
{
	var new_s = '';
	for(var i = 0; i < s.length; i++)
	{
		if(s.charAt(i) != ' ')
			new_s += s.charAt(i);
	}
	return new_s;
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

// pops a new dependant window with chrome removed
function popwin(surl) {
	window.open(surl, 'newwin', 'dependent=yes,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no'); 
	return false;	
}


function EvalSound(soundobject)	{
	var thisSound = document.getElementById(soundobject);
	thisSound.Play();
}

function checkSearchForm()	{
	if ($F('frmProduct') == '0' && $F('frmBrand') == '0' && (trim($F('frmSearchString')) == '' || $F('frmSearchString') == 'Enter search word(s)'))	{
		return showError($('frmSearchString'), 'Please select at least one search criteria');
	}
	$('frmSearch').submit();
}


function popBrands() {

	var successGetBrands = function(q) {
		var optBrandID
		var optBrandName
		if (q.responseXML.documentElement.getElementsByTagName("status").item(0).firstChild.data == 'ok') {
			optBrands = q.responseXML.documentElement.getElementsByTagName("brand");
			//alert(optBrands.length);
			$('frmBrand').length = 0;
			$('frmBrand').options[0] = new Option('-- All Brands --', '0');
			for (var i = 0;i < optBrands.length; i++ )	{
				optBrandID = optBrands[i].getElementsByTagName('brand_id')[0].firstChild.nodeValue;
				//alert(optBrandID);
				optBrandName = optBrands[i].getElementsByTagName('brand_name')[0].firstChild.nodeValue;
				//alert(optBrandName);
				$('frmBrand').options[i+1] = new Option(optBrandName, optBrandID);
			}
			return true;
		} 
		else {
			alert(q.responseText);
			return false;
		}
	}

	var failureGetBrands = function(q) {
		alert(q.responseText);
		return false;
	}

	var a = {
		seFunc: 'getBrands',
		frmProduct: $F('frmProduct')
	};

	var params = $H(a);
	new Ajax.Request('_ajax_general.asp', {parameters: params.toQueryString(), onSuccess:successGetBrands, onFailure:failureGetBrands});
}

function reset_search_form()
{
	var obj;
	obj = document.getElementById('frmProduct');
	obj.options.selectedIndex = 0;
	obj = document.getElementById('frmBrand');
	obj.options.selectedIndex = 0;
	obj = document.getElementById('frmSearchString');
	obj.value = 'Enter search word(s)';
}
