// SUPPRESS FORM submission
// add onKeyPress="return checkEnter(event);" to all input fields, ie
// <input type="text" name="text1" onKeyPress="return checkEnter(event);"> 
// script from jennifermadden.com 
function checkEnter(e){
	var characterCode;
	if( e && e.which ){ //if which property of event object is supported (NN4)
		e = e;
		characterCode = e.which; //character code is contained in NN4's which property
	} else {							
		e = event;						
		characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}
 
	if( characterCode == 13 ){ //if generated character code is equal to ascii 13 (if enter key)
		return false 
	} else {
		return true 
	} 
}

//REPLACE Carriage Returns with a <br> tag.  in Real time to preserve formatting. 
//Simultaneously suppresses form submission. Script from jennifermadden.com
var nn4 = (navigator.appName.indexOf("Netscape") > -1 && navigator.appVersion.indexOf("4") > -1) ? true : false;
 
function addP( e,ta ){ // e event object passed for NN4, ta is the textarea
	var characterCode;
	if(e && e.which){  //get event object reference and code property for each browser
		e = e;
		characterCode = e.which; // NN4;
	} else {
		e = event;
		characterCode = e.keyCode; // IE;
	}	 
	
	if( characterCode == 13 ){  //if enter key is pressed
		if( nn4 ){
//			ta.value += "<br>%0D%0A"; //encoding for \r\n must be manually entered 
			ta.value = unescape(ta.value);  //must be unescaped in order for returns 
			//to be rendered inside textarea in real time
			ta.select();
			ta.focus(); //the select + focus combo forces NN4 to place cursor at end of string
			return false;
		} else { //IE
//			ta.value += "<br>"  //simply add break tag when carriage return is pressed
			return true;
		}
	} else if( characterCode == 8 && !document.getElementById ){ 
		//if backspace pressed (NN4) and browser is not NN6	 
		//IE ignores clause because this function is called onKeyPress 
		//& non alphanmerc backspace requires keydown or keyup
		ta.value = ta.value.substring(0,ta.value.length-1); 
		// unselect string & chop off last entered character
		ta.select();
		ta.focus(); //reselect & focus to force correct cursor insertion point
		return false;
	} else { //if any other character is pressed
		if( nn4 ){
			ta.value += String.fromCharCode(characterCode); 
			//NN4 renders key/character codes so they must be converted 	
			//back to plain language values for display using fromCharCode method
			ta.select();ta.focus();
			return false;
		} else {
			return true;
		}
	}	 

}

function isValidString( s ){
	var reg = /[a-zA-Z]/;
}

function isNum(num){
	var x = num.search(/^\s*\d*\s*$/); //^\s*\d*\s*$
	if( x == -1 ){
		alert( 'Please enter a number' );
		return false;
	} else {
		return true;
	}
}

/*
function is_valid_num( $x ){
	if( ereg("^[0-9-]+$", $x) ){
		return true;
	} else {
		alert ('Please enter a valid number');
		return false; 
	}	
}

 function is_valid_string( $x ){
	if( ereg("^[a-z A-Z-]+$", $x) ){
		return true;
	} else {
		return false; 
	}
}
//-----ZIP VALIDATOR-------------------------------------
function is_valid_zip( $x ){
	if( ereg("^[0-9A-Z-]+$", $x) ){
		return true;
	} else {
		return false; 
	}
}


*/
//Is a form value a  number?
function isValidNum(x){
	isNum = x.search(/^[\d\.]+$/) != -1;
	x = parseFloat(x);
	if( isNum  ){
		return true;
	} else {
		return false;
	}
}

function isValidNeg(x){
//	alert('hi');
	isNum = x.search(/^[\d\.\-\s]+$/) != -1;
//	alert(x + " " + isNum);
	x = parseFloat(x);
	if( isNum  ){
		return true;
	} else {
		return false;
	}
}


