// Creates a popup window based on specified arguments. A handle to the
// popup window is returned so the developer can programmatically manipulate the
// window after it has been launched.
function getPopupWinHandle(loc, width, height, resizable, scrollbars, toolbar, dependent, winName, locationBar, statusBar, align){
	// Evaluate the window arguments.
	var alignValue = "top=1,left=1"

	if(align== 'center'){
	
		LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
		TopPosition = (screen.height) ? (screen.height-height)/2 : 0;
		alignValue = "top=" + TopPosition + ",left=" + LeftPosition;
	} else if (align=='topright'){
		LeftPosition = (screen.width) ? (screen.width-width): 0;
		TopPosition = 1;
		alignValue = "top=" + TopPosition + ",left=" + LeftPosition;
	} else if (align=='topcenter'){
		LeftPosition = (screen.width) ? (screen.width-width)/2 : 0;
		TopPosition = 1;
		alignValue = "top=" + TopPosition + ",left=" + LeftPosition;	
	} else if (align=='topleft'){
		alignValue = "top=1,left=1";
	}
	
	var win = eval("window.open(loc, '"
		+ winName
		+ "','width=" + width
		+ ",height=" + height
		+ ",resizable=" + resizable
		+  ",scrollbars=" + scrollbars
		+  ",toolbar=" + toolbar
		+  ",dependent=" + dependent
		+  ",screenX=1,screenY=1," + alignValue
		+  ",location=" + locationBar
		+ ",status=" + statusBar + "')");
		//set the focus to the newly created window.
		win.focus();
	// Return a handle to the newly created window.
	return win;
} // function getPopupWinHandle



// Creates a popup window based on specified arguments. 
function popupWin(loc, width, height, resizable, scrollbars, toolbar, dependent, winName,locationBar, statusBar,align){
	// Here we DO NOT return the handle to the window just created as it may
	// cause problems when embedding JavaScript statements in HTML anchor tags.
	var newWin = getPopupWinHandle(loc, width, height, resizable, scrollbars, toolbar, dependent, winName,locationBar, statusBar, align);
} // function popupWin






// Below selectbox.js javascript functions:
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
function selectUnselectMatchingOptions(obj,regex,which,only){if(window.RegExp){if(which == "select"){var selected1=true;var selected2=false;}else if(which == "unselect"){var selected1=false;var selected2=true;}else{return;}var re = new RegExp(regex);for(var i=0;i<obj.options.length;i++){if(re.test(obj.options[i].text)){obj.options[i].selected = selected1;}else{if(only == true){obj.options[i].selected = selected2;}}}}}
function selectMatchingOptions(obj,regex){selectUnselectMatchingOptions(obj,regex,"select",false);}
function selectOnlyMatchingOptions(obj,regex){selectUnselectMatchingOptions(obj,regex,"select",true);}
function unSelectMatchingOptions(obj,regex){selectUnselectMatchingOptions(obj,regex,"unselect",false);}
function sortSelect(obj){var o = new Array();if(obj.options==null){return;}for(var i=0;i<obj.options.length;i++){o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;}if(o.length==0){return;}o = o.sort(
function(a,b){if((a.text+"") <(b.text+"")){return -1;}if((a.text+"") >(b.text+"")){return 1;}return 0;});for(var i=0;i<o.length;i++){obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);}}
function selectAllOptions(obj){for(var i=0;i<obj.options.length;i++){obj.options[i].selected = true;}}
function moveSelectedOptions(from,to){if(arguments.length>3){var regex = arguments[3];if(regex != ""){unSelectMatchingOptions(from,regex);}}for(var i=0;i<from.options.length;i++){var o = from.options[i];if(o.selected){to.options[to.options.length] = new Option( o.text, o.value, false, false);}}for(var i=(from.options.length-1);i>=0;i--){var o = from.options[i];if(o.selected){from.options[i] = null;}}if((arguments.length<3) ||(arguments[2]==true)){sortSelect(from);sortSelect(to);}from.selectedIndex = -1;to.selectedIndex = -1;}
function copySelectedOptions(from,to){var options = new Object();for(var i=0;i<to.options.length;i++){options[to.options[i].value] = to.options[i].text;}for(var i=0;i<from.options.length;i++){var o = from.options[i];if(o.selected){if(options[o.value] == null || options[o.value] == "undefined" || options[o.value]!=o.text){to.options[to.options.length] = new Option( o.text, o.value, false, false);}}}if((arguments.length<3) ||(arguments[2]==true)){sortSelect(to);}from.selectedIndex = -1;to.selectedIndex = -1;}
function moveAllOptions(from,to){selectAllOptions(from);if(arguments.length==2){moveSelectedOptions(from,to);}else if(arguments.length==3){moveSelectedOptions(from,to,arguments[2]);}else if(arguments.length==4){moveSelectedOptions(from,to,arguments[2],arguments[3]);}}
function copyAllOptions(from,to){selectAllOptions(from);if(arguments.length==2){copySelectedOptions(from,to);}else if(arguments.length==3){copySelectedOptions(from,to,arguments[2]);}}
function swapOptions(obj,i,j){var o = obj.options;var i_selected = o[i].selected;var j_selected = o[j].selected;var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);o[i] = temp2;o[j] = temp;o[i].selected = j_selected;o[j].selected = i_selected;}
function moveOptionUp(obj){for(i=0;i<obj.options.length;i++){if(obj.options[i].selected){if(i != 0 && !obj.options[i-1].selected){swapOptions(obj,i,i-1);obj.options[i-1].selected = true;}}}}
function moveOptionDown(obj){for(i=obj.options.length-1;i>=0;i--){if(obj.options[i].selected){if(i !=(obj.options.length-1) && ! obj.options[i+1].selected){swapOptions(obj,i,i+1);obj.options[i+1].selected = true;}}}}
function removeSelectedOptions(from){for(var i=(from.options.length-1);i>=0;i--){var o=from.options[i];if(o.selected){from.options[i] = null;}}from.selectedIndex = -1;}
function removeAllOptions(from){for(var i=(from.options.length-1);i>=0;i--){from.options[i] = null;}from.selectedIndex = -1;}
function addOption(obj,text,value,selected){if(obj!=null && obj.options!=null){obj.options[obj.options.length] = new Option(text, value, false, selected);}}



String.prototype.trimStr = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};


//passwords should not have spaces and be less than 50 chars
function validatePassword(pword){
    var errMsg = "";
    
    if(pword.length > 49){
        errMsg = "The password entered is too long - must be less than 50 characters.";
    }
    
    if(pword.indexOf(" ") >= 0){
        errMsg = "The password field must not contain spaces.";
    }
    
    if(pword.length == 0){
        errMsg = "The password field must not be empty.";
    }
    
    return errMsg;
}

// email validation
/**
 *Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */



function validateEmailAddress(strorig) {
	
		var str = strorig.trimStr();
		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		
		var errMsg = "";
		var error = "The email address entered is not valid.";
		
		if(lstr == 0){
		  errMsg = "The email address field must not be empty.";
		}
		
		if(lstr > 99){
		  errMsg = "The email address field must be less than 100 characters.";
		}
		
		
		if (str.indexOf(at)==-1){
            errMsg = error;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   errMsg = error;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		     errMsg = error;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		     errMsg = error;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		     errMsg = error;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		     errMsg = error;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    errMsg = error;
		 }

 		 return errMsg;					
} //end of function echeck





