function toggleDepartmentDisabled() {
	var selectType = document.getElementById("type");
	var selectBoxValue = selectType.value;
	var selectDepartment = document.getElementById("yselectcategory");
	if (selectBoxValue == "M") {
		selectDepartment.disabled = true;
	} else {
		selectDepartment.disabled = false;
	}
}

//function to show hints 
function showHint(c,divName,divNumber) {	
	var targetDiv = divName + divNumber;	
	var el = document.getElementById(targetDiv);	
	var t = c.offsetTop;
	var l = c.offsetLeft;	
	el.style.display = "block";
	el.style.top = (t-45) + "px";
	el.style.left = (l-4) + "px";
}

function hideHint(divName,divNumber) {	
	var targetDiv = divName + divNumber;
	document.getElementById(targetDiv).style.display = "none";
}

// regex for validating monetary values (control panel)
function IsPrice(thevalue) {
	var number = /^\d+$|^\d+\.\d{1,2}$/;	
	return number.exec(thevalue);
}

// regex for validating email address
function IsEmail(thevalue) {
	var email = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;		
	return email.exec(thevalue);
}

// form vaildation for payment request amount (control panel)
function checkAmountPayable() {	
	var amountRequested = document.getElementById("amountrequested").value;
	var amountAvailable = document.getElementById("UserBalance").value;
	var email = document.getElementById("paypalemail").value;
	var confirmEmail = document.getElementById("paypalemailconfirm").value;
	if (!IsPrice(amountRequested) || amountRequested > amountAvailable || amountRequested == "" || amountRequested < 20) {
		alert("You must enter a valid dollar value less than or equal to the payable amount and no less than $20.00");	
		document.getElementById("amountrequested").focus();		
		document.getElementById("amountrequested").style.border = "1px solid #990000";
		return false;
	}
	if (!IsEmail(email) || email == "") {
		alert("You must enter a valid email address.");	
		document.getElementById("paypalemail").focus();		
		document.getElementById("paypalemail").style.border = "1px solid #990000";
		return false;
	}
	if (email != confirmEmail) {
		alert("Your email adresses do not match.");	
		document.getElementById("paypalemailconfirm").focus();		
		document.getElementById("paypalemailconfirm").style.border = "1px solid #990000";
		return false;
	}
	return true;
}

// resets field border on blur (control panel)
function clearFieldBorder(elementId) {
	document.getElementById(elementId).style.border = "1px solid #d7d7d7";
}

// compares the to and from dates to validate that they are sequetial (control panel)
function checkDate() {
	var fromDate = new Date(document.getElementById("calendarFrom").value);
	var toDate = new Date(document.getElementById("calendarTo").value);
	if (fromDate > toDate) {
		alert("The 'Start Date' must occur before the 'End Date'");
		return false;
	} else {
		return true;
	}
}  

// counts the length of the content and displays the count and review status to user (/add-merchant-review, /add-product-review)
function cbuTextareaCount() {
	var characterLength = document.getElementById("textarea").value.length;
	document.getElementById("character-count").value = characterLength + ".";	
	if (characterLength >= 1000) {
		document.getElementById("textarea-status").innerHTML = "Extended Review";
		document.getElementById("textarea-status").style.color = "#89A01D";
	} else {
		document.getElementById("textarea-status").innerHTML = "Quick Review";
		document.getElementById("textarea-status").style.color = "#990000";
	}
}

function cbuTextareaCountMedia() {
	var characterLength = document.getElementById("textarea").value.length;
	document.getElementById("character-count").value = characterLength + ".";	
}

// counts the length of the content and displays the count and review status to user (/add-merchant-review, /add-product-review)
function cbuCommentsCount() {
	var characterLength = document.getElementById("commentText").value.length;
	var charactersRemaining = 500 - characterLength;
	var textareaContent = document.getElementById("commentText").value;	
	document.getElementById("character-count").value = charactersRemaining;	
	if (charactersRemaining <= 0) {		
		document.getElementById("commentText").value = textareaContent.substring(0, 499);
	}
}