function validateRegistration(){

	var regTypes = new Array();
	var theForm = document.forms[0];
	var which = 0;
	var validate = 0;
	var chosen = 0; // how many registrant types are selected from menu.

	for(i=0; i<theForm.elements.length; i++){
		if( i >= 1 ){
			j=i-1;
			var myPrecedingValue = theForm.elements[j].value;
			var myPrecedingName = theForm.elements[j].name;
		}
		/*alert(theForm.elements[i].value);
		alert(theForm.elements[j].value);*/
		var mytype = theForm.elements[i].type;
		var myname = theForm.elements[i].name;
		var myvalue = theForm.elements[i].value;
		
		
//		alert(myPrecedingValue + ':'+ myvalue + ' ' + myname);
//		Make array of pull down menu values to check for duplicates
		if( mytype == "select-one" ){
			regTypes[which] = myvalue;
			which++;
		}

//		MAKE SURE all tuition values are numbers and not strings or spaces
		tuition = myname.search(/crs_tuition/);
		if( tuition != -1 ){
			
			if( myvalue !='' && myvalue != "complimentary" ){
				if( myvalue == '0' ){
					var hasNoFee = true;
				}
				var isMon = myvalue.search(/^\d+$/);
				myvalue = parseInt(myvalue);
				 
//				alert('myPrecedingValue: ' + myPrecedingValue);
//				alert('myPrecedingName: ' + myPrecedingName);
				if( isMon == -1 || hasNoFee == true ){
//					alert ('myvalue: ' + myvalue + ' isMon: ' + isMon);
					alert('Please make sure that any course tuition values are numerals greater than 0,  and entered in the correct format. ');
					validate++;
				}

	//			MAKE SURE each tuition is preceded by an actual registrant type			
				if( !isNaN(myvalue) ){
					if( theForm.elements[j].value == 'header'){
//						alert(theForm.elements[i].value + " :" + theForm.elements[j].value);
						alert('Please select a registrant type for each tuition number entered. ');
						validate++;
					}
				}
			}
			
		}

		var product = myname.search(/prod_price/);
		if( product != -1 ){
			var complimentary = myvalue.search(/complimentary/);
//			alert(myvalue + " " + complimentary);
			if( complimentary != 0 ){
				var x = isValidNum(myvalue);
//				alert('x: ' + x);
				if( x == false ){
					alert('Please make sure that any course tuition values are numerals. ');
					validate++;
				} else {
					if( theForm.elements[j].value == '' ){
						alert( 'Please enter a product name for every product price. ');
						validate++;
					}
				}
			}
		}

		var discount = myname.search(/discount_amt/);
		if( discount != -1 ){
			
			if( myvalue != '' ){
			var x = isValidNeg(myvalue);
//				alert('x: ' + x);
			if( x == false ){
				alert('Please make sure that any course discount values are numerals. ');
				validate++;
			} else {
				if( theForm.elements[j].value == '' ){
					alert( 'Please enter an item name for every disounted amount. ');
					validate++;
				}
			}
			}
		}

	}

//		CHECK for duplicate values in pulldown Menus
		for( i=0; i < regTypes.length; i++ ){
			
			var valueOuter = regTypes[i];
			for( j=0; j < regTypes.length; j++ ){
				var valueInner = regTypes[j];
				if( i != j ){
					if( valueOuter == valueInner && valueOuter != 'header' ){
						alert('Please do not select more than one of the same type of registrants. ');
						validate++;
						
					}
				}	
			}
		}

//		Check that there is at least one registrant type entered. 
	 	for( i=0; i < regTypes.length; i++ ){
			if( regTypes[i] == 'header' ){
				chosen++;
			}	 
		}
		if( chosen == regTypes.length ){
			alert('Please select at least one registrant type from the list. ');
			validate++;
		}
	
	if( validate > 0 ){
//		alert('validate ' + validate);
		return false;
	} else {
//		alert('validate ' + validate);
		return true;
	}
	
}

function validateSearch(form){
//	alert(form);
	var validate = 0;
	if( form == "specialties" ){
		if( document.forms.specialties.spec_id.value == "header" ){
			alert ('Please select a Specialty Name');
			validate++;
		}
	}

	if( form == 'date' ){
		
		if( document.forms.date.beginmonth.value == "blank" ||
			document.forms.date.beginyear.value == "blank" ){
			alert ('Please select a month and year');
			validate ++;
		}	
	}
	
	if( validate > 0 ){
		return false; 
	} else {
		return true;
	}
}

