/* 	Following code was added by Nay (Avetar Interactive) on Nov 20, 2006
 *	
 *  Following Functions are used in referral form submission to validate email, Comments, and etc.
 */
 
 function Trim(x) {
	var i;
	if (x.length == 0) {return "";};
	i = x.length - 1;
	while (x.substring(i,i+1) == " " && i > 0 )  {
		x = x.substring(0,i);
		i--;
	}
	if (x.length == 0) {return "";};
	while (x.substring(0,1) == " " && x != "" )  {
		x = x.substring(1,x.length);
		i++;
	}
	return x;
}// end Trim

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
}//echeck

function isInteger(s){   
	var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}// End isInteger

// bool: check_field( string: ele, int: len, string: msg )
function check_field( ele, len, msg ){

	if ( Trim(ele.value).length < len ){
		alert( msg );
		ele.focus();
		ele.select();
		return false;
	}else{
		return true;
	}//end if

}//check_field

function check_email(emailID, msg){

	if(msg == "") msg = "Please Enter your Email ID";

	if ((emailID.value==null)||(emailID.value=="")){
		alert(msg);
		emailID.focus();
		return false;
	}
	if (echeck(emailID.value)==false){
		emailID.value="";
		emailID.focus();
		return false;
	}	
	return true;

}//check_email

/// bool : isChecked( string: form_name, string: checkbox_name )
/// Check for the checkbox in the given form
/// whether it's cheked
function isChecked(frm,chk){
	var return_val = false;
	
	if (document.forms.length > 0) {
		var frm = document.forms[frm] ;
		for (i=0; i<frm.length; i++){
			if(frm.elements[i].type == "checkbox"){
				if(frm.elements[i].name && frm.elements[i].name.indexOf(chk) != -1){
						if(frm.elements[i].checked==true){
								return true;
						}else{
								return false;
						}
						break;
				}
			}
		} // End for LOOP
	}
	
	return return_val;
}//isChecked

/// bool : isOldEnough( int: age )
function isOldEnough(age){
		var return_val = false;
		if(age == "" || age == 0)
			return false;
			
		if(age >= 18)
			return_val = true;
		
		return return_val;
}//isOldEnough

/// bool : amount( int: amount )
function amount(amount){
		var return_val = false;
		if(amount == "" || amount == 0)
			return false;
			
		if(amount <= 25)
			return_val = true;
		
		return return_val;
}//amount

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function CountWordsLeft(myForm, field, count, no_words) {
    var text=field.value + " ";
    if(no_words>0)
    {
        var iwhitespace = /^[^A-Za-z0-9]+/gi; // remove initial whitespace
        var left_trimmedStr = text.replace(iwhitespace, "");
        var na = rExp = /[^A-Za-z0-9]+/gi; // non alphanumeric characters
        var cleanedStr = left_trimmedStr.replace(na, " ");
        var splitString = cleanedStr.split(" ");
        var word_count = splitString.length -1;
        count.value=no_words-word_count;
    }
}
function ValidateForm(){

    var f = document.jennysHeroesForm;

    
	if( !check_field( f.first_name, 1, "Please Enter Your First Name" ) )
		return false; 

	if( !check_field( f.last_name, 1, "Please Enter Your Last Name" ) )
		return false; 
	
	if( !check_field( f.age, 1, "Please Enter Your Age" ) )
		return false; 
	
	if( !isInteger( f.age.value ) ){
		alert("Please Enter Your Real Age");
		f.age.focus();
		f.age.select();
		return false; 	
	}
	
	if( !isOldEnough( f.age.value ) ){
		alert("You must be 18 or older to apply");
		f.age.focus();
		f.age.select();
		return false; 	
	}	
		
	if( !check_field( f.occupation, 1, "Please Enter Your Occupation" ) )
		return false; 		
		
	if( !check_field( f.city, 1, "Please Enter Your City of Residence" ) )
		return false; 		
		
	if( !check_field( f.phone_number, 7, "Please Enter Your Phone Number" ) )
		return false; 		
			
	if( !check_email(f.email, "Please Enter Your E-mail") )
		return false; 	
			
	if( !isChecked('jennysHeroesForm','citizen') ){
		alert("You must be a U.S. Citizen to apply");
		f.citizen.focus();
		return false;
	}
	
	if( !isChecked('jennysHeroesForm','bg_check') ){
		f.bg_check.focus();
		alert("You must agree to a background check");
		return false;
	}
		
	if( !check_field( f.brief, 3, "Please Describe Your Idea in Brief" ) )
		return false;
	
	if( !check_field( f.amount, 1, "Please Enter An Amount" ) )
		return false; 
	
	if( !isInteger( f.amount.value ) ){
		alert("Please Enter A Real Amount");
		f.amount.focus();
		f.amount.select();
		return false; 	
	}
	
	if( !amount( f.amount.value ) ){
		alert("You May Only Enter An Amount Up To 25 (meaning $25,000)");
		f.amount.focus();
		f.amount.select();
		return false; 	
	}		
		
	if( !check_field( f.dream, 7, "Please Describe Your Dream" ) )
		return false;
		
	if( !isChecked('jennysHeroesForm','policy') ){
		alert("You need to read our Privacy Policy, Terms of Use and Official Rules to apply");
		f.policy.focus();
		return false;
	}			

	return true
 }
 
 function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function CountWordsLeft(myForm, field, count, no_words) {
    var text=field.value + " ";
    if(no_words>0)
    {
        var iwhitespace = /^[^A-Za-z0-9]+/gi; // remove initial whitespace
        var left_trimmedStr = text.replace(iwhitespace, "");
        var na = rExp = /[^A-Za-z0-9]+/gi; // non alphanumeric characters
        var cleanedStr = left_trimmedStr.replace(na, " ");
        var splitString = cleanedStr.split(" ");
        var word_count = splitString.length -1;
        count.value=no_words-word_count;
    }
}