//creates topnav "more" flyout menu (*)
(new Image()).src="http://media.comparedby.us/pub/comparedbyus/i/nav-more-hover.gif";

function $(x) {
	return document.getElementById(x);
}

function displayMoreMenu(){
	try{clearTimeout(MORE_MENU_TIMEOUT);}catch(err){}
	$("nav-more").src = "http://media.comparedby.us/pub/comparedbyus/i/nav-more-hover.gif";
	$("topnav-more-dropdown").style.display = "block";		
}

function hideMoreMenu(){
	MORE_MENU_TIMEOUT = setTimeout(undisplayMoreMenu,200);
}

function undisplayMoreMenu(){
	$("topnav-more-dropdown").style.display = "none";
	$("nav-more").src = "http://media.comparedby.us/pub/comparedbyus/i/nav-more.gif";		
}    

//ppc whats this popup (*)
function wtpopup(){
	wtpopupwin = window.open('/wt', 'wtpopup', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=400,height=250');
}

//sets the approrpriate topnav button to on state per section (*)
function topNavInit() {	
	var whichSection = getUrlSubString();
	switch (whichSection) {
		case "auto":
			document.getElementById("nav-Auto").src = "http://media.comparedby.us/pub/comparedbyus/i/nav-Auto-on.gif"
			break;
		case "baby":
			document.getElementById("nav-Baby").src = "http://media.comparedby.us/pub/comparedbyus/i/nav-Baby-on.gif"
			break;
		case "clothing":
			document.getElementById("nav-Clothing").src = "http://media.comparedby.us/pub/comparedbyus/i/nav-Clothing-on.gif"
			break;
		case "computers":
			document.getElementById("nav-Computers").src = "http://media.comparedby.us/pub/comparedbyus/i/nav-Computers-on.gif"
			break;
		case "electronics":
			document.getElementById("nav-Electronics").src = "http://media.comparedby.us/pub/comparedbyus/i/nav-Electronics-on.gif"
			break;
		case "gifts":
			document.getElementById("nav-Gifts").src = "http://media.comparedby.us/pub/comparedbyus/i/nav-Gifts-on.gif"
			break;
		case "home-and-garden":
			document.getElementById("nav-Home-and-Garden").src = "http://media.comparedby.us/pub/comparedbyus/i/nav-Home-and-Garden-on.gif"
			break;
		case "jewelry":
			document.getElementById("nav-Jewelry").src = "http://media.comparedby.us/pub/comparedbyus/i/nav-Jewelry-on.gif"
			break;
		case "shoes":
			document.getElementById("nav-Shoes").src = "http://media.comparedby.us/pub/comparedbyus/i/nav-Shoes-on.gif"
			break;
		case "sporting-goods":
			document.getElementById("nav-Sporting-Goods").src = "http://media.comparedby.us/pub/comparedbyus/i/nav-Sporting-Goods-on.gif"
			break;
		case "toys":
			document.getElementById("nav-Toys").src = "http://media.comparedby.us/pub/comparedbyus/i/nav-Toys-on.gif"
			break;			
	}
}

//topnav rollover function (*)
function topNavRollOver (whichNav,imageFileType) {	
	var rollOverSuffix = "-hover";
	var whichNav = whichNav.replace(/^\s+|\s+$/g,'');
	var imageFileType = imageFileType.replace(/^\s+|\s+$/g,'');
	var imagePath = "http://media.comparedby.us/pub/comparedbyus/i/";
	var navRoot = document.getElementById(whichNav);				
	var u = getUrlSubString();
	var i = u.indexOf(".");
	var c = "";				
	if (i != 0) {
		u = u.split(".");
		c = u[0];
	} else {
		c = u;
	}
	for(i=0; i<navRoot.childNodes.length; i++){
		node0 = navRoot.childNodes[i];
		if(node0.nodeName=='A'){
			for(a=0; a<node0.childNodes.length; a++){
				node1=node0.childNodes[a];
				if(node1.nodeName=="IMG"){
					node1.onmouseover=function(){
						if (
							(this.id=="nav-Auto" && c=="auto")||
							(this.id=="nav-Baby" && c=="baby")||
							(this.id=="nav-Clothing" && c=="clothing")||
							(this.id=="nav-Computers" && c=="computers")||
							(this.id=="nav-Electronics" && c=="electronics")||
							(this.id=="nav-Gifts" && c=="gifts")||
							(this.id=="nav-Home-and-Garden" && c=="home-and-garden")||
							(this.id=="nav-Jewelry" && c=="jewelry")||
							(this.id=="nav-Shoes" && c=="shoes")||
							(this.id=="nav-Sporting-Goods" && c=="sporting-goods")||
							(this.id=="nav-Toys" && c=="toys")
							)
						{
							srcactive = imagePath+this.id+"-on"+imageFileType;
						} else {
							srcactive = imagePath+this.id+rollOverSuffix+imageFileType;
						}						
						this.src = srcactive;						
					}
					node1.onmouseout=function(){						
						if (
							(this.id=="nav-Auto" && c=="auto")||
							(this.id=="nav-Baby" && c=="baby")||
							(this.id=="nav-Clothing" && c=="clothing")||
							(this.id=="nav-Computers" && c=="computers")||
							(this.id=="nav-Electronics" && c=="electronics")||
							(this.id=="nav-Gifts" && c=="gifts")||
							(this.id=="nav-Home-and-Garden" && c=="home-and-garden")||
							(this.id=="nav-Jewelry" && c=="jewelry")||
							(this.id=="nav-Shoes" && c=="shoes")||
							(this.id=="nav-Sporting-Goods" && c=="sporting-goods")||
							(this.id=="nav-Toys" && c=="toys")
							)
						{
							src = imagePath+this.id+"-on"+imageFileType;
						} else {
							src = imagePath+this.id+imageFileType;	
						}						
						this.src = src;
					}
				}
			}
		}
	}
}


//reads page url and splits into an array (*)
function getUrlSubString() {
	var url = document.URL;
	var whichPageArray=url.split("/");	
	return whichPageArray[3];	
}

//changes the form action on the search box as per section selected (*)
function searchChangeAction() {			
	var url;
	if( document.searchboxform.yselectcategory.value ) {
		url = '/' + document.searchboxform.yselectcategory.value;
	} else {
		url = '/Default.pl';
	}
	document.searchboxform.yselectcategory.disabled=true;
	if(document.searchboxform.ysquery.value == "")document.searchboxform.ysquery.disabled=true;
	document.searchboxform.action = url;
}

//changes the form action on the search box as per section selected (*)
function changeAction() 
{			
	var url;
	if (document.searchboxform.type.value == 'M') 
	{
	   if (document.searchboxform.ysquery.value == "") 
	   {
		  url = '/' + 'all.ml';
	   } 
	   else
	   {
	      url = '/' + 'search.ml';
	   }
	} 
	else 
	{
	    //  alert("yselectcategory ="+ document.searchboxform.yselectcategory.value);
	    if (document.searchboxform.yselectcategory.value) 
	    {
	         url = '/' + document.searchboxform.yselectcategory.value;
	    } 
	    else 
	    {

	        if(document.URL.indexOf('.gl') > -1)
	        {
	            url = '/Default.gl';
	        }
	        else
	        {
	            url = '/Default.pl';
	        } 
	        document.searchboxform.yselectcategory.disabled=true;
	    }
	}   

	if(document.searchboxform.ysquery.value == "")document.searchboxform.ysquery.disabled=true;

  //alert("url ="+ url);
	document.searchboxform.action = url;
}


////changes the form action on the search box as per section selected (*)
//function changeAction() {			
//	var url;
//	if (document.searchboxform.type.value == 'M') {
//	   if (document.searchboxform.ysquery.value == "") {
//		  url = '/' + 'all.ml';
//	   } else {
//	      url = '/' + 'search.ml';
//	   }
//	} else {
//	   if (document.searchboxform.yselectcategory.value ) {
//	      url = '/' + document.searchboxform.yselectcategory.value;
//	   } else {
//	      url = '/Default.pl';
//	   }
//	}   
//	document.searchboxform.yselectcategory.disabled=true;
//	if(document.searchboxform.ysquery.value == "")document.searchboxform.ysquery.disabled=true;
//	document.searchboxform.action = url;
//}

//changes the form action on the lander search box as per section selected (/)
function landerChangeAction() {   
	var url;
	url = '/Lander.aspx';	
	//document.searchboxform.yselectcategory.disabled=true;
	if(document.searchboxform.ysquery.value == "")document.searchboxform.ysquery.disabled=true;
	document.searchboxform.action = url;
}

//changes the form action on the lander search box as per section selected (/)
function rmxChangeAction() {
    var url;
    url = '/default.rm';
    //document.searchboxform.yselectcategory.disabled=true;
    if (document.searchboxform.ysquery.value == "") document.searchboxform.ysquery.disabled = true;
    document.searchboxform.action = url;
}

//general image rollover function (*)
function navRollOver (whichNav,imageFileType) {	
	var rollOverSuffix = "-hover";
	var whichNav = whichNav.replace(/^\s+|\s+$/g,'');
	var imageFileType = imageFileType.replace(/^\s+|\s+$/g,'');
	var imagePath = "http://media.comparedby.us/pub/comparedbyus/i/";
	var navRoot = document.getElementById(whichNav);
	for(i=0; i<navRoot.childNodes.length; i++){
		node0 = navRoot.childNodes[i];
		if(node0.nodeName=='A'){
			for(a=0; a<node0.childNodes.length; a++){
				node1=node0.childNodes[a];
				if(node1.nodeName=="IMG"){
					node1.onmouseover=function(){
						srcactive = imagePath+this.id+rollOverSuffix+imageFileType;						
						this.src = srcactive;						
					}
					node1.onmouseout=function(){
						src = imagePath+this.id+imageFileType;
						this.src = src;
					}
				}
			}
		}
	}
}

//general form input rollover function (*)
function formRollOver (buttonId,imageFileType,direction) {	
	var buttonId = buttonId;
	var imageFileType = imageFileType.replace(/^\s+|\s+$/g,'');
	var buttonSrc = "";
	var direction = direction;
	if (direction == "in") {
		buttonSrc = "http://media.comparedby.us/pub/comparedbyus/i/"+buttonId+"-hover"+imageFileType;
	} else {
		buttonSrc = "http://media.comparedby.us/pub/comparedbyus/i/"+buttonId+imageFileType;
	}
	document.getElementById(buttonId).src=buttonSrc;
}

//mootols init function for the community header and its tab (*)
function mootoolsCommunityHeaderInit() {	
	var memberLoginSlide = new Fx.Slide('member-login', {duration: 400});	
	var memberLoginAlpha = new Fx.Style('member-login-form','opacity', {duration: 250});
	var newMemberSlide = new Fx.Slide('new-member', {duration: 400});
	var newMemberAlpha = new Fx.Style('new-member-form','opacity', {duration: 250});
	var whichOut=0; //closed=0 login=1 newmember=2
	$('community-header-login').addEvent('click', function(){					
		document.getElementById("community-header-login").disabled = true;
		document.getElementById("community-header-newmembers").disabled = true;
		if (whichOut == 0) {
			memberLoginSlide.toggle().chain(function(){
				document.getElementById("community-header-login").src = "http://media.comparedby.us/pub/comparedbyus/i/community-header-login-hover.gif";
				memberLoginAlpha.start(0,1).chain(function() {
					document.getElementById("community-header-login").disabled = false;
					document.getElementById("community-header-newmembers").disabled = false;
				});							
			});						
			whichOut=1;
		} else if (whichOut == 1) {
			memberLoginAlpha.start(1,0).chain(function(){
				memberLoginSlide.toggle().chain(function() {
					document.getElementById("community-header-login").src = "http://media.comparedby.us/pub/comparedbyus/i/community-header-login.gif";
					document.getElementById("community-header-login").disabled = false;
					document.getElementById("community-header-newmembers").disabled = false;
				});
			});						
			whichOut=0;
		} else {
			newMemberAlpha.start(1,0).chain(function(){
				newMemberSlide.toggle().chain(function(){
					document.getElementById("community-header-newmembers").src = "http://media.comparedby.us/pub/comparedbyus/i/community-header-newmembers.gif";
					memberLoginSlide.toggle().chain(function(){
						document.getElementById("community-header-login").src = "http://media.comparedby.us/pub/comparedbyus/i/community-header-login-hover.gif";
						memberLoginAlpha.start(0,1).chain(function() {
							document.getElementById("community-header-login").disabled = false;
							document.getElementById("community-header-newmembers").disabled = false;
						})							
					});
				});
			});						
			whichOut=1;
		}
	});
	$('community-header-newmembers').addEvent('click', function(){					
		document.getElementById("community-header-login").disabled = true;
		document.getElementById("community-header-newmembers").disabled = true;
		if (whichOut == 0) {
			newMemberSlide.toggle().chain(function(){
				document.getElementById("community-header-newmembers").src = "http://media.comparedby.us/pub/comparedbyus/i/community-header-newmembers-hover.gif";
				newMemberAlpha.start(0,1).chain(function() {
					document.getElementById("community-header-login").disabled = false;
					document.getElementById("community-header-newmembers").disabled = false;
				})						
			});						
			whichOut=2;
		} else if (whichOut == 1) {
			memberLoginAlpha.start(1,0).chain(function(){
				memberLoginSlide.toggle().chain(function(){
					document.getElementById("community-header-login").src = "http://media.comparedby.us/pub/comparedbyus/i/community-header-login.gif";
					newMemberSlide.toggle().chain(function(){
						document.getElementById("community-header-newmembers").src = "http://media.comparedby.us/pub/comparedbyus/i/community-header-newmembers-hover.gif";
						newMemberAlpha.start(0,1).chain(function() {
							document.getElementById("community-header-login").disabled = false;
							document.getElementById("community-header-newmembers").disabled = false;
						})
					});
				});
			});
			whichOut=2;						
		} else {
			newMemberAlpha.start(1,0).chain(function(){
				newMemberSlide.toggle().chain(function() {
					document.getElementById("community-header-newmembers").src = "http://media.comparedby.us/pub/comparedbyus/i/community-header-newmembers.gif";
					document.getElementById("community-header-login").disabled = false;
					document.getElementById("community-header-newmembers").disabled = false;
				})
			});						
			whichOut=0;
		}
	});
	memberLoginSlide.hide();
	memberLoginAlpha.start(1,0);
	newMemberSlide.hide();
	newMemberAlpha.start(1,0);	
}

//mootools init function for the the product description (.pi,.pr)
function mootoolsProductInformationInit() {
	var descriptionIsOut = false;
	var descriptionSlide = new Fx.Slide('product-info-right-description-togglearea', {duration: 400});
	$('product-info-right-description-togglelink').addEvent('click', function(){
		descriptionSlide.toggle().chain(function(){			
			if (!descriptionIsOut) {				
				var el = document.getElementById('product-info-right-description-togglelink');
				el.removeChild(el.firstChild);
				el.appendChild(document.createTextNode("Hide Full Description"));
				document.getElementById('product-info-right-description-togglelink').style.background = "url(http://media.comparedby.us/pub/comparedbyus/i/product-specs-togglelink-minus.gif) 0 2px no-repeat";
				descriptionIsOut = true;
			} else {
				var el = document.getElementById('product-info-right-description-togglelink');
				el.removeChild(el.firstChild);
				el.appendChild(document.createTextNode("Show Full Description"));
				document.getElementById('product-info-right-description-togglelink').style.background = "url(http://media.comparedby.us/pub/comparedbyus/i/product-specs-togglelink-plus.gif) 0 2px no-repeat";
				descriptionIsOut = false;
			}
		});
	});
	descriptionSlide.hide();	
}

//mootools init function for the the product specifications (.pi,.pr)
function mootoolsProductSpecsInit() {
	var specsIsOut = false;
	var specsSlide = new Fx.Slide('product-specs-togglearea', {duration: 400});	
	$('product-specs-togglelink').addEvent('click', function(){
		specsSlide.toggle().chain(function(){
			if (!specsIsOut) {				
				document.getElementById('product-specs-togglelink').style.background = "url(http://media.comparedby.us/pub/comparedbyus/i/product-specs-togglelink-minus.gif) 0 1px no-repeat";
				specsIsOut = true;
			} else {
				document.getElementById("product-specs-togglelink").style.background = "url(http://media.comparedby.us/pub/comparedbyus/i/product-specs-togglelink-plus.gif) 0 1px no-repeat";
				specsIsOut = false;
			}
		});
	});	
	specsSlide.hide();
}

//mootools init function for the the user mood form in profile peek (*)
function mootoolsMoodFormInit() {	
	var moodIsOut = false;
	var moodSlide = new Fx.Slide('user-mood-form', {duration: 400});	
	$('edit-mood-link').addEvent('click', function(){
		moodSlide.toggle().chain(function() {		
			if (!moodIsOut) {			
				document.getElementById('edit-mood-link').innerHTML = "close";
				moodIsOut = true;
			} else {
				document.getElementById('edit-mood-link').innerHTML = "edit";
				moodIsOut = false;
			}
	   });
	});	
	moodSlide.hide();	
}

var hintVisible = false;
function mootoolsTextareaHintInit(targetDiv) {		
	var targetDiv = targetDiv;
	if (!hintVisible) {
		document.getElementById(targetDiv).style.display = "block";
		hintVisible = true;
	} else {
		document.getElementById(targetDiv).style.display = "none";
		hintVisible = false;
	}
}

//confirmation prompt for message deletion (/messages)
function confirmDelete(delUrl) {
	if (confirm("Are you sure you want to permanently delete this message?")) {
		document.location = delUrl;
	}
}

//swaps the privacy checkbox states on image click (/userprofile)
function checktest(whichCheckBox) {
	var checkBoxId = "checkbox-" + whichCheckBox;
	var iconId = "icon-" + whichCheckBox;
	if(document.getElementById(checkBoxId).checked) {
		document.getElementById(checkBoxId).checked = false;
		document.getElementById(iconId).src = "http://media.comparedby.us/pub/comparedbyus/i/public-icon.png";
	} else {
		document.getElementById(checkBoxId).checked = true;
		document.getElementById(iconId).src = "http://media.comparedby.us/pub/comparedbyus/i/private-icon.png";
	}
}

//looks for querystring to determine redirect path (/login,/logout)
function queryString(parameter) { 
	var loc = location.search.substring(1, location.search.length);
	var param_value = false;
	var params = loc.split("&");
	for (i=0; i<params.length;i++) {
		param_name = params[i].substring(0,params[i].indexOf('='));
		if (param_name == parameter) {
			param_value = params[i].substring(params[i].indexOf('=')+1)
		}
	}
	if (param_value) {
		return param_value;
	}
	else {
		return '/home';
	}
}

//loads popup for upload image page and form (/userprofile)
function uploadImagePopup() {
	imageuploadpopupwin = window.open('/uploadimage', 'uploadimage', 'toolbar=0,scrollbars=0,location=500,statusbar=0,menubar=0,resizable=0,width=470,height=200');
}