/**
* counts the value of inputelement which can be passed by reference or as a string identifier
* places the count value in spanid
* limit is optional but if set, compares the count with the limit value
*/
function CountCharacters(inputelement, spanid, limit)
{
	var i = $('#'+inputelement);
	var s = $('#'+spanid);
	
	var text = i.val();
	
	var limittext = "";
	if (limit != undefined) { 
		limittext = " / " + limit + " characters"; 
	}
	if (limit != undefined) {
		if (text.length >= limit) {
			limittext = "<strong style=\"color:red\">" + text.length + "</strong>" + limittext;
		}
		else {
			limittext = text.length + limittext;
		}
	}
	s.html(limittext);
}
/**
* opens a popup window for the import facility
*/
function open_company_search_popup(region) {
	var myWindow = window.open('/network-central/company-import.php?region=' + region, 'findcompany', 'width=650,height=500,scrollbars=yes,resizable=yes');
	myWindow.focus();
}
/**
* perform character counts for all elements specified
*/
function count_description(limit) {
	CountCharacters("entrydescription", "countentrydescription", limit);
}
function count_whychooseus(limit) {
	CountCharacters("entrywhychooseus", "countentrywhychooseus", limit);
}
function count_memoryhook(limit) {
	CountCharacters("entrymemoryhook", "countentrymemoryhook", limit);
}

/* set the parent category box to be selected for a particular category */
function setParentForCat(c) {
	var cat = null;
	for (var i=0; i<cats.length; i++) {
		if (cats[i][1]==c) {
			cat = cats[i]; break;
		}
	}
	if (cat==null) return false;
	var s = document.getElementById('searchparentcategory');
	if (!s) return false;
	for (var i=0; i<s.options.length; i++) {
		if (s.options[i].value==cat[0]) {
			s.options[i].selected=true; 
			//populateSubCats(p[1]);
			return true;
		}
	}
	return false;
}

/* populate the subcategories select */
function populateSubCats(parent_id,child_id) {
	if (child_id) {
		for (var i=0; i<cats.length; i++) {
			if (cats[i][1]==child_id) { parent_id = cats[i][0]; break; }
		}
	}
	var s = document.getElementById('searchcategory');
	s.options.length=0;
	s.disabled=false;
	s.options[s.options.length] = new Option(' ','');
	for (var i=0; i<cats.length; i++) {
		if (cats[i][0]==parent_id) {
			s.options[s.options.length] = new Option(cats[i][2],cats[i][1]);
		}
	}
	/* THIS CODE WORKS CORRECTLY IN EVERY BROWSER EXCEPT INTERNET EXPLORER
	$('#searchcategory').get(0).add( new Option(' ',''), document.all ? 0 : null);
	for (var i=0; i<cats.length; i++) {
		if (cats[i][0]==parent_id) {
			$('#searchcategory').get(0).add( new Option(cats[i][2],cats[i][1]), document.all ? 0 : null);
		}
	}
	*/
	if (child_id) {
		// set a selected option
		$('#searchcategory').val(child_id);
	}
}

