//in order for the scripts to function they must be loaded after the page loads 
//so the id's will be loaded into the DOM
window.onload = prepareScript;

//prepareScript waits for the page to load then calls the functions
function prepareScript(){
	var itemDescription = document.getElementById("itemdesc");
	var counter = document.getElementById("counter");
	itemDescription.onkeypress = function(){
		textCounter(this,this.form.counter,255);
	}
	counter.onblur = function(){
		textCounter(this,this.form.counter,255);
	}
	focusForm();
}

//if the id exists on the page, set the relevant form focus 
function focusForm(){
	//login page	
	if(document.getElementById("usernamelogin")){
		document.getElementById("usernamelogin").focus();
	//add warehouse item page
	} else if(document.getElementById("name")){
		document.getElementById("name").focus();
	} else if(document.getElementById("category")){
		document.getElementById("category").focus();
	} else if(document.getElementById("newsheadline")){
		document.getElementById("newsheadline").focus();
	} 
}
	
//used to track how many characters have been entered into a text area
function textCounter( field, countfield, maxlimit ) {
	if ( field.value.length > maxlimit )
  	{
  	   	field.value = field.value.substring( 0, maxlimit );
  	}
  	else
  	{
    	countfield.value = maxlimit - field.value.length;
  	}
}