//admin.js
//JavaScript for Nova resource database aministration
//Komut webbutveckling 2004

function toPerson(form){
	form.action="../person/showpers1.php";
	form.act.value="viewPers";
	form.submit();
}
function toAdm2(form){
	form.action="admin2.php";
	form.act.value="toAdm2";
	form.submit();
}
function toAdm1(form){
	form.action="admin1.php";
	form.act.value="toAdm1";
	form.submit();
}
function goToApp(form,id,year,perstypes,editlock){
//alert("goToApp editlock="+editlock);
	form.action="../application/newapp3.php";
	form.act.value="viewApp";
	form.applId.value=id;
	form.appYear.value=year;
	if(perstypes.indexOf("H")!=-1){
		form.lock.value=editlock;
		form.viewAs.value="H";
	}
	else if(perstypes.indexOf("A")!=-1){
		form.lock.value="1";
		form.viewAs.value="A";
	}
	else if(perstypes.indexOf("N")!=-1){
		form.lock.value="1";
		form.viewAs.value="N";
	}
	form.submit();
}
function goToProj(form,id,perstypes,editlock){
	form.action="../editproj/editproj1.php";
	form.projId.value=id;
	if(perstypes.indexOf("H")!=-1 || perstypes.indexOf("A")!=-1 || perstypes.indexOf("L")!=-1){
		form.lock.value=editlock;
	}
	else if(perstypes.indexOf("C")!=-1){
		form.lock.value="1";
	}
	form.submit();
}
function toLogin(){
	window.location.href="index.php";
}
function loginToAccount(form){
	return validateLogin(form);
}
function logOut(form){
	form.action="../index.php";
	form.act.value="logout";
	form.submit();
}
function toNwadm(form,nwid){
	form.action="../network/nwadmin.php";
	form.nwId.value=nwid;
	form.submit();
}
function toNwInfoSub(form,nwid){
	form.action="../network/infosub.php";
	form.act.value="infosub";
	form.nwId.value=nwid;
	form.submit();
}
function toNewreg(form){
	form.action="admin/regnewusr.php";
	form.act.value="regnewusr";
	form.submit();
}
function toNewregsub(form){
	form.action="regnewsub.php";
	form.act.value="regnewsub";
	form.submit();
}

function regNewUser(form){
	if(validateNewUser(form)){
		form.submit();
	}
}
function regNewSubs(form){
	if(validateSubs(form)){
		form.submit();
	}
}
function upgradeUser(form){
	if(!validateLogin(form)){
		return;
	}
	form.act.value="upgradeUser";
	form.submit();
}
function updateSubs(form){
	if(validateSubs(form)){
		form.act.value="updateSubs";
		form.submit();
	}
}
function remSubs(form){
	var mess = "All subscriptions and all data for this user will be deleted from NOVA database.";
	if(window.confirm(mess)){
		form.action="../index.php";
		form.act.value="remSubs";
		form.submit();
	}
}
function toNewapp(form){
	form.lock.value="0";
	form.action="../application/newapp0.php";
	form.submit();
}
function validateNewUser(form){
	if(typeof(form.lastname)=="object" && form.lastname.value==""){
		alert("Please enter last name.");
		form.lastname.focus();
		return false;
	}else if(typeof(form.firstname)=="object" && form.firstname.value==""){
		alert("Please enter first name.");
		form.firstname.focus();
		return false;
	}
	else if(typeof(form.email)=="object" && form.email.value==""){
		alert("Please enter e-mail address.");
		form.email.focus();
		return false;
	}
	else if(typeof(form.email)=="object" && form.email.value!="" && form.email.value.search(/^[\-\w]{1,}(\.[\-\w]{1,})*@[\-\w]{1,}(\.[\-\w]{1,})*\.[a-z]{2,4}$/)==-1){
		alert("Please check e-mail address, it doesn't seem correct.");
		form.email.focus();
		return false;
	}
	if(typeof(form.webblink)=="object" && form.webblink.value.length>3 && form.webblink.value.substr(0,4)!="http" && form.webblink.value.substr(0,3)!="ftp"){
		form.webblink.value="http://"+form.webblink.value;
	}
	
	var selval=form.univid.options[form.univid.selectedIndex].value;
	
	if(selval=="-"){
		form.univid.focus();
		alert("Please select university, if the type is 'OTHER', enter the name in the textfield.");
		return false;
	}
	if(form.otherlist.value.indexOf(";"+selval+";")!=-1){
		if(form.otheruni.value==""){
			form.otheruni.focus();
			alert("Please enter the university / organization.");
			return false;
		}else{
			form.university.value=form.otheruni.value;
		}
	}else{
		if(form.otheruni.value!=""){
			form.univid.selectedIndex=0;
			form.univid.focus();
			alert("Please select the name of the university from the list.\nIf not on the list, select a type from 'OTHER' and enter the name in the textfield.");			
			return false;
		}
	}	
	
/* old
	var selval=form.seluni.options[form.seluni.selectedIndex].value;
	if(selval=="Other"){
		if(form.otheruni.value=="" || form.otheruni.value=="Other" || form.otheruni.value=="----"){
			form.otheruni.focus();
			alert("If not on the list, please enter the name of university.");
			return false;
		}else{
			form.university.value=form.otheruni.value;
		}
	}else if(selval=="----"){
		form.seluni.focus();
		alert("Please select university, or if not on the list, enter the name in the textfield.");
		return false;
	}else{
		if(form.otheruni.value!=""){
			form.seluni.selectedIndex=form.seluni.options.length-1;
			form.seluni.focus();
			alert("Please select university from the list.\nIf not on the list, select 'Other' and enter the name in the textfield.");			
			return false;
		}else{
			form.university.value=selval;
		}
	}
*/
	return true;
}
function validateSubs(form){
	if(typeof(form.lastname)=="object" && form.lastname.value==""){
		alert("Please enter last name.");
		form.lastname.focus();
		return false;
	}else if(typeof(form.firstname)=="object" && form.firstname.value==""){
		alert("Please enter first name.");
		form.firstname.focus();
		return false;
	}
	else if(typeof(form.email)=="object" && form.email.value==""){
		alert("Please enter e-mail address.");
		form.email.focus();
		return false;
	}
	else if(typeof(form.email)=="object" && form.email.value!="" && form.email.value.search(/^[\-\w]{1,}(\.[\-\w]{1,})*@[\-\w]{1,}(\.[\-\w]{1,})*\.[a-z]{2,4}$/)==-1){
		alert("Please check e-mail address, it doesn't seem correct.");
		form.email.focus();
		return false;
	}else if(typeof(form.firstname)=="object" && form.firstname.value==""){
		alert("Please enter first name.");
		form.firstname.focus();
		return false;
	}
	if(typeof(form.webblink)=="object" && form.webblink.value.length>3 && form.webblink.value.substr(0,4)!="http" && form.webblink.value.substr(0,3)!="ftp"){
		form.webblink.value="http://"+form.webblink.value;
	}
	
	var selval=form.univid.options[form.univid.selectedIndex].value;
	
	if(selval=="-"){
		form.univid.focus();
		alert("Please select university, if the type is 'OTHER', enter the name in the textfield.");
		return false;
	}
	if(form.otherlist.value.indexOf(";"+selval+";")!=-1){
		if(form.otheruni.value==""){
			form.otheruni.focus();
			alert("Please enter the university / organization.");
			return false;
		}else{
			form.university.value=form.otheruni.value;
		}
	}else{
		if(form.otheruni.value!=""){
			form.univid.selectedIndex=0;
			form.univid.focus();
			alert("Please select the name of the university from the list.\nIf not on the list, select a type from 'OTHER' and enter the name in the textfield.");			
			return false;
		}
	}	
	
	
/* old
	var selval=form.seluni.options[form.seluni.selectedIndex].value;
	if(selval=="Other"){
		if(form.otheruni.value=="" || form.otheruni.value=="Other" || form.otheruni.value=="----"){
			form.otheruni.focus();
			alert("If not on the list, please enter the name of university.");
			return false;
		}else{
			form.university.value=form.otheruni.value;
		}
	}else if(selval=="----"){
		form.seluni.focus();
		alert("Please select university, or if not on the list, enter the name in the textfield.");
		return false;
	}else{
		if(form.otheruni.value!=""){
			form.seluni.selectedIndex=form.seluni.options.length-1;
			form.seluni.focus();
			alert("Please select university from the list.\nIf not on the list, select 'Other' and enter the name in the textfield.");			
			return false;
		}else{
			form.university.value=selval;
		}
	}
*/
	var selval=form.title.options[form.title.selectedIndex].value;
	if(selval==""){
		form.title.focus();
		alert("Please select an appropriate title.");
		return false;
	}
	return true;
}

