// set qut= "
var qut=String.fromCharCode(34);

//This function return the last num charters of a string
function last(str,num) {
  var l=str.length;
  if(num>l) {return "";}
  return(str.substring(l-num,l));
}

//This function is used to set the ProcessMe form item
//the obj is any form item in the same form
function setProcessFlag(obj){
  if (obj.form) {
	  if(obj.form.ProcessMe) {
    	obj.form.ProcessMe.value=1;
	  }
  }
}

//Thes two functions work in tandum where the flags are set to publish and order
//This function is called when the checkbox is is clicked
//This function takes two arguments cb is the checkbox, dd is the dropdown box
//If the box is now black it changes the dropdown to say do not show
//else if make the dropdown box say the last thing
function pubOrd(cb,dd) {
  if(cb.checked==false) {dd.selectedIndex=dd.options.length-1;}
  else {dd.selectedIndex=dd.options.length-2;}
  setProcessFlag(cb);
}
//This function is called when the dropdown is is changed
//This function takes two arguments dd is the dropdown box, cb is the checkbox
//If the dropdown box is now says don't show, it unchecks the box
//else it checks the box
function ordPub(dd,cb) {
  if(dd.selectedIndex==dd.options.length-1) {cb.checked=false;}
  else {cb.checked=true;}
  setProcessFlag(dd);
}

//This function submits form for manage person
//The first argument is the form beeing submitted
//and the second is a string with the name of the tab to go to
function clickMPTab(frm,tab) {
  if(tab!="gen" && tab!="con" && tab!="edu" && tab!="prev") {tab="gen";}
  frm.action="manage_person.cfm?id="+tab;
  if(isVaildForm()) {frm.submit();}
}

//This function submits form for department person
//The first argument is the form beeing submitted
//and the second is a string with the name of the tab to go to
function clickDPTab(frm,tab) {
  if(tab!="ttl" && tab!="con" && tab!="url" && tab!="deg" && tab!="int") {tab="ttl";}
  frm.action="department_person.cfm?id="+tab;
  if(isVaildForm()) {frm.submit();}
}

//This function submits form for department manage
//The first argument is the form beeing submitted
//and the second is a string with the name of the tab to go to
function clickDMTab(frm,tab) {
  if(tab!="gen" && tab!="msg" && tab!="mis" && tab!="mkt" && tab!="val" && tab!="ppl") {tab="gen";}
  frm.action="department_manage.cfm?id="+tab;
  frm.submit();
}
//This function submits form for division manage
//The first argument is the form beeing submitted
//and the second is a string with the name of the tab to go to
function clickDivMTab(frm,tab) {
  if(tab!="gen" && tab!="msg" && tab!="mis" && tab!="mkt" && tab!="val" && tab!="ppl") {tab="gen";}
  frm.action="division_manage.cfm?id="+tab;
  frm.submit();
}
//This function submits form for center-institute manage
//The first argument is the form beeing submitted
//and the second is a string with the name of the tab to go to
function clickInstTab(frm,tab) {
  if(tab!="gen" && tab!="msg" && tab!="mis" && tab!="mkt" && tab!="val" && tab!="ppl") {tab="gen";}
  frm.action="centinst_manage.cfm?id="+tab;
  frm.submit();
}


function validlogin(form) { 
  var field = form.login;  
  var loginID = field.value;   
  if (!loginID) {
    alert("You must enter login (email id).");
	field.focus();
    return false;
  }
}


function validreg(f) { 
  var msg;
  var empty_fields="";
  //loop thru elements of the form
  for(var i=0;i<f.length;i++)
  {
    var e=f.elements[i];
    if(e.type=="text")
    {
	  //check if field is empty
	  if(e.value=="")
	  	{
	  	empty_fields +="\n            "+e.name;
		continue;
		}
  	}
  }
  if(!empty_fields) return true;
  if(empty_fields){
    msg ="The following required field(s) are empty:"+ empty_fields + "\n";
  }
  alert(msg);
  return false;
}

function validtxt(form) { 
  var field = form.Name;  
  var Nm = field.value;   
  var Nm_len = Nm.length;
  //alert(Nm_len);
  if (!Nm) {
    alert("You must enter Name, Email or Title.");
	field.focus();
    return false;
	}
  if (Nm_len == 1) {
    alert("You must enter more than one character.");
	field.focus();
    return false;
	}
}

