
// Strip out these characters: < > " ' % ; ) ( & +
function RemoveBad(strTemp) { 
	strTemp = strTemp.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\-/g,""); 
return strTemp;
} 

function validate(frm) {
	
	var ErrorMessage = new String("") ;	
	
	frm.BrowserType.value = navigator.appName + " " + navigator.appVersion ;
	
	if(trim(frm.Username.value).length == 0){
		
		valid = false;
		ErrorMessage += "Please enter a username.\n";
		
	} else {
	
		if(trim(frm.Username.value).length < 6){
			valid = false;
			ErrorMessage += "Your username must contain at least 6 characters.\n";
		}
		
		if( !AlphaNumeric(trim(frm.Username.value)) ){
			valid = false;
			ErrorMessage += "The username you entered contains an invalid character.\n";
		}
		
	}
	
	if (RemoveBad(frm.Firstname.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter your other name.\n";
    }		
	if (RemoveBad(frm.Familyname.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter your family name.\n";
    }	
	
	if (RemoveBad(frm.Familiarname.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter your personal name.\n";
    }	
	
	if (frm.Password1.value.length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter a password.\n";
    } else {
		
		if (frm.Password1.value.length < 6) {
			valid = false;
			ErrorMessage += "The password must be at least 6 characters long.\n";
		}
		
		if( !AlphaNumeric(trim(frm.Password1.value)) ){
			valid = false;
			ErrorMessage += "The password you entered contains an invalid character.\n";
		}
		
	}	
	
	if (!validEmail(frm.Email1.value)) {
		 valid = false;
		 ErrorMessage += "Please enter a valid email address.\n";
    }
	if (frm.Password1.value != frm.Password2.value) {
		valid = false;
		ErrorMessage += "The passwords do not match.\n";
		frm.Password2.value = "";
	}	
		
	if (RemoveBad(frm.Email1.value) != RemoveBad(frm.Email2.value)) {
		valid = false;
		ErrorMessage += "Oops, the emails do not match.\n"; 
		frm.Email2.value = "";
	}		
	
	// Check that country has been selected
	if(0 == frm.selCountry.options[frm.selCountry.selectedIndex].value){
		valid = false;
		ErrorMessage += "Please select your country.\n"; 
	}
	
	// check that age group has been selected
	blnRadOK = false;
	
	for (var i=0;i<frm.radAgeGroup.length;i++) {
        if (frm.radAgeGroup[i].checked){
            blnRadOK = true;
		}
    }
	
	if(!blnRadOK){
		valid = false;
		ErrorMessage += "Please select your age group.\n"; 
	}

	// check that sex has been selected
	blnRadOK = false;
	
	for (var i=0;i<frm.radSex.length;i++) {
        if (frm.radSex[i].checked){
            blnRadOK = true;
		}
    }
	
	if(!blnRadOK){
		valid = false;
		ErrorMessage += "Please select your sex.\n"; 
	}
	
	if(frm.radResident[0].checked==false && frm.radResident[1].checked==false) {
		valid = false;
		ErrorMessage += "Please select your residency status.\n"; 		
	}
	
	// check that a PLAN has been selected
	blnRadOK = false;
	
	for (var i=0;i<frm.ProductID.length;i++) {
        if (frm.ProductID[i].checked){
            blnRadOK = true;
		}
    }
	
	if(!blnRadOK){
		valid = false;
		ErrorMessage += "Please select a pricing plan.\n"; 
	}

	
	if(ErrorMessage.length > 1)
	{
		alert(ErrorMessage) ;
		return false ;
	}
	//return true ;
	document.frm.submit(); 
}


function validateExpressionOfInterestForm(frm) {
	var ErrorMessage = new String("") ;
		
	if (RemoveBad(frm.Name.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter your Name\n";
    }
	if (RemoveBad(frm.ReplyEmail.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter your Email Address\n";
    }		
	// Check that country has been selected
	if(0 == frm.Country.options[frm.Country.selectedIndex].value){
		valid = false;
		ErrorMessage += "Please select your Country\n"; 
	}
	if (RemoveBad(frm.Topic.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter a Topic\n";
    }
	if (ErrorMessage.length > 1) 	{
		alert(ErrorMessage) ;
		return false ;	
	}
	return true ;
}



function validateEnquiryForm(frm) {
	var ErrorMessage = new String("") ;
		
	if (RemoveBad(frm.Name.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter your Name\n";
    }
	if (RemoveBad(frm.ReplyEmail.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter your Email Address\n";
    }		
	// Check that country has been selected
	if("0" == frm.Country.options[frm.Country.selectedIndex].value){
		valid = false;
		ErrorMessage += "Please select your Country\n"; 
	}
	if (RemoveBad(frm.Topic.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter a Topic\n";
    }	
	if (RemoveBad(frm.EnquiryText.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter a Comment\n";
    }	
	if (ErrorMessage.length > 1) 	{
		alert(ErrorMessage) ;
		return false ;	
	}
	return true ;
}

function validateGiveFeedbackForm(frm) {
	var ErrorMessage = new String("") ;
		
	if (RemoveBad(frm.Name.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter your Name\n";
    }
	if (RemoveBad(frm.ReplyEmail.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter your Email Address\n";
    }		
	if (RemoveBad(frm.Topic.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter a Topic\n";
    }	
	if (RemoveBad(frm.EnquiryText.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter a Comment\n";
    }	
	if (ErrorMessage.length > 1) 	{
		alert(ErrorMessage) ;
		return false ;	
	}
	return true ;
}


function validateTellaFriend(frm) {
	var ErrorMessage = new String("") ;	

	if (RemoveBad(frm.NameOfFriend.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter your friend's name\n";
    }	
	if (RemoveBad(frm.DirectTo.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter your friend's email address\n";
    }
	
	if (RemoveBad(frm.fromName.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter your name so your friend will know who sent them this email\n";
    }	
	
	if (RemoveBad(frm.EnquiryText.value).length == 0) {
		 valid = false;
		 ErrorMessage += "Please enter a message\n";
    }		
	if (ErrorMessage.length > 1) 	{
		alert(ErrorMessage) ;
		return false ;	
	}
	return true ;
}

function validateLogin(f) {	
	var ErrorMessage = new String("") ;		
	f.BrowserType.value = navigator.appName + " " + navigator.appVersion ;
	
	if (RemoveBad(f.StudentUsername.value).length == 0) {
		ErrorMessage += "- Please enter a valid Username\n" ;
	}
	if (RemoveBad(f.StudentPassword.value).length == 0 ) {
		ErrorMessage += "- Please enter your login Password\n" ;
	}	
	if(ErrorMessage.length > 1){
		alert(ErrorMessage) ;
		return false ;
	}
	return true ;
}

// login page - Warns user if a session already exists
function ConfirmLogin() {
	
	var strConfirm =  "A session is already active for this username.\n\n";
		strConfirm += "You should wait until the user logs out or their session expires.\n";  
		strConfirm += "If you are certain that the user is no longer using the session\n";
		strConfirm += "you may continue and start a new session. Doing so will\n";
		strConfirm += "terminate the existing session.";

	if(!confirm(strConfirm)){
		CancelLogic();
	} 
	
}

// login page
function CancelLogic() {    
	 location.href = '../../default.htm';	
}

//login page
function ContinueLogic () {
    if (confirm("Are you sure you want to terminate an old session and start a new session for this user?")) {	  
		if (validateLogin(loginFrm)) {
	        document.loginFrm.action = "";
			document.loginFrm.submit(); 
	    }
    return ;
	} 
}


function validateForgot(f) {	

	if(validEmail(f.txtEmailForgotten.value)){
		
		return true;
		
	} else {
		alert("Please enter your email address\n") ;
		return false ;	
	}
	
}

function openFlashWindow_Labs(theURL) { //v2.0
  window.open(theURL,'LabWindow','channelmode=0, directories=0, fullscreen=0,toolbar=no,location=yes,status=yes,menubar=no,scrollbars=yes,resizable=yes');
}

function openFlashWindow_Lessons(theURL) { //v2.0
  window.open(theURL,'FlashPopup','channelmode=0, directories=0, fullscreen=0,toolbar=no,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes');
}

function logout() {
	document.frmLogout.submit() ;
}

function gotoURL(theURL) { //v2.0
 window.location = theURL;  
}


/*  Macromedia Scripts  */

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}


function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

function trim(varString){

	var strTemp = varString.toString();
		
	if(strTemp.indexOf(" ") >= 0){
	
		// Left Trim
		for (i=0; i < strTemp.length; i++) {
			if(strTemp.charAt(i) == " "){
				strTemp = strTemp.substring(i + 1);
			}
			else{
				break
			}
		}
		
		// Right Trim
		for (i=strTemp.length - 1;  i >= 0; i--) {
			if(strTemp.charAt(i) == " "){
				strTemp = strTemp.substring(0, i);
			}
			else{
				break
			}
		}
	}
	
	return strTemp
}


function validEmail(email) {

	invalidChars = " /:,;"

	if (email == "") {
	return false
	}

	for (i=0; i<invalidChars.length; i++) {
	badChar = invalidChars.charAt(i)
	if (email.indexOf(badChar,0) > -1) {
	return false
	}
}	
	atPos = email.indexOf("@",1)
	if (atPos == -1) {
	return false
	}

	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {
	return false
	}

	if (periodPos+3 > email.length) {
	return false
	}

	return true
	}

function AlphaNumeric(varValue){
	
	AlphaNumericChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	
	if (varValue == "") {
		return false;
	}

	for (i = 0; i < varValue.length; i++) {
		strCurrentChar = varValue.charAt(i).toUpperCase();
		if (AlphaNumericChars.indexOf(strCurrentChar, 0) < 0) {
			return false;
		}
	}

	return true;
	
}


function showPos(strURL, strType){

	var intWidth = 700;
	var intHeight = screen.availHeight - 350;
	var intX = (screen.availWidth / 2) - 350;
	var intY = 50;
	var toolbar = 0;
	var scrollbar = 1;
	var menubar = 0;
	var status = 0;
	var resizable = 1;
	
	switch(strType){
		case 'practicelab':			
			intHeight = 560;
			break;
		case 'answersheet':
			menubar = 1;
			intHeight = 560;
			resizable = 0;
			break;
			case 'TellAfriend':
			menubar = 0;
			intWidth = 500;
			intHeight = 560;
			resizable = 0;
			break;	
		case 'signup':
			intWidth = 550;
			intX = (screen.availWidth / 2) - 275;
			break;
		case 'resources':
			intWidth = 720;
			intHeight = 562;
			intX = (screen.availWidth / 2) - 360;
			intY = (screen.availHeight / 2) - 281;
			scrollbar = 0;
			break;
		case 'resourcesPDF':
			intWidth = 740;
			intHeight = 590;
			intX = (screen.availWidth / 2) - 370;
			intY = (screen.availHeight / 2) - 280;
			scrollbar = 0;
			break;
		case 'tour':
			intWidth = 794;
			intHeight = 505;
			intX = (screen.availWidth / 2) - 397;
			intY = (screen.availHeight / 2) - 250;
			scrollbar = 0;
			break;		
	}
		
	window.open(strURL,"popUpWin","height="+ intHeight +",width="+ intWidth +",left="+ intX +",top="+ intY +",toolbar="+toolbar+",scrollbars="+scrollbar+",menubar="+menubar+",status="+status+",resizable="+resizable+"");
}

