
var submitFlag = false;
var pwdFlag = false;
var path = "document.";


// We we have an idea of all the illegal character we don't
// sent to the server let's rewrite this using regular expression
function checkForm(f)
{
	//alert("working") //debug
	//initVars();
	if(path.length < 10){
	path += f.name;
	}
	if(submitFlag){
		alert("Form already submitted\n Thank you!");
		return false;
	}
	eLength = f.elements.length;
	for( var i=0; i < eLength; i++){
		// check for required fields
		// Is that field required, if not , no need to work
	
	if(f.elements[i].name.indexOf("_req") != -1){
			// check passwords
			if(f.elements[i].type == "password") {
				pwdFlag = !pwdFlag ;
				if(!checkPwd(f, i, "onsubmit")) return false;
			}
			// check empty
			if((f.elements[i].name.indexOf("_req") != -1)&& (f.elements[i].value == 0)){
			   alert("This field is required");
			   f.elements[i].focus();
			   return false
			} 
			// check select
			if((f.elements[i].type == "select-one")||(f.elements[i].type == "select-multiple")){
				if(!checkSelect(f.elements[i]))return false;
			}
			//check all text box for illegal chars
			if((f.elements[i].type == "text")||(f.elements[i].type == "textarea")){
				if(!checkChar(f.elements[i]))return false;
			}
			if(f.elements[i].type == "textarea"){
				if(!checkMax(f.elements[i])) return false;
			}
			
			// EJF added to check checkbox
			if(f.elements[i].type == "checkbox"){
				if(!checkCheckbox(f.elements[i]))return false;
			}
			if((f.elements[i].name.indexOf("email") != -1)){
				if(!checkEmail(f.elements[i]))return false;
			}
		}//end of if	
	}//end of for
	submitFlag = true;
	return true;
} // end of checkForm()

//check pwd for this particular file/vortex
function checkAcctPwd()
{
	if(document.frm.acct_pwd_req.value != document.frm.acct_confirm_req.value ){
		alert("Unmatched password");
		document.forms[0].acct_confirm_req.focus();
		return false;
	}
	return true;
	
}


// reset flag if from reset
function resetFlag()
{
	if(submitFlag) submitFlag = !submitFlag;
	//or simply submitFlag = !submitFlag; 
}
// EJF added check for check box
function checkCheckbox(el)
{  
  if(el.checked == 0){
    alert("You must agree to the Affiliate Agreement and check the box to participate!");
    el.focus();
    return false;
  }
  return true;
}
function checkSelect(el)
{  
  // alert(document.frm.acct_st_req.options[document.frm.acct_st_req.selectedIndex].value); // debug
  if(el.selectedIndex == 0){
    alert("Required selection");
    el.focus();
    return false;
  }
  return true;
}

function checkKeywords(f){

	if(path.length < 10){
	path += f.name;
	}
	if(submitFlag){
		alert("Form already submitted\n Thank you!");
		return false;
	}
	eLength = f.elements.length;
	for( var i=0; i < eLength; i++){
		if((f.elements[i].type == "text")||(f.elements[i].type == "textarea")){
				if(!checkChar(f.elements[i]))return false;
			}
	}// end of for
	submitFlag = true;
	return true;
	
}
function checkChar(el)
{	
	var val = el.value;
	//alert("val: " + val); //debug
	if(val.indexOf("\\") != -1){
		//el.blur();
		//el.select(); // this does not work, find out why
		alert("No back slashes in text \n ... please");
		el.focus();


		return false;
	}
	if(val.indexOf("\"") != -1){
		alert("No double-quotes in text \n ... please");
		el.focus();
		return false;
	}
	
	
	return true;
}

// quasi generic check pwd function , check where the function is
// called from, i.e. which event triggered it
function checkPwd(formObj, count, event)
{
	
	var a = eval(formObj).elements[count].value;
	var b = eval(formObj).elements[count + 1].value;
	 //alert("a: " + a + "b: " + b);
	
	if(a != b){
		alert("Unmatched Password");
		eval(formObj).elements[count].focus(); // it does not matter which password it focuses on
		return false;
	}
	return true;
}

// Setting JavaScript flag
// If JS is enable this function sets flag to yes if not
function checkJS()
{	
	
	document.forms[0].elements[0].value = "yes";
}



function checkEmail(el)
{

	if(-1 == el.value.indexOf("@")) { 
        el.focus(); 
        alert("Your email must have a '@'."); 
        return false; 
        }
     if(-1 != el.value.indexOf(",")) { 
        el.focus(); 
        alert("Your email must not have a ',' in it"); 
        return false; 
        }
     if(-1 != el.value.indexOf("#")) { 
        el.focus(); 
        alert("Your email must not have an '#' in it." ); 
        return false; 
        }
     if(-1 != el.value.indexOf("!")) { 
        el.focus(); 
        alert("Your email must not have a '!' in it." ); 
        return false; 
        }
     if(-1 != el.value.indexOf(" ")) { 
        el.focus(); 
        alert("Your email must not have a space in it." ); 
        return false; 
        }
     if(el.value.length == (el.value.indexOf("@")+1) ) {
       var i = el.value.indexOf("@"); // debug
       //alert("i: " + i); // debug 
       el.focus(); 
       alert("Your email must have a domain name after the '@'."); 
       return false; 
       }
      if(el.value.indexOf("@")== 0) {
       // var i = el.value.indexOf("@"); // debug
       //alert("i: " + i); // debug 
       el.focus(); 
       alert("Your email must have something \n before the '@'."); 
       return false; 
       }
		if( el.value.indexOf(".") == -1){
			el.focus(); 
			alert("Your email must have a period in it." ); 
			return false; 
		}
       
      
       if(el.value.length == 0) { 
       el.focus(); 
       alert("Please enter your email."); 
       return false; 
       }
 
     return true;
	
}

function checkPhone()
{
	// to be completed
}
function resetForm()
{
	if(confirm("Reset all data?")){
	   resetFlag();
	   return true;
	}else
	return false;
}

// Take any textarea, token if more than one string
// return false if token is more than MAX
function checkMax( el){
	// max value of token
	var MAX = 50
	var	strTest = el.value;
	var strArray = strTest.split(/\s/);
	//alert("strArray: " + strArray ); // debug
	//alert("testLength: " + strArray.length )// debug
	for( i=0; i < strArray.length; i++)
	{		var len = strArray[i].length;
			if( len > MAX){
				strSub = strArray[i].substr(0, 8)
				//alert("strSub: " + strSub );
				alert( "Word begining with: \n" + strSub + " \nis too long.");
				return false;
			}
	}
	return true;
}