function validsrchtxt(form) {
    if (form.checkbox_nm.checked == false &&
	    form.checkbox_ttl.checked == false &&
		form.checkbox_eml.checked == false &&
		form.checkbox_ptype.checked == false &&
		form.checkbox_adm.checked == false &&
		form.checkbox_div.checked == false &&
		form.checkbox_cinst.checked == false &&
	    form.checkbox_aca.checked == false) {
        alert("You must choose at least one check box.");
        return false;
	}
	else {
	  if (form.checkbox_aca.checked == true && form.selectaca.selectedIndex == 0) {
          alert("You must select one of the Departments.");
		  return false;
	  }
	  if (form.checkbox_nm.checked == true && !form.Name.value) {
	    alert("You must enter Name if you checked the Name.");
		return false;
	  }	
	  if (form.checkbox_ttl.checked == true && !form.Title.value) {
	    alert("You must enter Title if you checked the Title.");
		return false;
	  }	
	  if (form.checkbox_eml.checked == true && !form.Email.value) {
	    alert("You must enter Email if you checked the Email.");
		return false;
	  }
	}			
}

function validinsttxt(form) { 
  if (!form.Inst.value) {
    alert("You must enter Faculty Interest.");
	form.Inst.focus();
    return false;
	}
  if (form.Inst.value.length == 1) {
    alert("You must enter more than one character.");
	form.Inst.focus();
    return false;
	}
}

//This function prompts the user if they try to delete a Education
//cb is the checkbox form item
//strFName, strLName are string for the first and last name
//objDeg, objIns, objMajor, objYear are the form items Degree, Instutie, major and Year
function delEduPrompt(cb,strFName,strLName,objDeg,objIns,objMaj,objYear) {
  if(cb.checked == true) {
    var strDeg=objDeg.options[objDeg.selectedIndex].text;
    var strIns=objMaj.value;
    var strYear=objYear.value;
	var msg="You have elected to Delete this Degree Information";
	msg +="\n First Name: "+strFName;
	msg +="\n Last Name: "+strLName;
	msg +="\n Degree: "+strDeg;
	msg +="\n Intitute: "+strIns;
	msg +="\n Year: "+strYear;
	msg +="\n Click 'OK' to mark for deletion or 'Cancel' not to";
	if(confirm(msg) == false) {cb.checked = false;}
  }
  setProcessFlag(cb);
}

//This function prompts the user if they try to delete a Office Contact
//cb is the checkbox form item
//strFName, strLName are string for the first and last name
//objPhn, objBld, objHrs, are the form items Phone, Building (pulldown box), Hours (textarea)
function delConPrompt(cb,strFName,strLName,objPhn,objBld,objHrs) {  
  if(cb.checked == true) {
    var strPhn=objPhn.value;
	var strBld=objBld.options[objBld.selectedIndex].text;
    var strHrs=objHrs.value;
	var msg="You have elected to Delete this Contact Information";
	msg +="\n First Name: "+strFName;
	msg +="\n Last Name: "+strLName;
	msg +="\n Phone Number: "+strPhn;
	msg +="\n Building: "+strBld;
	msg +="\n Office Hours: "+strHrs;
	msg +="\n Click 'OK' to mark for deletion or 'Cancel' not to";
	if(confirm(msg) == false) {cb.checked = false;}
  }
  setProcessFlag(cb);
}

//This function will take out thing that may cause problems if then are writen in a
//HTML document, or in a HTML form item
function niceHTML(x) {  //merrored in form.js
  var ret=x.replace("'","^@39$");  
  ret=ret.replace('"',"^@34$");
  ret=ret.replace("<","^@60$");
  return ret;
}

// This function will allow us to write unsafe things into the data base safely
function niceDatabase(x) {
  var ret=x.replace("'","^@39$");
  ret=ret.replace('"',"^@34$");
  ret=ret.replace("<","^@60$");
  return ret;
}

// This function will allow us to write unsafe things into the url safely
function niceURL(X) {
	var ret=niceDataBase(x);
	ret=ret.replace("&","^@38$");
	ret=ret.replace("?","^@63$");
	return ret;
}


