ns = (document.layers)? true:false
ie = (document.all)? true:false 
mainWndHref='manageUsersViewEntity.asp';

function owm(url) {
	var ie=document.all;
	var options = 'directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,titlebar=no,toolbar=no';
	if (ie) options += ",fullscreen=yes";
	else if (window.screen) options += ',width=' + window.screen.availWidth + ',height=' + window.screen.availHeight;
	options += ',screenX=0,screenY=0,top=0,left=0';
	this.name='main';
	var win = window.open(url, 'maxWnd', options);win.focus();win.moveTo(0, 0);
}

function flipInheritance(chkBox){
	var isChecked = chkBox.checked;
   for (var i=0, j=document.forms[0].elements.length; i<j; i++) {
		var obj=document.forms[0].elements[i];
		var tmpstr=obj.name;
		if ( tmpstr.indexOf('R_') > -1){
			if (isChecked) obj.disabled=true;
			else obj.disabled=false;
      }
	}
}
function doInherit(cmbBox){
	eval("var chkBox = document.forms[0]." + cmbBox.name.substr(0, cmbBox.name.indexOf("_from")));
	if (cmbBox.options[cmbBox.selectedIndex].value != "-1"){
		chkBox.checked=true;
	}else{
		chkBox.checked=false;
	}
	window.document.forms[0]._action.value='CHANGE_INHERITANCE';
	window.document.forms[0].submit();	
}
function newEntity(etype){
	var p_root='True';
	var path=document.forms[0].path.value;
	window.location.href=mainWndHref + '?p_root=' + p_root + '&path=' + path + '/' + etype + '_-1';
}

function entityName(type){
	switch (type){
		case "C": return "Client";
		case "B": return "Branch";
		case "G": return "Group";
		case "U": return "User";
		default:;
	}
}
function removeRelation(path, type, parent_type){
	var msg='Remove ' + entityName(type) + ' from current ' + entityName(parent_type);
	if (!window.confirm(msg)) return;
	window.document.forms[0]._action.value='REMOVE_RELATION';
	window.document.forms[0].param0.value=path;
	window.document.forms[0].submit();
}
function selectUsers(){
	window.document.forms[0]._action.value='SELECT_USERS_TO_ADD';
	window.document.forms[0].submit();
}
function addUsers(){
	window.document.forms[0]._action.value='ADD_USERS_TO';
	window.document.forms[0].submit();
}
function setActiveTab(no){
	if (no==window.document.forms[0].tab_active.value) return;
	window.document.forms[0]._action.value='';
	window.document.forms[0].tab_active.value=no;
	window.document.forms[0].submit();
}
function setActiveSubTab(no){
	if (no==window.document.forms[0].sub_tab_active.value) return;
	window.document.forms[0]._action.value='';
	window.document.forms[0].sub_tab_active.value=no;
	window.document.forms[0].submit();
}
function persistUser(){
	// error strings
	var iEmail = "This field must be a valid email address (like yourname@yourdomain.com). Please reenter it now."
	// reg expressions
	var reEmail = /^.+\@.+\..+$/
	// check required fields
	var frm=document.forms[0];
	if ( frm.usr_email.value=='' ){
		alert('Please complete fields marked with *!');
		return;
	}
	if (!reEmail.test(frm.usr_email.value)){
		warnInvalid(frm.usr_email, iEmail);
		return;
	}
	var p=frm.usr_password;var pv=p.value;
	if (pv.length>0){
		if (pv.length<6){warnInvalid(p,'Please enter a password at least 6 characters long');return false;}
		if (!/\d/.exec(pv)){warnInvalid(p,'Passwords must contain at least 1 digit'); return false;}
		if (!/[a-zA-Z]/.exec(pv)){warnInvalid(p,'Passwords must contain at least 1 letter'); return false;}
		if (/\s/.exec(pv)){warnInvalid(p,'Passwords may not contain spaces'); return false;}
	}
	frm._action.value="PERSIST_USER";
	frm.submit();

}
function warnInvalid (theField, s){
	theField.focus();
	if (theField.selectedIndex==undefined) theField.select();
	alert(s);
	return false;
}
function isValidDate(str){
	var isDate = new Date(str);
	if(isNaN(isDate) || isDate == "Invalid Date") return false;
	else return true;
}
function massAccnt(){
	var path=document.forms[0].path.value;
	window.location.href=mainWndHref + '?p_root=True&path=' + path + '/U_-1&tab_active=-1';	
}
function deleteUser(uid){
	var msg='Are you sure you want to delete this user?';
	if (!window.confirm(msg)) return;
	window.document.forms[0]._action.value='DELETE_USER';
	window.document.forms[0].param0.value=uid;
	window.document.forms[0].submit();
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}