//POPUP with scroll bars and variable size
function popup(urlVar,x,y){
	var popup_width=x;
    var popup_height=y;
    var screen_width=window.screen.width;
    var screen_height=window.screen.height;
    var popup_left=Math.round((screen_width-popup_width)/2);
    var popup_top=Math.round((screen_height-popup_height)/2); 	
    window.open(urlVar,'',"height=" + popup_height + ",width=" + popup_width + ",resizable=no,left="+ 
    popup_left + ",top=" +popup_top+ ",scrollbars=1");

}

function validateDirectors(){
	var validate = 0;
	var theForm = document.forms[0];
	if( theForm.elements[0].value != "header" ){
		var test = 0;
		for(i=0; i<theForm.elements.length; i++){
//			alert ("b4 if: " + i + " " + theForm.elements[i].value );
//			alert ("b4 if: " + i + " " + theForm.elements[i].name + " " + theForm.elements[i].value);
    		if( i != 4 && (i > 0 && i <13)){
				if(theForm.elements[i].value != '' && theForm.elements[i].name != 'crs_id'){
//					alert ("in if: " + i + " " + theForm.elements[i].name + " " + theForm.elements[i].value);
					test++;	
				}
			}		
   
		}

		/*for( i=1; i<4; i++ ){
//			alert(theForm.elements[i].value);
			
		}*/
		if( test != 0){
			alert('You can not have more than one director per screen. Make sure that all the director fields are blank, or that you have not selected a director from the pull-down menu. If you want to add another director, select \'Add Another Course Director >>\' ');
			validate++;
//			return false;
		} 
		
		
	}
	/*Coding this out to allow you to pass screen with no director.
	if( theForm.elements[0].value == 'header' ){
		if( theForm.elements[1].value == '' || theForm.elements[3].value == ''  ){
			alert('Please select a director from the list or enter one in the boxes below.');
			validate++;
		}
	}*/

	if( validate > 0 ){
		return false; 
	} else {
		return true; 
	}	
} 


function validateCoursePanel(){
	var crs_id = document.forms.me.crs_id.value;
	var radios=document.forms.me.crs_action;
	var checkvalue;
	var validate=0;
//	Validate text fields	
	if( !crs_id || crs_id=="header"  ){
		alert ('Please Select A Course Type');
		validate++;
	}

	//	Check Radio Buttons	
	for( var i=0; i < radios.length; i++ ){
		if( radios[i].checked ){
			checkvalue = radios[i].value;
			break;
		}  
	}  

	if( checkvalue==undefined ){
		alert('Please select an action');
		validate++;
	}

	if( validate > 0 ){
		return false;
	} else {
		return true; 
	}
}

//******************************************************
function reloadCourse(){
	opener.location.reload(true);
    self.close();

}


//*************************************************************8
// Similar registration valiation function for courses you are EDITING: 
// This is b/c these courses have an additional field that throws off
// the logic in the loop for the initial form. 
// The extra field is the tuition ID field that is then used to UPDATE
// the database. Obviously, the create registration form does not have this, and
// the PHP back=end already has it's loop worked out (which I don't want to re-do), 
// and hence I am creating a new version of this function with a slightly different loop. 
// This changed when I actually updated registrant types and prodcuts etc instead
// of deleting and re-adding them, as I had previously done.  Whew!