//This Function simplifies usage of niceHTML from within a form
function setFormItem(fm_obj1, frm_obj2){
     frm_obj1=niceHTML(frm_obj1);
	 frm_obj2=niceHTML(frm_obj2);
}


//This function take in 5 arrguments
//  1 String: x
//  3 boolean: letL, letU, letN
//  1 String: list
//  This funtion will return a string that is same as x, but with modification
//    if letL is true then lower case letters will be included in the output, else not
//    if letU is true then upper case letters will be included in the outout, else not
//    if letN is true then number will be included in the output, else not
//    And any charters found in list will be included
//  NOTE onlyThes("abc",true,false,false,"b") will return "abbc"
//  NOTE onlyThes("a.c",true,false,false,".-.") will return "a..c"
function onlyThese(x,letL,letU,letN,list) {
  var ret="";
  var l=x.length;
  var ll=list.length;
  var crLet='';
  var i;
  for(i=0;i<l;i++) {
    crLet=x.charAt(i);
	if(letL == true && crLet >= 'a' && crLet <= 'z') {ret+=crLet;}
	if(letU == true && crLet >= 'A' && crLet <= 'Z') {ret+=crLet;}
	if(letN == true && crLet >= '0' && crLet <= '9') {ret+=crLet;}
	for(j=0;j<ll;j++) {
		if(crLet==list.charAt(j)) {ret+=crLet;}
	}
  }
  return ret;
}

//This function is simaler to onlyTest, but hear it will allow, all charaters, but the ones in list
function notThese(x,list) { // not mirrored
  var ret="";
  var l=x.length;
  var crLet='';
  var i;
  for(i=0;i<l;i++) {
    crLet=x.charAt(i);
    if(list.indexOf(crLet) < 0) {ret+=crLet;}
  }
  return ret;
}

// This function will return true is the string x no shorter then minLen and no longer then maxLen
// and has no singal or double quotes or lesthen signs
function isValidGeneric(x, minLen, maxLen) {
	if(x.indexOf("'") >=0) {return false;}
	if(x.indexOf(qut) >=0) {return false;}
	if(x.indexOf("<") >=0) {return false;}
	if(x.length < minLen) {return false;}
	if(x.length > maxLen) {return false;}
	return true;
}

// this function returns true if x looks like it could be a vaild phone number
function isValidPhone(x) {
  // looking for 1-(xxx)-xxx-xxxx or 0x{0,18}
  if(x.substring(0,1) == "0") {
    if(x != onlyThese(x,false,false,true,"-()")) {return false;}
	if(x.length > 18) {return false;}
	return true;
  }
  if(x.substring(0,3) != "1-(") {return false;}
  if(x.substring(6,8) != ")-") {return false;}
  if(x.substring(11,12) != "-") {return false;}
  if(x.length !=16) {return false;}
  var crLet='';
  var i;
  for(i=3;i<6;i++) {
    crLet=x.charAt(i);
	if(crLet <'0' || crLet >'9') {return false;}
  }
  for(i=8;i<11;i++) {
    crLet=x.charAt(i);
	if(crLet <'0' || crLet >'9') {return false;}
  }
  for(i=12;i<16;i++) {
    crLet=x.charAt(i);
	if(crLet <'0' || crLet >'9') {return false;}
  }
  return true;
}

//this function returns true if the string x is not shorter then minLen or longer then maxLen
//and if it looks like a number
function isValidNumber(x, minLen, maxLen) {
    if(x.substr(0,1) != onlyThese(x.substr(0,1),false,false,true,"-")) {return false;}
    if(x.substr(1,x.length) != onlyThese(x.substr(1,x.length),false,false,true,"")) {return false;}
	return isValidGeneric(x, minLen, maxLen);
	return true;
}

//This function is just like the one above, but only alows numbers >=0
function isValidCountingNumber(x, minLen, maxLen) { // >=0
    if(x != onlyThese(x,false,false,true,"")) {return false;}
	return isValidGeneric(x, minLen, maxLen);
}

//This function returns true if x is an empty sting or a number that falls in or on minVal and maxVal
function isValidNumberValue(x, maxVal, minVal) {
    if(x > maxVal) {return false;}
    if(x < minVal) {return false;}
	return true;
}

