/*-----------------------------------------------------------------------
Created by: Kd
Created date: 26-Sep-06
File description: Member Form
Special instructions-notes:Java script Validation
Tables used:None
Stored procedures:None
Triggers used:None
-----------------------------------------------------------------------*/

function Validate(theForm)
{
	var errMesg = "";
	var displayMesg = "";
	
	var loginName = theForm.loginId.value = theForm.loginId.value.toLowerCase();
	
	if ( !isCharsInBag( loginName, "abcdefghijklmnopqrstuvwxyz0123456789.-_@")){
		errMesg += "User name has invalid characters.\n" ;
	}else if ( loginName.length < 3 ){
		errMesg += "User name must be 3 or more characters.\n" ;
	}else{
		var count;
		for (count=0; count < loginName.length; count++){
			if (loginName.charAt(count) == "_")
			{
				if (loginName.charAt(count+1) == "_"){
					errMesg += "User name may not contain more than one consecutive underscore.\n";
				}
			}
		}
	}
	
	if(theForm.pass.value.length < 6 && theForm.pass.value.length > 12){
		errMesg += "Password must be 6-12 characters.\n";
	}
	
 	var Q = ""; // this block determines lifespan of Q
	{	
		if(isWhitespace(theForm.orgName.value)){
			Q += "  Organization Name\n";
		}
		
		if(isWhitespace(theForm.orgPhone1.value) || isWhitespace(theForm.orgPhone2.value) || isWhitespace(theForm.orgPhone3.value)){
			Q += "  Organization Phone\n";
		}
		
		if(isWhitespace(theForm.senfirstName.value)){
			Q += "  Pastor / Executive director First Name\n";
		}
		
		if(isWhitespace(theForm.orgEmail.value)){
			Q += "  Organization Email\n";
		}else if(echeck(theForm.orgEmail.value)){
		   errMesg += "Invalid Organization Email Address\n";
		}else if(!isCharsInBag( theForm.orgEmail.value, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@._-")){
			errMesg += "Organization Email Address contains Invalid Characters\n";
		}
		
		if(isWhitespace(theForm.senlastName.value)){
			Q += "  Pastor / Executive director Last Name\n";
		}

		if(theForm.senEmail.value != ""){
			if(echeck(theForm.senEmail.value)){
			   errMesg += "Invalid Organization Pastor / Executive director Email Address\n";
			}else if(!isCharsInBag( theForm.senEmail.value, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@._-")){
				errMesg += "Pastor / Executive director Email Address contains Invalid Characters\n";
			}
		}
		
		if(theForm.senmobNumber1.value != ""){
			if(!isCharsInBag(theForm.senmobNumber1.value, "0123456789") || !isCharsInBag(theForm.senmobNumber2.value, "0123456789") || !isCharsInBag(theForm.senmobNumber3.value, "0123456789")){
				errMesg += "Pastor / Executive director cell Number contains Invalid Characters\n";
			}
		}

if(theForm.orgWebsite.value==""){
			Q += "  Organization Website\n";
		}	
		
		if(theForm.loginId.value==""){
			Q += "  Login Id\n";
		}
		
		if (isWhitespace(theForm.pass.value)){
			Q += "  Password\n";
		}
		
		if (isWhitespace(theForm.firstAddress.value)){
			Q += "  Address Line 1\n";
		}

		if (isWhitespace(theForm.evanfirstName.value)){
			Q += "  Person Over Follow-up First Name\n";
		}
		
		if (isWhitespace(theForm.city.value)){
			Q += "  City\n";
		}
		
		if (isWhitespace(theForm.evanlastName.value)){
			Q += "  Person Over Follow-up Last Name\n";
		}
		
		if (isWhitespace(theForm.state.value)){
			Q += "  State / Province\n";
		}
		
		if (isWhitespace(theForm.evanEmail.value)){
			Q += "  Person Over Follow-up Email Address\n";
		}else if(echeck(theForm.evanEmail.value)){
		   errMesg += "Invalid Evanglism director Email Address\n";
		}else if(!isCharsInBag( theForm.evanEmail.value, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@._-")){
			errMesg += "Person Over Follow-up Email Address contains Invalid Characters\n";
		}
		
		if (isWhitespace(theForm.zipCode.value)){
			Q += "  Zip Code\n";
		}else if(!isCharsInBag(theForm.zipCode.value, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz")){
			errMesg += "Zip Code contains Invalid Characters\n";
		}
		
		if (isWhitespace(theForm.evanHomePhone1.value) || isWhitespace(theForm.evanHomePhone2.value) || isWhitespace(theForm.evanHomePhone3.value)){
			Q += "  Person Over Follow-up Home Phone Number\n";
		}
		
		if (isWhitespace(theForm.country.value)){
			Q += "  Country\n";
		}

		/*if(isWhitespace(theForm.churchLogo.value)){
			Q += "  Logo\n";
		}*/
		
		if(theForm.churchLogo.value != ""){
			str=theForm.churchLogo.value;
			
			indx=str.lastIndexOf(".");
			
			if(indx>0){	
				ext=str.substr(++indx);
				if((ext=='png')||(ext=='jpg')||(ext=='jpeg')||(ext=='gif')||(ext=='bmp')||(ext=='JPEG')||(ext=='GIF')||(ext=='JPG')||(ext=='BMP')||(ext=='PNG')){	
					displayMesg +="";
				}else{								
					errMesg +="Invalid Logo File\n";
				}
			}
		}
	
		if( Q.length > 0 ){
			displayMesg = "Please provide Valid values for\n" + Q ;
		}	
	} 
	
	if (errMesg == "" && displayMesg == "")
	{
		return true;
	}
	else
	{
		if(displayMesg!="")
			{
				alert(displayMesg);
				return false;			
			}
			else
			{
				alert(errMesg);
				return false;
			}	
	}
}

function show(){
	document.form1.orgName.focus();	
}

var dFilterStep

function dFilterStrip (dFilterTemp, dFilterMask)
{
    dFilterMask = replace(dFilterMask,'#','');
    for (dFilterStep = 0; dFilterStep < dFilterMask.length++; dFilterStep++)
		{
		    dFilterTemp = replace(dFilterTemp,dFilterMask.substring(dFilterStep,dFilterStep+1),'');
		}
		return dFilterTemp;
}

function dFilterMax (dFilterMask)
{
 		dFilterTemp = dFilterMask;
    for (dFilterStep = 0; dFilterStep < (dFilterMask.length+1); dFilterStep++)
		{
		 		if (dFilterMask.charAt(dFilterStep)!='#')
				{
		        dFilterTemp = replace(dFilterTemp,dFilterMask.charAt(dFilterStep),'');
				}
		}
		return dFilterTemp.length;
}

function dFilter (key, textbox, dFilterMask)
{		
		dFilterNum = dFilterStrip(textbox.value, dFilterMask);
		
		if (key==9)
		{
		    return true;
		}
		else if (key==8&&dFilterNum.length!=0)
		{
		 	 	dFilterNum = dFilterNum.substring(0,dFilterNum.length-1);
		}
 	  else if ( ((key>47&&key<58)||(key>95&&key<106)) && dFilterNum.length<dFilterMax(dFilterMask) )
		{
        dFilterNum=dFilterNum+String.fromCharCode(key);
		}

		var dFilterFinal='';
    for (dFilterStep = 0; dFilterStep < dFilterMask.length; dFilterStep++)
		{
        if (dFilterMask.charAt(dFilterStep)=='#')
				{
					  if (dFilterNum.length!=0)
					  {
				        dFilterFinal = dFilterFinal + dFilterNum.charAt(0);
					      dFilterNum = dFilterNum.substring(1,dFilterNum.length);
					  }
				    else
				    {
				        dFilterFinal = dFilterFinal + "";
				    }
				}
		 		else if (dFilterMask.charAt(dFilterStep)!='#')
				{
				    dFilterFinal = dFilterFinal + dFilterMask.charAt(dFilterStep); 			
				}
		}
		textbox.value = dFilterFinal;
    return false;
}

function replace(fullString,text,by) {
// Replaces text with by in string
    var strLength = fullString.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return fullString;

    var i = fullString.indexOf(text);
    if ((!i) && (text != fullString.substring(0,txtLength))) return fullString;
    if (i == -1) return fullString;

    var newstr = fullString.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(fullString.substring(i+txtLength,strLength),text,by);

    return newstr;
}