function clearOptions (obj){
	while (obj.options.length > 0) {
   		obj.options[0] = null;
	}
}
function categoryChanged(obj){
	if(obj.selectedIndex > 0){
		document.getElementById("categoryId").value = obj.options[obj.selectedIndex].value;
		var level = document.getElementById("level");
		if(obj.id == 'category'){
			level.value = "1";
			executeAction('getChildren');
			document.getElementById("categoryId").value = "";
		}else {
			level.value = "2";
			document.getElementById("categoryId").value = obj.options[obj.selectedIndex].value;
		}
		//executeAction('getChildren');
	}else{
		document.getElementById("categoryId").value = "";
		//remove all child objects
		var sel;
		if(obj.id == 'category'){
			sel = document.getElementById("subCategory");
			clearOptions(sel);
			sel.options.add(new Option("--Select Subcategory--", "-1"));
		}
		
		sel = document.getElementById("subSubCategory"); 
		if(sel){
			clearOptions(sel);
			sel.options.add(new Option("--Select Sub-sub-category--", "-1"));
		}
		
		
	}
}

function startTimer(){
	var v = setTimeout("keepAlive();", 5 * 60 * 1000); //first hit after 5 mins
}

function keepAlive(){
	setInterval("executeAction('keepalive')", 5 * 60 * 1000); //subsequent hits after 5 mins
}

var emptyStarImage = new Image();
var fullStarImage = new Image();

function loadRatingImages()
{
	emptyStarImage.src = "resources/images/star_empty.gif";
	fullStarImage.src = "resources/images/star_full.gif";
}

function renderRatingImages (divId, rating, clickable, hiddenFieldId)
{

	var str = "";
	var imageTag;
	var anchorTag;
	var div;	
	
	div = ActionCommons.getElement(divId);
	div.innerHTML = "";
	for (var i = 1; i <= 5; i++)
	{
	    imageTag = document.createElement("img");
        imageTag.border = "0";
        imageTag.id = divId + "_img_" + i;
	    
	    if(i <= rating)
		{
			imageTag.src = fullStarImage.src;
		}
		else
		{
			imageTag.src = emptyStarImage.src;			
		}

        if(clickable)
		{
            anchorTag = document.createElement("A"); 
            anchorTag.href = "javascript:onRatingStarClicked(" + i + ", '" + divId +  "', '" + hiddenFieldId + "');";
            anchorTag.id = divId + "_a_" + i;
            div.appendChild(anchorTag);
	       	anchorTag.appendChild(imageTag);
	    }
		else
		{
	        div.appendChild(imageTag);
	    }
	}
	if(hiddenFieldId){
	    ActionCommons.getElement(hiddenFieldId).value = rating;
	}
}

function onRatingStarClicked (rating, divId, hiddenFieldId)
{
    var imgTag;
    for (var i = 1;i <= 5; i++)
	{
        imgTag = ActionCommons.getElement(divId + "_img_" + i);
        if(i <= rating)
		{
            imgTag.src = fullStarImage.src;
        }
		else
		{
            imgTag.src = emptyStarImage.src;
        }    
    }
	
    var field = ActionCommons.getElement(hiddenFieldId);
   	field.value = rating;
}

loadRatingImages();

function handler(widget, node){
	if(node){
		node.innerHTML = "";
		var fld = document.getElementById("hdn_" + node.id);
		var clickable = document.getElementById("clickable");
		if(fld){
			//alert(fld.value);
			renderRatingImages(node.id, fld.value, clickable, fld.id);
		}
		 
	}
}

function sliderHandler_all(widget, node)
{
	var min = ActionCommons.getElement("slider_all_minrange");
	var max = ActionCommons.getElement("slider_all_maxrange");
	min = Math.round(min.value);
	min = min - (min % 100);   			
		if (min < 0)
	{
		min = 0;
	}	

	max = Math.round(max.value);
	var remainder = max % 100;
	if(remainder > 0) 
	{
		max = (max - (max % 100)) + 100;
	}

	if(max > 0 && max > min)
	{
    	var span = ActionCommons.getElement("slider_all_minspan");
		span.innerHTML = "$" + min;
    	span = ActionCommons.getElement("slider_all_maxspan");
    	span.innerHTML = "$" + max;
	    
		var slider = new Control.Slider(['slider_all_leftknob','slider_all_rightknob'],'slider_all',{
	        sliderValue:[min, max],
	        restricted:true,
	        range:$R(min,max),
	        onSlide:onSliderSlide_all,
	        onChange:onSliderChange_all}
	  	);
  	}
	else 
	{
  		ActionCommons.hideDiv("slider_all_container", true);
  	}
}
  	