//This function returns true if x is a string that looks like a zipcode 
function isValidZipCode(x) {
    // looking for xxxxx or xxxxx-xxxx
	if(x.length<5) {return false;}
	var crLet='';
	var i;
	for(i=0;i<5;i++) {
	  crLet=x.charAt(i);
	  if(crLet <'0' || crLet >'9') {return false;}
	}
	if(x.length == 5) {return true;}// note differentce
	if(x.length != 10) {return false;}
	if(x.charAt(5) != '-') {return false;}
	for(i=6;i<10;i++) {
	  crLet=x.charAt(i);
	  if(crLet <'0' || crLet >'9') {return false;}
	}
	return true; 
}

//This function return true if x is a string that looks like a year
function isValidYear(x) {
  // looking for 19xx or 2xxx
  if(x != onlyThese(x,false,false,true,"")) {return false;}
  if(x.length == 4) {
    if(x.substr(0,2) == "19") {return true;}
    if(x.substr(0,1) == "2") {return true;}
  }
  return false;
}
//This function reture true if x is a string that looks like a email
function isValidEmail(x) {
  // looking for x@x.x
  // it will take x.x@x
  if(x.length < 5) {return false;}
  indexAt=x.indexOf('@');
  indexDot=x.indexOf('.');
  if(indexAt <= 0) {return false;}
  if(indexDot <= 0) {return false;}
  return true;
}

//These functions are used when people make a change to a form.  It will look
//for invaild data and prompt for corestions
//and then set the ProccessMe form item

//obj is the form item that change
//type is a string discribing it
function chnTextField(obj,type) {
  setProcessFlag(obj);
  str=obj.value;
  tmp=notThese(str,"'<"+qut);
  if(str != tmp) {
    mes ="Bad "+type;
	mes +="\nYou provided: "+qut+str+qut;
	mes +="\nDid you mean "+qut+tmp+qut+"?";
	mes +="\nIf so click 'Yes', if not click 'Cancel'";
	if(confirm(mes)) {
	  obj.value=tmp;
	} else {
	  obj.value="";
	}
  }
}

//obj is the form item that change
//type is a string discribing it
function chnUrl(obj,type) {
  setProcessFlag(obj);
  str=obj.value;
  tmp=notThese(str,"'<"+qut);
  if(tmp.substr(0,4) !="http") {tmp="http://"+tmp;}
  if(tmp.length >= obj.maxLength) {tmp=tmp.substr(0,obj.maxLength);}
  if(str != tmp) {
	mes ="Bad "+type;
	mes +="\nYou provided: "+qut+str+qut;
	mes +="\nDid you mean "+qut+tmp+qut+"?";
	mes +="\nIf so click 'Yes', if not click 'Cancel'";
	if(confirm(mes)) {
	  obj.value=tmp;
	} else {
	  obj.value="";
	}
  }
}

//obj is the form item that change
//type is a string discribing it
function chnPhone(obj, type) {
  setProcessFlag(obj);
  str=obj.value;
  l=str.length;
  crLet = '';
  var i;
  var j;
  if(l != 0) {
  	if(str.charAt(0)=='0') {
	  tmp=onlyThese(str,false,false,true,"-()");
	} else {
      i=0;
	  if(str.charAt(0)=='1') {i=1;}
  	  tmp="1-(";
      j=0;
	  while(j<3&&i<l) {
	    crLet=str.charAt(i);
  	    if(crLet >= '0' && crLet <= '9') {j++; tmp+=crLet;}
  	    i++;
	  }
	  tmp+=")-";
	  j=0;
	  while(j<3&&i<l) {
	    crLet=str.charAt(i);
	    if(crLet >= '0' && crLet <= '9') {j++; tmp+=crLet;}
	    i++;
	  }
	  tmp+="-";
	  j=0;
	  while(j<4&&i<l) {
	    crLet=str.charAt(i);
	    if(crLet >= '0' && crLet <= '9') {j++; tmp+=crLet;}
	    i++;
	  }
	}
	if(str != tmp || (str.charAt(0)!= '0' && tmp.length != 16)) {
	  mes ="Bad "+type;
	  mes +="\nPhone number should be in this format 1-(xxx)-xxx-xxxx";
	  mes +="\nFor international numbers please start with '0-'";
      mes +="\nYou provided: "+qut+str+qut;
	  if(tmp.length == 16) {
	    mes +="\nDid you mean "+qut+tmp+qut+"?";
		mes +="\nIf so click 'Yes', if not click 'Cancel'";
		if(confirm(mes)) {
	      obj.value=tmp;
	  	} else {
		  obj.value="";
	  	}
	  } else {
	    mes += "\nPlease use the next box to complete the phone number";
		alert(mes);
		tmp=prompt("Please complete the phone number, if you do not know it, please press 'Cancel'",tmp);
        if(tmp) {
		  obj.value=tmp;
		  if(tmp.length > 0 && !isValidPhone(tmp)) {chnPhone(obj, type);}
		} else {
		  obj.value="";
		}		
	  }
	}
  }
}

