

function showsub(eID) {
	document.getElementById(eID).style.display = "inline";
}



function hidesub(eID) {
	document.getElementById(eID).style.display = "none";
}


function selectmajor(dispLang) {
	var obj=document.getElementById('id_majorCategory');
	var index=obj.selectedIndex;
	var majroID = obj.options[index].value;

	BeanHandler.getSubcategories(majroID, function(dataArray) {
		obj=document.getElementById('id_category'); 
		obj.options.length=0;
		if(dataArray != null) {
			if(dispLang == '1')
			{
				obj.options.add(new Option("All Sub Category", -1)); 
			}
			else
			{
				obj.options.add(new Option("All Sub Category", -1)); 
			}
			for(var i = 0; i < dataArray.length; i++) {
				var data = dataArray[i];
				if(dispLang == '1')
				{
					obj.options.add(new Option(data.categoryNameCN, data.categoryID)); 
				}
				else
				{
					obj.options.add(new Option(data.categoryName, data.categoryID)); 
				}
				//alert(data.categoryName)
			}
		}
		else {
			if(dispLang == '1')
			{
				obj.options.add(new Option("All Sub Category", -1)); 
			}
			else
			{
				obj.options.add(new Option("All Sub Category", -1)); 
			}
		}
		//alert("ok" + dataArray.length);
	  });
}


function selectmajor2(dispLang) {
	var obj=document.getElementById('id_majorCategory');
	var index=obj.selectedIndex;
	var majroID = obj.options[index].value;
	
	

	if(majroID == 2) {//Apparels and Accessories
		document.getElementById("existing_tags").style.display = "none";
		document.getElementById("about_tags").style.display = "none";
		document.getElementById("apparel_tags").style.display = "block";
		document.getElementById("apparel_addition_tags").style.display = "block";
		
		document.getElementById("id_tags").readOnly = true;

		
	}
	else{ //Hide apparels styles
		document.getElementById("existing_tags").style.display = "block";
		document.getElementById("about_tags").style.display = "block";
		document.getElementById("apparel_tags").style.display = "none";
		document.getElementById("apparel_addition_tags").style.display = "none";
		document.getElementById("id_tags").readOnly = false;
	}	
	
	BeanHandler.getSubcategories(majroID, function(dataArray) {
		obj=document.getElementById('id_category'); 
		obj.options.length=0;
		if(dataArray != null) {
			for(var i = 0; i < dataArray.length; i++) {
				var data = dataArray[i];
				if(dispLang == '1')
				{
					obj.options.add(new Option(data.categoryNameCN, data.categoryID)); 
				}
				else
				{
					obj.options.add(new Option(data.categoryName, data.categoryID)); 
				}
				//alert(data.categoryName)
			}
		}
		//alert("ok" + dataArray.length);
	  });
}

function init_edit_screen(dispLang) {

	var obj=document.getElementById('id_majorCategory');
	var index=obj.selectedIndex;
	var majroID = obj.options[index].value;
	
	
	if(majroID == 2) {//Apparels and Accessories
		document.getElementById("existing_tags").style.display = "none";
		document.getElementById("about_tags").style.display = "none";
		
		document.getElementById("apparel_tags").style.display = "block";
		document.getElementById("apparel_addition_tags").style.display = "block";
		
		document.getElementById("id_tags").readOnly = true;

		
	}
	else{ //Hide apparels styles
		document.getElementById("existing_tags").style.display = "block";
		document.getElementById("about_tags").style.display = "block";
		document.getElementById("apparel_tags").style.display = "none";
		document.getElementById("apparel_addition_tags").style.display = "none";
		document.getElementById("id_tags").readOnly = false;
	}	
	
}

