var upload_progress = false;
  
function trimLeft(s) {
	var whitespaces = " \t\n\r";
	for(n = 0; n < s.length; n++) { 
		if (whitespaces.indexOf(s.charAt(n)) == -1) 
			return (n > 0) ? s.substring(n, s.length) : s; 
	}
	return("");
}
function trimRight(s){
	var whitespaces = " \t\n\r";
	for(n = s.length - 1; n  > -1; n--) { 
		if (whitespaces.indexOf(s.charAt(n)) == -1) 
			return (n < (s.length - 1)) ? s.substring(0, n+1) : s; 
	}
	return("");
}
function trim(s) {
	return ((s == null) ? "" : trimRight(trimLeft(s))); 
}
function isBlank(edit_id, ErrorMsg) {
    var ff=document.getElementById(edit_id);
	var strTrimmed = trim(ff.value);
	if (strTrimmed.length > 0) return false;
	alert(ErrorMsg);
	ff.focus();
	return true;
}

function isEmptyFile(edit_id, ErrorMsg) {
	var edit_id2 = edit_id+"_file";
    var ff=document.getElementById(edit_id2);
	var strTrimmed = trim(ff.value);
	if (strTrimmed.length > 0){
		upload_progress=true;
		 return false;
	}
	alert(ErrorMsg);
	ff.focus();
	return true;
}

function isFile(edit_id, ErrorMsg) {
	var edit_id2 = edit_id+"_file";
    var ff=document.getElementById(edit_id2);
	var strTrimmed = trim(ff.value);
	if (strTrimmed.length > 0){
		upload_progress=true;
	}
	return false;
}

function isFileUpload(edit_id, ErrorMsg) {
    var edit_id2 = edit_id+"_file";
    var ff=document.getElementById(edit_id2);
    var strTrimmed = trim(ff.value);
    if (strTrimmed.length > 0){
        upload_progress=true;
    }
    return false;
}

function isNotBlank(edit_id, ErrorMsg) {
    var ff=document.getElementById(edit_id);
        var strTrimmed = trim(ff.value);
        if (strTrimmed.length > 0) return true;
        alert(ErrorMsg);
        ff.focus();
        return false;
}
function isSamePassword (edit_id, ErrorMsg, onTheFly) {
	var ff1 = document.getElementById('new1_password_1_');
	var ff2 = document.getElementById('new2_password_1_');
	if ((ff1 == null) || (ff1 == undefined) || (ff2 == null) || (ff2 == undefined))
		return true;
	if ((trim(ff1.value)) == (trim(ff2.value))) {
		return true;
	}
	alert(ErrorMsg);
	ff1.focus();
	return false;
}
function isPassword(edit_id, ErrorMsg) {
var 	id1=concat("_new1_",edit_id);
alert(id1.value);
    var ff1=document.getElementById("_new1_".edit_id);
    var ff2=document.getElementById("_new2_".edit_id);
        var strTrimmed = trim(ff.value);
        if (strTrimmed.length > 0) return true;
        alert(ErrorMsg);
        ff.focus();
        return false;
}
function isBlankRadio(edit_id, ErrorMsg) {
    var ff=document.getElementById(edit_id);

    return(false);
    // This needsd to be debugged
	if (returnSelection(ff) == null) {
		alert(ErrorMsg);
		ff.focus();
		return true;
	} else {
		return false;
	}
}
function isBadURL(edit_id, ErrorMsg) {
    var ff=document.getElementById(edit_id);

	var strTrimmed = trim(ff.value);
	if (strTrimmed.length == 0 || 
		strTrimmed.substring(0,7) == 'http://' || 
		strTrimmed.substring(0,6) == 'ftp://' || 
		strTrimmed.substring(0,7) == 'mailto:' || 
		strTrimmed.substring(0,8) == 'https://') return false;
	alert(ErrorMsg);
	ff.focus();
	return true;
}
function isNumber(edit_id, ErrorMsg) {
    var ff=document.getElementById(edit_id);

	var strVal = trim(ff.value);
	if (strVal.length == 0) return true;
	var 	x = 0;
	for (i=0;i < strVal.length; i++) { 
		if ((strVal.charAt(i) >= '0' && strVal.charAt(i) <= '9') || strVal.charAt(i) == ".") x++;
	}
	if (strVal.length > x) {
		alert(ErrorMsg);
		ff.focus();
		return false;
	} else {
		return true;
	}
}
function isEmail(edit_id, ErrorMsg) {
    var ff=document.getElementById(edit_id);

	var strMsg = ""; 
	var chAt  = '@'; 
	var chDot = '.'; 
	var strEmailAddr = trim(ff.value);
	   if (strEmailAddr.length == 0) return true;
	   if (strEmailAddr.indexOf(" ") == -1)
	   {
	       var iFirstAtPos = strEmailAddr.indexOf(chAt);
	       var iLastAtPos = strEmailAddr.lastIndexOf(chAt);
	       if (iFirstAtPos > 0 && iFirstAtPos < (strEmailAddr.length - 1) &&iFirstAtPos == iLastAtPos) {
		   // look for '.' there must be at least one char between '@' and '.'
		   var iDotPos = strEmailAddr.indexOf(chDot, iFirstAtPos + 1);
		   if (iDotPos > (iFirstAtPos + 1) && iDotPos < (strEmailAddr.length -1)) return true;
	       }
	   }
	   alert(ErrorMsg);
	   ff.focus();
	   return false;
}

function isFirstOption(edit_id, ErrorMsg) {

    var ff=document.getElementById(edit_id);

	if (ff.options[0].selected) {
		alert(ErrorMsg);
	    ff.focus();
		return true;
	} else {
		return false;
	}
}

function returnSelection(radioButton) {

	var selection=null;
    alert("Radio Button");
	for(var i=0; i<radioButton.length; i++) {
		    alert(i);
		if(radioButton[i].checked) {
		    alert("selected");
			selection=radioButton[i].value;
			return selection;
		}
	}
	return selection; 
}

function isChecked(edit_id, ErrorMsg) {
    var ff=document.getElementById(edit_id);

	if ( ff.checked == 1 ) 
	{
		return true;
	} else { 	
		alert(ErrorMsg);
		ff.focus();
		return false;
	}
}
function addLoadEvent(func) {
    var oldonload = window.onload;
    if(typeof window.onload != "function")
        window.onload = func;
    else
        window.onload = function() {
        oldonload();
        func();
    }
}