//obj is the form item that change
//type is a string discribing it
function chnZipCode(obj) {
  setProcessFlag(obj);
  str=obj.value;
  l=str.length;
  tmp="";
  crLet='';
  i=0;
  j=0;
  while(j<5&&i<l) {
    crLet=str.charAt(i);
    if(crLet >= '0' && crLet <= '9') {j++; tmp+=crLet;}
    i++;
  }
  if(l>5) {
    tmp+="-";
    j=0;
    while(j<4&&i<l) {
      crLet=str.charAt(i);
      if(crLet >= '0' && crLet <= '9') {j++; tmp+=crLet;}
      i++;
    }
  }
  if(tmp!=str || (tmp.length!=5 && tmp.length!=10)) {
    mes ="Bad Zip Code";
    mes +="\nZip Codes should be in this format xxxxx or xxxxx-xxxx";
    mes +="\nYou provided: "+qut+str+qut;
	if(tmp.length==5 || tmp.length==10) {
	  mes +="\nDid you mean "+qut+tmp+qut+"?";
	  mes +="\nIf so click 'Yes', if not click 'Cancel'";
	  if(confirm(mes)) {
	    obj.value=tmp;
	  } else {
		obj.value="";
	  }
	} else {
	  mes += "\nPlease use the next box to complete the Zip Code";
	  alert(mes);
	  tmp=prompt("Please complete the Zip Code, if you do not know it, please press 'Cancel'",tmp);
      if(tmp) {
		obj.value=tmp;
		if(tmp.length > 0 && !isValidZipCode(tmp)) {chnZipCode(obj);}
	  } else {
		obj.value="";
	  }
	}
  }
}

//obj is the form item that change
//type is a string discribing it
function chnYear(obj,type) {
  setProcessFlag(obj);
  str=obj.value;
  l=str.length;
  var i;
  var j;
  var tmp;
  var crLet;
  var mes;
  if(l == 1) {
    if(str.charAt(0) >= '0' && str.charAt(0) <='9') {tmp="200"+str;}
  }
  else {
    tmp="";
    j=0;
    if(l == 2 && parseInt(str)) {
      if(parseInt(str)>30) {
	    tmp="19";
		j=2;
  	  } else {
	    tmp="20";
		j=2;
	  }
    }
	i=0;
    crLet='';
    while(j<4&&i<l) {
      crLet=str.charAt(i);
      if(crLet >= '0' && crLet <= '9') {j++; tmp+=crLet;}
      i++;
    }
  }
  if(str != tmp || tmp.length!=4 ) {
    mes ="Bad "+type;
    mes +="\nYears should be in this format 19xx or 20xx";
    mes +="\nYou provided: "+qut+str+qut;
	if(tmp.length==4) {
	  mes +="\nDid you mean "+qut+tmp+qut+"?";
	  mes +="\nIf so click 'Yes', if not click 'Cancel'";
	  if(confirm(mes)) {
	    obj.value=tmp;
	  } else {
		obj.value="";
	  }
	} else {
	  mes += "\nPlease use the next box to complete the Year";
	  alert(mes);
	  tmp=prompt("Please complete the Year, if you do not know it, please press 'Cancel'",tmp);
      if(tmp) {
		obj.value=tmp;
		if(tmp.length > 0 && !isValidYear(tmp)) {chnYear(obj,type);}
	  } else {
		obj.value="";
	  }
	}    
  }
}