function selectmajorcategory(dispLang) {
	var obj=document.getElementById('id_majorCategory');
	var index=obj.selectedIndex;
	var majroID = obj.options[index].value;

	BeanHandler.getSubcategories(majroID, function(dataArray) {
		obj=document.getElementById('id_category'); 
		obj.options.length=0;
		obj.options.add(new Option('All Items...', -1)); 
		if(dataArray != null) {
			for(var i = 0; i < dataArray.length; i++) {
				var data = dataArray[i];
				if(dispLang == '1')
				{
					obj.options.add(new Option(data.categoryNameCN, data.categoryID)); 
				}
				else
				{
					obj.options.add(new Option(data.categoryName, data.categoryID)); 
				}
				//alert(data.categoryName)
			}
		}
		//alert("ok" + dataArray.length);
	  });
}


String.prototype.count=function(s1) { 
	return (this.length - this.replace(new RegExp(s1,"g"), '').length) / s1.length;
}

function additional_tags_changed(){
	
	var tags = "";
	var checkTags;
	
		
	// Get tags from the checkboxes
	for (i = 0; i < document.add_gift_form.apparel_tags_checkbox.length; i++)
	      if (document.add_gift_form.apparel_tags_checkbox[i].checked){
		      if(tags.length > 0)				      
	    	  	tags = tags +"," + document.add_gift_form.apparel_tags_checkbox[i].value;
		      else
		    	  tags = tags + document.add_gift_form.apparel_tags_checkbox[i].value;  	
	    	  	
	}
	
    // Get tags from the text input
	var additionalTags = document.add_gift_form.additional_tags.value;
	var trimedAdditionalTags = trimAll(additionalTags);

	trimedAdditionalTags  = removeSpaces(trimedAdditionalTags );
    trimedAdditionalTags = trimCommas(trimedAdditionalTags);	
	
    if(trimedAdditionalTags.count(',') > 2) {
    	
    	//document.getElementById('additional_tags').focus();
    	setTimeout("document.getElementById('additional_tags').focus()",0);
    	alert("Maxium 3 additional tags.");
    	setTimeout("document.getElementById('additional_tags').focus()",0);
    }
    else{
    	if(tags != ""){
    		tags = tags + ","+trimedAdditionalTags ; 
    	}
    	else{
    		tags = trimedAdditionalTags ;
    	}
    	dwr.util.setValue("id_tags", tags);
    }
	
}	

function apparel_tags_changed(lastCheckBox){
	
	var tags = "";
	var checkTags;
	
	var tagCount = 0;
	for (i = 0; i < document.add_gift_form.apparel_tags_checkbox.length; i++){
	      if (document.add_gift_form.apparel_tags_checkbox[i].checked){
	    	  tagCount ++;
	      }
	}

	if(tagCount > 3){ // Too manay pre-defined tags selected.
		alert("Too manay style tags. Maxium 3.");
		lastCheckBox.checked=false;
	}	
	
	
	for (i = 0; i < document.add_gift_form.apparel_tags_checkbox.length; i++)
	      if (document.add_gift_form.apparel_tags_checkbox[i].checked){
		      if(tags.length > 0)				      
	    	  	tags = tags +"," + document.add_gift_form.apparel_tags_checkbox[i].value;
		      else
		    	  tags = tags + document.add_gift_form.apparel_tags_checkbox[i].value;  	
	    	  	
	}
	

	var additionalTags = document.add_gift_form.additional_tags.value;
	var trimedAdditionalTags = trimAll(additionalTags);

	trimedAdditionalTags  = removeSpaces(trimedAdditionalTags );
    trimedAdditionalTags = trimCommas(trimedAdditionalTags);	
	
	
	if(additionalTags != ""){
	
		tags = tags + ","+trimedAdditionalTags ; 
	}	
	
	dwr.util.setValue("id_tags", tags);
}	



function removeSpaces(string) {
	 return string.split(' ').join('');
	}


function trimAll(sString)
{
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
} 
function trimCommas(sString)
{
	while (sString.substring(0,1) == ',')
	{
		sString = sString.substring(1, sString.length);
	}
	while (sString.substring(sString.length-1, sString.length) == ',')
	{
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
} 