function setOther(list,form){	
	if(form.otherlist.value.indexOf(";"+list.options[list.selectedIndex].value+";")==-1){
		form.otheruni.value="";
	}
	if(list.options[list.selectedIndex].value=="-"){
		list.selectedIndex=0;
	}
}
function validateLogin(form){
	if(form.username.value == ""){
		alert("Please enter your e-mail address as username!");
		form.username.focus();
		return false;
	}
	else if(form.username.value.search(/^[\-\w]{1,}(\.[\-\w]{1,})*@[\-\w]{1,}(\.[\-\w]{1,})*\.[a-z]{2,3}$/)==-1){
		alert("Please check your e-mail address, it doesn't seem correct.");	
		form.username.focus();
		return false;
	}
	else if(form.passwd.value == ""){
		alert("Please enter your password!");
		form.passwd.focus();
		return false;
	}
	else if(form.passwd.value.length < 6 || form.passwd.value.length > 15){
		alert("Your password must be between 6 and 15 charachters");
		form.passwd.focus();
		return false;
	}
	else if(form.passwd.value.indexOf("%")!=-1){
		alert("Your password can't contain '%'.");
		form.passwd.focus();
		return false;
	}
	return true;
}
function printEvalForm(){
	var winprops="width=690,height=600,scrollbars,resizable,menubar,toolbar";
	var formwin=window.open("evalform.php","evalForm",winprops);
	formwin.focus();
	formwin.print();
}
function openHelp(file,w,h,x,y){
	var winprops="width="+w+",height="+h+",scrollbars,resizable";
	var helpwin=window.open("../help/"+file,"helpWindow",winprops);
	helpwin.moveTo(x,y);
	helpwin.focus();
}
function openSubjInfo(subjid){
	var subjinfwin=window.open("subjinfo.php?subjid="+subjid,"SubjectInfo","height=500,width=600,status,resizable,scrollbars,titlebar");
	subjinfwin.moveTo(100,100);
	subjinfwin.focus();
}

function setStepOne(form,sel){
	if(sel.options[sel.selectedIndex].value=="---"){
		alert("Please select an option!");
		sel.focus();
		return;
	}
	form.act.value="setStepOne";
	form.submit();
}

function setStepTwo(form,sel){
	if(sel.options[sel.selectedIndex].value=="---"){
		alert("Please select an option!");
		sel.focus();
		return;
	}
	form.act.value="setStepTwo";
	form.submit();
}

function setStepThree(form,sel){
	if(sel.options[sel.selectedIndex].value=="---"){
		alert("Please select an option!");
		sel.focus();
		return;
	}
	form.act.value="setStepThree";
	form.submit();
}