//obj is the form item that change
//type is a string discribing it
function chnExt(obj,type) {
  setProcessFlag(obj);
  var str=obj.value;
  var tmp=onlyThese(str,false,false,true,"");
  if(str != tmp) {
    mes ="Bad "+type;
	mes +="\nYou provided: "+qut+str+qut;
	mes +="\nDid you mean "+qut+tmp+qut+"?";
	mes +="\nIf so click 'Yes', if not click 'Cancel'";
	if(confirm(mes)) {
	  obj.value=tmp;
	} else {
	  obj.value="";
	}
  }
}

//obj is the form item that changed
function chnEmail(obj) {
  setProcessFlag(obj);
  var str=obj.value;
  if(!isValidEmail(str)) {
  	mes ="Bad Email";
  	mes +="\nYou provided: "+qut+str+qut;
  	mes +="\nWe are looking for email in this form x@x.x";
  	alert(mes);
  	obj.value="";
  }
}

//obj is the form item that changed,
//chng is the form item taht is the flag for changed item
/*
Note Used any more
function chnTitile(obj, chng) {
  setProcessFlag(obj);
  if(false) { // add check for vaild titile  	
  } else {chng.value="true";}
}
*/

//this function will strip the punctuation from a phone number "-","(",")" and the leading 1 if there is one
function unMakePhone(x) {
	var ret=notThese(x,"-()");
	if(ret.substr(0,1) == "1") {return last(ret,ret.length-1);}
	return ret;
}

//This function is used to check that a form have vaild data in it
//Not that the data has bad charters in it, but things like requiered fileds
//right now it only set for person forms.
//formObj shuld be a global JavaScript variable see docs for notes on formObj
function isVaildForm() {
  //alert(formObj+"\n"+formObj.length);
  var i;
  var j;
  var k;
  var rep;
  var fnd;
  if(!formObj) {
    alert("FormObj not defined");
	return false;
  }
  var l=formObj.length;
  for(i=0;i<l;i++) {formObj[i][1].className="normal";}
  for(i=0;i<l;i++) {
    switch (formObj[i][0]) {
	case "dD":
	  j=i-formObj[i][3];	  
	  while(j>=0){	    
	    rep=true;
		fnd=false;
	    for(k=0;k<formObj[i][3];k++) {
		  if(formObj[j+k][0]==formObj[i+k][0]&&formObj[j+k][2]==formObj[i+k][2]) {
		    fnd=true;
 		    if(formObj[j+k][1].value != formObj[i+k][1].value) {rep=false;}
		  }
		}
		if(rep&&fnd) {
		  for(k=0;k<formObj[i][3];k++) {formObj[i+k][1].className="excite";}
		  alert("for "+formObj[i][2]+" there is a degree that has been repeated");
		  return false;
		}
		j=j-formObj[i][3];
	  }
    break
	case "dI":
	  if(formObj[i][1].value == ""  ) {
	    for(j=1;j<formObj[i][3];j++) {
		  if(formObj[i+j][1].value !="" ) {
  		    for(k=0;k<formObj[i][3];k++) {formObj[i+k][1].className="excite";}
		    alert("for "+formObj[i][2]+"'s degree to be added the Institute must be filled in");			
			return false;
		  }
		}
	  } else {
  	    for(j=1;j<formObj[i][3];j++) {
		  if(formObj[i+j][1].value =="" ) {
		    for(k=0;k<formObj[i][3];k++) {formObj[i+k][1].className="excite";}
		    alert("for "+formObj[i][2]+"'s degree from "+formObj[i][1].value+" to be added the Concentration and Degree Year must be filled in");
			return false;
		  }
		}
	  }
	break	
	case "pP*":
	  if(formObj[i][1].value == ""  ) {
	    for(j=1;j<formObj[i][3];j++) {
		  if(formObj[i+j][1].value !="" ) {
		    for(k=0;k<formObj[i][3];k++) {formObj[i+k][1].className="excite";}
		    alert("To save "+formObj[i][2]+"'s Post Doctorate the Position must be filled in");
			return false;
		  }
		}
	  } else {
  	    for(j=1;j<formObj[i][3];j++) {
		  if(formObj[i+j][1].value =="" ) {
		    for(k=0;k<formObj[i][3];k++) {formObj[i+k][1].className="excite";}
		    alert("T7o save "+formObj[i][2]+"'s Post Doctorate at "+formObj[i][1].value+" the Year must be filled in");
			return false;
		  }
		}
		fnd=false;
		for(j=i;j>=0;j--) {
		  if(formObj[j][0]=="dI" && formObj[j][1].value != "" && formObj[j][2]==formObj[i][2]) {fnd=true;}
		}
		if(!fnd) {
		  for(k=0;k<formObj[i][3];k++) {formObj[i+k][1].className="excite";}
		  alert("To save "+formObj[i][2]+"'s Post Doctorate at "+formObj[i][1].value+" there must be a Degree first");
		  return false;
		}
	  }
	break
	case "PK*":
	  if(formObj[i][1].value == ""  ) {
	    for(j=i-formObj[i][3];j<i+formObj[i][4];j++) {
		  if(formObj[j][1].value !="") {formObj[i][1].value="1-(000)-000-0000";}
		}
	  }
	  for(j=i-1;j>=0;j--) {
	    if(formObj[j][0]=="PK*") {
	      if(formObj[j][1].value==formObj[i][1].value && formObj[j][2]==formObj[i][2]) {
	        formObj[i][1].className="excite";
	        alert(formObj[i][2]+" may not use the same Phone number for more then one office.");
	        return false;
	      }
	    }
	  }
	break
	default : ;
	}
  }
  for(i=0;i<l;i++) {
	if(formObj[i][1].value) {formObj[i][1].value=niceDatabase(formObj[i][1].value);}
  	if(formObj[i][0] == "PK*" || formObj[i][0] == "hecf" || formObj[i][0] == "hp") {
		formObj[i][1].value=unMakePhone(formObj[i][1].value);
	}  	
  }
  return true;
}