function validateEditedRegistration(){

	var regTypes = new Array();
	var theForm = document.forms[0];
	var which = 0;
	var validate = 0;
	var chosen = 0; // how many registrant types are selected from menu.

	for(i=0; i<theForm.elements.length; i++){
		if( i >= 2 ){
			j=i-1;
			k = i-2;
			var myPrecedingValue = theForm.elements[j].value;
			var myPrecedingName = theForm.elements[j].name;
			var myOtherPrecedingValue = theForm.elements[k].value;
			var myOtherPrecedingName = theForm.elements[k].name;
		}
		
		
			
			
		
		/*alert(theForm.elements[i].value);
		alert(theForm.elements[j].value);*/
		var mytype = theForm.elements[i].type;
		var myname = theForm.elements[i].name;
		var myvalue = theForm.elements[i].value;
		
		
//		alert(myPrecedingValue + ':'+ myvalue + ' ' + myname);
//		Make array of pull down menu values to check for duplicates
		if( mytype == "select-one" ){
			regTypes[which] = myvalue;
			which++;
		}

//		MAKE SURE all tuition values are numbers and not strings or spaces
		tuition = myname.search(/crs_tuition/);
		if( tuition != -1 ){
			if( myvalue == '0' ){
					var hasNoFee = true;
			}			
			if( myvalue !='' && myvalue != "complimentary" ){
				
				var isMon = myvalue.search(/^\d+$/);
				myvalue = parseInt(myvalue);
				/*
				alert('myvalue: ' + myvalue);
				alert('myPrecedingValue: ' + myPrecedingValue);
				alert('myPrecedingName: ' + myPrecedingName);
				alert('myOtherPrecedingValue: ' + myOtherPrecedingValue);
				alert('myOtherPrecedingName: ' + myOtherPrecedingName);
				*/
				if( isMon == -1 || hasNoFee == true ){
//					alert ('myvalue: ' + myvalue + ' isMon: ' + isMon);
					alert('Please make sure that any course tuition values are numerals greater than 0,  and entered in the correct format. ');
					validate++;
				}

	//			MAKE SURE each tuition is preceded by an actual registrant type			
				if( !isNaN(myvalue) ){
					if( myOtherPrecedingValue == 'header'){
//						alert(theForm.elements[i].value + " :" + theForm.elements[j].value);
						alert('Please select a registrant type for each tuition number entered. ');
						validate++;
					}
				}
			}
			
		}

		var product = myname.search(/prod_price/);
		if( product != -1 ){
			var complimentary = myvalue.search(/complimentary/);
			/*
				alert('myvalue: ' + myvalue);
				alert('myPrecedingValue: ' + myPrecedingValue);
				alert('myPrecedingName: ' + myPrecedingName);
				alert('myOtherPrecedingValue: ' + myOtherPrecedingValue);
				alert('myOtherPrecedingName: ' + myOtherPrecedingName);
				*/
//			alert(myvalue + " " + complimentary);
			if( complimentary != 0 ){
				var x = isValidNum(myvalue);
//				alert('x: ' + x);
				if( x == false ){
					alert('Please make sure that any course tuition values are numerals. ');
					validate++;
				} else {
					if( theForm.elements[j].value == '' ){
						alert( 'Please enter a product name for every product price. ');
						validate++;
					}
				}
			}
		}

		var discount = myname.search(/discount_amt/);
		if( discount != -1 ){
			
			if( myvalue != '' ){
			var x = isValidNeg(myvalue);
//				alert('x: ' + x);
			if( x == false ){
				alert('Please make sure that any course discount values are numerals. ');
				validate++;
			} else {
				if( theForm.elements[j].value == '' ){
					alert( 'Please enter an item name for every disounted amount. ');
					validate++;
				}
			}
			}
		}

	}

//		CHECK for duplicate values in pulldown Menus
		for( i=0; i < regTypes.length; i++ ){
			
			var valueOuter = regTypes[i];
			for( j=0; j < regTypes.length; j++ ){
				var valueInner = regTypes[j];
				if( i != j ){
					if( valueOuter == valueInner && valueOuter != 'header' ){
						alert('Please do not select more than one of the same type of registrants. ');
						validate++;
						
					}
				}	
			}
		}

//		Check that there is at least one registrant type entered. 
	 	for( i=0; i < regTypes.length; i++ ){
			if( regTypes[i] == 'header' ){
				chosen++;
			}	 
		}
		if( chosen == regTypes.length ){
			alert('Please select at least one registrant type from the list. ');
			validate++;
		}

	if( validate > 0 ){
//		alert('validate ' + validate);
		return false;
	} else {
//		alert('validate ' + validate);
		return true;
	}
	
}