/*********************************************************************
	17/11/09	From Waypoint


*********************************************************************/

function CheckFrame() {

	var strURL = "MainTimer.asp";
	
	if (self.parent.frames.length != 0)	{}
	else	{location.href = strURL;}

}


function Refresh()	{

	document.getElementById("frmLogin").submit();
}

function isNotEmpty(elem){
	var str = elem.value;
	var re = /.+/;
	if (!str.match(re)){
		return false;
	} else {
		return true;
	}
}
	
function checkForm(form){
	// Laboratory Number
	if (isNotEmpty(form.lab)){

		//Username
		if (isNotEmpty(form.username)){
			
			//Password
			if (isNotEmpty(form.password)){
				
				//Grade
				if (checkGrade(form)){
						
					//City
					if (checkCity(form)){
						return true;
						
					} else { alert("Please choose a city in the UK or enter the name of your country in the box below."); }
						
				} else { alert("Please enter your grade"); }
					
			} else { alert("Please fill in your password"); }
				
		} else { alert("Please fill in your username"); }
			
	} else { alert("Please fill in your laboratory number"); }
		
	return false;		
}
	
function checkGrade(form){
	if (isNotEmpty(form.gradeother)){
		return true;
	} else {
		if(isChosen(form.grade)){
			return true; 
		}
	}
	return false;
}
	
function checkCity(form){
	if (isNotEmpty(form.countryother)){
		return true;
	} else {
		if (!isChosen(form.mycity1) ){
			if (!isChosen(form.mycity2)){
				if (!isChosen(form.mycity3)){
					return false;
				}
			}
		}
	}
	return true;
}
	
	
function isChosen(select) {
	if (select.selectedIndex == 0) {
		return false;
	} else {
		return true;
	}
}
	



/***********************       Added     *******************************/

function CorrectLogin(intIsAdmin)	{
	
	if (intIsAdmin == 1)	{
		document.getElementById("frmLogin").action = "admin/AdminOpener.asp";
	}
	else	{
		document.getElementById("txtRole").value = intIsAdmin;
		document.getElementById("frmLogin").action = "Home.asp";
	}
	document.getElementById("frmLogin").submit();

}



function Reminder(strReminderPrompt, strReminderLabNo, strReminderValidEmail, strReminderEmail)	{

	var strLab = document.frmLogin.lab.value;
	var strEMailAddress = "";
	
	if (strLab.length ==0) {
		alert(strReminderLabNo);
	}
	else	{
		strEMailAddress = prompt(strReminderPrompt, "name@address.com");
		if ((strEMailAddress==null)||(strEMailAddress=="")){
			alert(strReminderEmail);
		}
		if (EMailValidation(strEMailAddress)==false){
			document.frmLogin.txtEmail.value="";
			alert(strReminderValidEmail);
		}
		else	{
			document.frmLogin.txtEmail.value = strEMailAddress;
			document.getElementById("txtAction").value = "reminder";
			document.getElementById("frmLogin").submit();
		}
	}
}

function EMailValidation(str) {

		var at="@"
		var dot="."
		
		if (str==null)	{str='';}
		
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("Invalid E-mail Address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("Invalid E-mail Addresss")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail Addresss")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail Addresss")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail Addresss")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail Addresss")
		    return false
		 }

 		 return true					
}


function Login()	{

	document.getElementById("txtAction").value = "Login";
	//document.getElementById("frmLogin").action = "Home.asp";
	document.getElementById("frmLogin").submit();

}