//This function is used to open a new window so that
//People can make request to have data change that is in Banner or FIS
function newWindowChnageData(type,targetID,targetN,old,maxSize) {
  //maxSize is the size of the the text box that will be provided
  var url="../correct/data_change.cfm";
  url +="?chg_type="+type;
  url +="&chg_targetID="+targetID;
  url +="&chg_targetN="+targetN;
  url +="&chg_old="+old;
  url +="&chg_maxSize="+maxSize;
  var setting="width=500, height=300, location=no, menubar=no, toolbar=no, resizable=yes, scrollbars=yes";
  var changeWindow = window.open(url,"",setting);
}
//This function is used to open a new window so that
//People can make request to have data deleted that is in FIS
function newWindowDeleteData(type,targetID,targetN,old) {
  //This is a delete, so there is not text box so maxSize is not needed
  var url="../correct/data_delete.cfm";
  url +="?del_type="+type;
  url +="&del_targetID="+targetID;
  url +="&del_targetN="+targetN;
  url +="&del_old="+old;
  var setting="width=400, height=200, location=no, menubar=no, toolbar=no, resizable=no, scrollbars=yes";
  var changeWindow = window.open(url,"",setting);
}

function subSelect_mas(obj,slv) {  
  if(obj.options[obj.selectedIndex].text.indexOf('(') > 0) {
    var i=0;
    var ktext=obj.options[obj.selectedIndex].text.substring(obj.options[obj.selectedIndex].text.indexOf('('));
    for(i=0;i<slv.options.length;i++) {
	  if(ktext=="("+slv.options[i].value+")") {
	    slv.selectedIndex=i;
		break;
	  }
	}
  }
  setProcessFlag(obj);
}

function subSelect_slv(obj,mas) {
  var i=0;  
  for(i=0;i<mas.options.length;i++) {
    if(last(mas.options[i].text,2+obj.value.length)=="("+obj.value+")") {
	  mas.selectedIndex=i;
	  break;
	}
  }
}
function checkComment(form,x,y,z,tab,cat) {
	//y = y.replace([/^\n^\f^\l/], ' ');
    if ((x=="") && (y!=z) && z!="[SUPER]") {
	  //Now you will only get the message if the textbox value != the sneak value
	  //  and there is Nothing in the comment field
	  //  and sneak value != [SUPER] (sneak = [SUPPER] when you are the NJMSCD or NJMSSA)
	  alert("please enter a comment explaining your change");
	}
	else{ 
		switch(cat) {
		case "dept": clickDMTab(form, tab);
			  		break;
		case "inst": clickInstTab(form, tab);
					break;
		case "div": clickDivMTab(form, tab);
					break
		}
	 }
	}
	
