/**
 * This file contains utility javascript functions
 */
 
 
/**
 * This function trims and returns a given string.
 * It looks complicated, but, this is one of the best implementations of the string trim function.
 */
function trimString (str) {
str = str.replace(/^\s+/, '');
for( var i = str.length-1; i > 0; --i ) {
if( /\S/.test( str.charAt(i) ) ) {
str = str.substring( 0, i+1 );
break;
}
}
return str;
}

/**
 * This function takes a text area control and checks for the value.
 * If the value is empty (after trimming the text), an alert message is shown
 * or else, the form is submitted 
 */
function checkChange(textArea, message) {
	//alert(textArea.value != null && trimString(textArea.value).length == 0);
	if(textArea.value == null || trimString(textArea.value).length == 0) {
		alert(message);
	} else {
		textArea.form.submit();
	}
}

function hideDiv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function showDiv(id) {
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}




function getTopPos(inputObj) {
	var topValue = 0;
	if (inputObj.offsetParent) {
		while (inputObj.offsetParent) {
			topValue += inputObj.offsetTop
			inputObj = inputObj.offsetParent;
		}
	} else if (inputObj.y) topValue += inputObj.y;
	return topValue;
}

function getLeftPos(inputObj) {
	var leftValue = 0;
	if (inputObj.offsetParent) {
		while (inputObj.offsetParent) {
			leftValue += inputObj.offsetLeft
			inputObj = inputObj.offsetParent;
		}
	} else if (inputObj.x) leftValue += inputObj.x;
	return leftValue;
}

function checkTextBoxDefaultText(control, defaultText, clearFlag) {
	if((control.value == defaultText) && clearFlag) {
		control.value = '';
	} else if(((control.value == null) || (trimString(control.value) == '')) && (!clearFlag)) {
		control.value = defaultText;
	}
}

var stars = {

  highlight: function(divid, starnumber, starsize, starclass) {
    for(var i=1; i<=starnumber; i++) {
    document.getElementById(divid+'Star'+i).className = starclass + starsize;
    }
  for(var i=starnumber+1; i<= 5; i++) {
    document.getElementById(divid+'Star'+i).className = 'starMt' + starsize;
    }  
  }
};