function onSliderSlide_all(v)
{
	$('slider_all_minspan').innerHTML= "$" + Math.round(v[0]);
	$('slider_all_maxspan').innerHTML= "$" + Math.round(v[1]);
}
  	
function onSliderChange_all (v)
{
	ActionCommons.getElement("minPrice_txt_all").value = Math.round(v[0]);
	ActionCommons.getElement("maxPrice_txt_all").value = Math.round(v[1]);
	$('slider_all_minspan').innerHTML="$" + Math.round(v[0]);
	$('slider_all_maxspan').innerHTML="$" + Math.round(v[1]);
	
	if(Math.round(v[1]) > 0)
	{
		var url = ActionCommons.getElement("slider_all_url");
		Controller.executeAction('searchpriceall', 'search_listing_all',url.value);
	}	
}


function sliderHandler_fr(widget, node)
{
	var min = ActionCommons.getElement("slider_fr_minrange");
	var max = ActionCommons.getElement("slider_fr_maxrange");

	min = Math.round(min.value);	
	min = min - (min % 100);
	if (min < 0)
	{
		min = 0;
    }		

	max = Math.round(max.value);
	var remainder = max % 100;
	if(remainder > 0)
	{
		max = (max - (max % 100)) + 100;
	}

	if(max > 0 && max > min)
	{
    	var span = ActionCommons.getElement("slider_fr_minspan");
    	span.innerHTML = "$" + min;
    	span = ActionCommons.getElement("slider_fr_maxspan");
    	span.innerHTML = "$" + max;
	    
		var slider = new Control.Slider(['slider_fr_leftknob','slider_fr_rightknob'],'slider_fr',{
	        sliderValue:[min, max],
	        restricted:true,
	        range:$R(min,max),
	        onSlide:onSliderSlide_fr,
	        onChange:onSliderChange_fr}
	  	);
  	}
	else
	{
  		ActionCommons.hideDiv("slider_fr_container", true);
  	}
}
  	
function onSliderSlide_fr(v)
{
	$('slider_fr_minspan').innerHTML = "$" + Math.round(v[0]);
	$('slider_fr_maxspan').innerHTML = "$" + Math.round(v[1]);
}
  	
function onSliderChange_fr (v)
{
	ActionCommons.getElement("minPrice_txt_fr").value = Math.round(v[0]);
	ActionCommons.getElement("maxPrice_txt_fr").value = Math.round(v[1]);

	$('slider_fr_minspan').innerHTML="$" + Math.round(v[0]);
	$('slider_fr_maxspan').innerHTML="$" + Math.round(v[1]);	

	if(Math.round(v[1]) > 0)
	{
		var url = ActionCommons.getElement("slider_fr_url");
		Controller.executeAction('searchpricefr', 'search_listing_fr',url.value);
	}
}

function submitAction(event)
{
	var e = dojo.event.browser.fixEvent(event);
	if (e.keyCode == e.KEY_ENTER) {

		var args = new Array();
		for(var i = 1; i < arguments.length; i++)
		{
			args.push(arguments[i]);
		}
		
		executeAction.apply(null, args);
		return false;
	}
	return true;
}

function submitProductClicked(){
   window.onbeforeunload = null;
   	var anc = document.getElementById('btnSaveTop');
   	if(anc){
   		anc.innerHTML = 'Saving...';
   		anc.onclick = null;
   	}
   	anc = document.getElementById('btnSaveBottom');
   	if(anc){
   		anc.innerHTML = 'Saving...';
   		anc.onclick = null;
   	}
}

function disableButton (link, text){
   	var anc = document.getElementById(link);
   	if(anc){
   		anc.innerHTML = text;
   		anc.onclick = null;
   	}
}

function setFocusOnLoginForm(){
	var element;
	element = document.getElementById('userlogin_email');
	if(element){
		element.focus();
		return;
	}
	element = document.getElementById('productsearch_txtbx');
	if(element){
		element.focus();
		return;
	}
}

function isReviewRated(){
	//ONLY for addreview action
	var tempRating = document.getElementById('hdn_rating');
	if(tempRating){
		if(tempRating.value == ""  || parseInt(tempRating.value) <= 0){
			//Controller.showMessage("Enter Review Rating", errorTag);
			return false;
		}
	}
	return true;
}