function previewChange(obj) {
    var setting="";
  var p=45; // Percentage of window
  var f=10; // Fudge factor to account for the top
  
  if(window.screen) {
    if(window.screen.width) {setting +="width="+window.screen.width*(p-f)/100.0;}
    if(window.screen.height) {
      if(setting.length>0) {setting+=", ";}
      setting +="height="+window.screen.height*(p-f)/100.0;
    }
  }  
  if(setting.length>0) {
    setting+=", location=no";
    setting+=", menubar=no";
    setting+=", toolbar=no";
    setting+=", resizable=no";
    setting+=", scrollbars=no";
  }  
  var newWindow = window.open("","",setting);
  var text="<p>"+obj.value+"</p>";
  var i=text.indexOf("[C]");
  var more=false;
  while(i>-1) {
    text=text.substring(0,i)+"<span class='highlight'>"+text.substring(i+3);
    i=text.indexOf("[C]");
  }
  i=text.indexOf("[CEND]");
  while(i>-1) {
    text=text.substring(0,i)+"</span>"+text.substring(i+6);
    i=text.indexOf("[CEND]");
  }
  i=text.indexOf("[B]");
  while(i>-1) {
    text=text.substring(0,i)+"<b>"+text.substring(i+3);
    i=text.indexOf("[B]");
  }
  i=text.indexOf("[BEND]");
  while(i>-1) {
    text=text.substring(0,i)+"</b>"+text.substring(i+6);
    i=text.indexOf("[BEND]");
  }
  i=text.indexOf("[L]");
  while(i>-1) {
    text=text.substring(0,i)+"<ul><li>"+text.substring(i+3);
    i=text.indexOf("[L]");
  }
  i=text.indexOf("[LEND]");
  while(i>-1) {
    text=text.substring(0,i)+"</li></ul>"+text.substring(i+6);
    i=text.indexOf("[LEND]");
  }
  i=text.indexOf("[p]");
  while(i>-1) {
    text=text.substring(0,i)+"</p><p>"+text.substring(i+3);
    i=text.indexOf("[p]");
  }
  i=text.indexOf("[MORE]");
  while(i>-1) {
    text=text.substring(0,i)+text.substring(i+6);
	more=true;
    i=text.indexOf("[MORE]");
  }
  if(more) {text +="<br><i>A link will appear for more information</i>";}
  newWindow.document.write("<link href='../style.css' rel='stylesheet' type='text/css'>");
  newWindow.document.write(text);
  newWindow.document.close();
}

function checkContact(form,x,a,b,c,url,d,tab,cat) {
	//y = y.replace([/^\n^\f^\l/], ' ');
	if ((url)&&(url.substr(0,4) !="http"))  {
	  alert("please type the full web adress, beginning with 'http'");
	  return;
	} 
    if (   (x=="None") && ( (a=="")||(b=="")||(c=="") )   )	{
	  alert("Please enter phone number, email, and building name if not using a primary contact");
	} else if (c=="") {
	  alert("Please enter a building name");
	} else{
		if (!isValidPhone(a) && a.length>0 ){
			  alert("please enter a valid phone number\nIt should be in this format 1-(xxx)-xxx-xxxx");
			  return;
			  } 
		if (!isValidPhone(d) && a.length>0){
			  alert("please enter a valid fax number\nIt should be in this format 1-(xxx)-xxx-xxxx");
			  return;
			  } 
		switch(cat) {
		case "dept":form.DeptConPhone.value = unMakePhone(a); 
					form.DeptConFax.value = unMakePhone(d);
					clickDMTab(form, tab);
			  		break;
		case "inst":form.CentInstConPhone.value = unMakePhone(a);
					form.CentInstConFax.value = unMakePhone(d);
					clickInstTab(form, tab);
					break;
		case "div": form.DivConPhone.value = unMakePhone(a);
					form.DivConFax.value = unMakePhone(d);
					clickDivMTab(form, tab);
					break
		}
	 }
	
	}