  /*
  * Function to trim spaces around a string.
  * todo: extend to trim all whitespace surrounding a string.
  */
  function trim(sometext){
	if ( sometext) {
	
		//trim preceding spaces
		while ( (sometext.length > 0) && (sometext.substring(0,1) == ' ') ) {
			sometext=sometext.substring(1);
		}
		
		//trim trailing spaces
		while ( (sometext.length > 0) && (sometext.substring(length-1,1) == ' ') ) {
			sometext=sometext.substring(0,length-1);
		}				
	}
	return sometext;
 }
  
function setDivContents(fromUrl, loadTo, pageTitle) { 
	dojo.xhrGet( {
	 url: encodeURI(fromUrl),
	 headers:{"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"},
	 handleAs: "text",
	 preventCache: true,
	 timeout: 55000, // Time in milliseconds
	 // The LOAD function will be called on a successful response.
	 load: function(response, ioArgs) {
		var mainContentPane = dijit.byId(loadTo);
			mainContentPane.setContent(response);
			//dojo.back.addToHistory(state);
				//pageInit(); important . do not remove.
		return response;

			},

	// The ERROR function will be called in an error case.
	error: function(response, ioArgs) {
		  console.log("error " + response);
		  document.getElementById("message").innerHTML = response;
			  console.error("HTTP status code: ", ioArgs.xhr.status, response);
			   return response;
			}
	});
}

function loadLoginPage(){
	setDivContents('/classbar/main/loginUser.jsp', 'ceditor', 'Login User');
}


function postLoginForm(){
	postGivenForm('/classbar/LoginServlet','userLogin',onLoginSuccess,showError);
}

function postGivenForm(toUrl, formName, onLoad, onError)
{		
	dojo.io.iframe.send({
		// The target URL on your webserver:				
		url: toUrl,            	 
		preventCache: true,
		headers:{"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"},
		// The HTTP method to use, form specified POST:
		method: "POST",            	 
		// The form node, which contains the
		// to be transfered form elements:
		form: formName,            	 
		// The used data format:
		handleAs: "json",            	 
		// Callback on successful call:
		load: onLoad ,            	 
		// Callback on errors:
		error: onError
	});

}	

function displayLoginMsg(jdata,msgFldId){			
	if ( jdata["errorMessage"]) {
		msg = jdata.errorMessage ;			
	}
	else
	if ( jdata["message"] ) {
		msg = jdata.errorMessage;			
	}			
	dojo.byId(msgFldId).innerHTML=msg;		
}

function onLoginSuccess(response, ioArgs){
	console.log("show sucess");
	displayLoginMsg(response, "lmessage");	
	return response;
}

function showError(response, ioArgs){
	displayLoginMsg(response, "lmessage");		

}


function setTextValue(fieldName,jsonField)
{
	console.log(fieldName);
	if (jsonField){
		dijit.byId(fieldName).setValue(jsonField);
	}
	else{
		dijit.byId(fieldName).setValue("");
	}
}

function setDateValue(fieldName,jsonField)
{
	console.log(fieldName);
	if (jsonField){
		dijit.byId(fieldName).setDisplayedValue(jsonField);
	}
	else{
		dijit.byId(fieldName).setDisplayedValue("");
	}
}



function setHiddenFieldValue(field,jsonField)
{			
	if (jsonField){
		field.value=jsonField;
	}
	else{
		field.value="";
	}
}		

function setCheckBox(fieldName, jsonField)
{
	console.log(fieldName);
	if (jsonField && ( jsonField =='Y') ) {
		dijit.byId(fieldName).setChecked(true);

	}
	else{
		dijit.byId(fieldName).setChecked(false);
	}
} 

function setCheckBoxIfCheckedValue(fieldName, jsonField, checkedValue)
{
	console.log(fieldName);
	if (jsonField && ( jsonField == checkedValue) ) {
		dijit.byId(fieldName).setChecked(true);

	}
	else{
		dijit.byId(fieldName).setChecked(false);
	}
} 