function validateReviewText(value){
	if(dojo.validate.isText(value)){
		//there is some text, should be between 1 to 8000 characters
		var f = { maxlength:8000};
		var ret = dojo.validate.isText(value, f);
		return ret;
	}
	return true;
}

function validateAboutMeText(value){
	if(dojo.validate.isText(value)){
		//there is some text, should be between 1 to 255 characters
		var f = { maxlength:255};
		var ret = dojo.validate.isText(value, f);
		return ret;
	}
	return true;
}

function validateCollectionText(value){
	if(dojo.validate.isText(value)){
		//there is some text, should be between 1 to 255 characters
		var f = { maxlength:255};
		var ret = dojo.validate.isText(value, f);
		return ret;
	}
	return true;
}
function validateAddProductToCollection(){
	var value = dwr.util.getValue('review_text');
	if(dojo.validate.isText(value)){
		var ret = isReviewRated();
		return ret;
	}
	return true;
}

function checkProductPrice(value){
	if(dojo.validate.isRealNumber(value)){
		if(parseFloat(value) > 0){
			return true;
		}
	}
	return false;
}

function validateInterestsText(value){
	if(dojo.validate.isText(value)){
		//there is some text, should be between 1 to 500 characters
		var f = { maxlength:500};
		var ret = dojo.validate.isText(value, f);
		return ret;
	}
	return true;
}

function validateShoppingFavoritesText(value){
	if(dojo.validate.isText(value)){
		//there is some text, should be between 1 to 500 characters
		var f = { maxlength:500};
		var ret = dojo.validate.isText(value, f);
		return ret;
	}
	return true;
}

function checkValidUrl(value)
{
	
	if(value){
		var f = {scheme:'true'}
		if(!dojo.validate.isUrl(value, f)){
			//alert( "Please enter a valid URL");
			return false;
		}
	}
	return true;

}

function validateUrlAndFavoritesStoresText(value)
{
	
	if(value.length>0){
		var f = {scheme:'true'}
		if(!dojo.validate.isUrl(value, f)){
			//alert( "Please enter a valid URL");
			return false;
		}
	}
	if(dojo.validate.isText(value)){
		//there is some text, should be between 1 to 200 characters
		var f = { maxlength:200};
		var ret = dojo.validate.isText(value, f);
		return ret;
	}
	return true;

}

function toggleCollectionFieldsDisplay(id, textareaId){
	var div = document.getElementById(id);
	var reviewarea = document.getElementById(textareaId);
	if(div){
		if(div.style.display == 'block'){
			div.style.display = 'none';
			if(reviewarea){
				reviewarea.setAttribute('rows', '15');
			}
		}else {
			div.style.display = 'block';
			if(reviewarea){
				reviewarea.setAttribute('rows', '5');
			}
		}
	}
}

function delayOverlayClose(msg){
	var val = "<span id='floating_form_err_txt'>" + msg + "</span>";
	ActionCommons.getElement("floating_form_large_body").innerHTML = val;
	var v = setTimeout("closeOverlay()", 2 * 1000); //close after 2 seconds
}

function closeOverlay(){
	Controller.executeAction("closeFloatingForm");
}

function validateDiscussionText(value){
	var msg = "Hey there. You\'ll need both a title and a message or a question to start a new discussion.";
	if(!dojo.validate.isText(value) || value =='Type your question or message here...')
	{
		return msg;
	}
	//there is some text, should be between 1 to 6000 characters
	var f = { maxlength:6000};
	var ret = dojo.validate.isText(value, f);
	if(!ret){
		return "Please limit your message or question to 6000 characters.";
	}else {
		return "";
	}
}

function validateDiscussionCommentText(value){
	var msg = "Hey there. You\'ll need to enter some text in your comment.";
	if(!dojo.validate.isText(value) || value =='Type your comment here...')
	{
		return msg;
	}
	//there is some text, should be between 1 to 6000 characters
	var f = { maxlength:5000};
	var ret = dojo.validate.isText(value, f);
	if(!ret){
		return "Please limit your comment to 5000 characters.";
	}else {
		return "";
	}
}

function validateDiscussionTitle(value){
	var msg = "Hey there. You\'ll need both a title and a message or a question to start a new discussion.";
	if(!dojo.validate.isText(value)  || value =='Type your post title here...')
	{
		return msg;
	}
	return "";
}