// Purpose: Library of functions used for validating forms
// funtion: Determines whether str is empty or not
// note: A string build of only spaces & newlines is also considered empty

function emptyField(str){
	while(str.indexOf(" ",0)!=-1){
		str=str.replace(" ","");
	}
	
	while(str.indexOf("\r\n",0)!=-1){
		str=str.replace("\r\n","");
	}
	
	while(str.indexOf("\n",0)!=-1){
		str=str.replace("\n","");
	}
	
	if(str==""){
		return(true);
	}

	return(false);
}

//function: Determines whether a valid email address entered or not
function validEmail(email){

	//checking for existence of invalid characters
 	var invalidChars = " /\\:,;#%$^*&+|=`,<>\"";
	
 	if(emptyField(email)){
		return(false);
	}
	
	for(i = 0 ; i < invalidChars.length ; i++){
		badChar = invalidChars.charAt(i);
		if(email.indexOf(badChar,0)!=-1){
			return(false); // email contains invalid chrs.
		}
	}
	
	//checking for @ validity
	atPos = email.indexOf("@",1);
	if(atPos == -1 || atPos==email.length-1){
		return(false); // either no '@' present or it is present at the very first or last position.
	}
	otherAtPos = email.indexOf("@",atPos+1);
	if(otherAtPos != -1){
		return(false); // more than one '@' present
	}
	
	//checking for . validity
	periodPos = email.indexOf(".",0);
	
	if(periodPos == -1){
		return(false); // no period exits at all
	}
	
	while(periodPos != -1){
		if(periodPos == 0 || periodPos==email.length-1 || periodPos+3 > email.length || periodPos==atPos-1 || periodPos==atPos+1){
			return(false); // no period exits or it is present at very first or last position or just before or after @ or atleast 2 chrs are not present after .
		}
		periodPos = email.indexOf(".",periodPos+1);
	}

	periodPos = email.indexOf(".",atPos+1);
	
	if(periodPos == -1){
		return(false); // no period exits after @
	}
		
	return(true);
}

//function: Determines whether str contains spaces or not
function containsSpaces(str){
	var i,badChar;

	if(str == ""){
		return false;
	}
	
	badChar = " ";
	if(str.indexOf(badChar,0)!=-1){
		return true; // str contains invalid chrs.
	}

	return false;
}

//function: Determines whether str contains special characters or not
function containsSplCharacters(str){
	var i,badChar;
 	var invalidChars = "'/\\:,;#%$^*&+|=`-.~!@()_?,<>\"";
	
 	if(str == ""){
		return false;
	}
	
	for(i = 0 ; i < invalidChars.length ; i++){
		badChar = invalidChars.charAt(i);
		if(str.indexOf(badChar,0)!=-1){
			return true; // str contains invalid chrs.
		}
	}
	return false;
}

//function: Determines whether str contains digits or not
function containsDigits(str){
	var i,badChar;
 	var invalidChars = "1234567890";
	
 	if(str == ""){
		return false;
	}
	
	for(i = 0 ; i < invalidChars.length ; i++){
		badChar = invalidChars.charAt(i);
		if(str.indexOf(badChar,0)!=-1){
			return true; // str contains invalid chrs.
		}
	}
	return false;
}

//function: Determines whether str contains " or not
function containsDoubleQuotes(str){
	var i,badChar;

	if(str == ""){
		return false;
	}
	
	badChar = "\"";
	if(str.indexOf(badChar,0)!=-1){
		return true; // str contains invalid chrs.
	}

	return false;
}