function Trim(Untrimmed) 
{
/*This function removes all spaces from a string
Example Trim(' Me And You ') returns 'MeAndYou'
*/
var Trimmed = ''
for (var i = 0; i < Untrimmed.length; i++) 
{
if (Untrimmed.charCodeAt(i)!=32)
{
Trimmed += Untrimmed[i]
}
}
return Trimmed
} 

function validRequired(formField,fieldLabel)
{
	var result = true;
	
	
	if (Trim(formField.value) == "")
	{
		alert('Please enter a value for the "' + fieldLabel +'" field.');
		formField.focus();
		result = false;
	}
	
	return result;
}

function isEmailAddr(email)
{
  var result = false;
  var theStr = new String(email);
  var index = theStr.indexOf("@");
  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  return result;
}


function validEmail(formField,fieldLabel,required)
{
	var result = true;
	
	if (required && !validRequired(formField,fieldLabel))
		result = false;

	if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
	{
		alert("Please enter a complete email address in the form: yourname@yourdomain.com");
		formField.focus();
		result = false;
	}
   
  return result;

}

/*Field Validation for Quick Lik pages*/

function membershipValidateForm(theForm)
{
        var pass ="", pass1=""
   	if (!validRequired(theForm.name,"Name"))
		return false; 
		if (!validRequired(theForm.companyName,"Company Name"))
		return false; 
		if (!validRequired(theForm.address,"Address"))
		return false; 
		if (!validRequired(theForm.city,"City"))
		return false; 
		if (!validRequired(theForm.province,"Province"))
		return false; 
		if (!validRequired(theForm.postal_code,"Postal Code"))
		return false; 
		if (!validRequired(theForm.telephone,"Telephone"))
		return false; 
		if (!validRequired(theForm.username,"User Name"))
		return false; 
		if (!validRequired(theForm.password,"Password"))
		return false; 
		
		 
		if (!validRequired(theForm.password1,"re-type Password"))
		return false; 
		if (theForm.password.value.length<6){
			alert("Please enter a password of at least than 6 letters")
		return false; 
		}

		if (theForm.password.value.length<6){
			alert("Please enter a password of at least than 6 letters")
		return false; 
		}
		if (Trim(theForm.password.value)!=Trim(theForm.password1.value)) 
		{
		alert("Both Passwords should be identical. Please Re-Enter password");
		return false;
		} 
            
	return true;
}
 function passwordValidateForm(theForm)
{
        var pass ="", pass1=""
		if (!validRequired(theForm.oldPassword,"Password"))
		return false; 
		if (!validRequired(theForm.password,"Old Password"))
		return false; 
		if (!validRequired(theForm.password1,"re-type Password"))
		return false; 
        if (theForm.password.value.length<6){
			alert("Please enter a password of at least than 6 letters")
		return false; 
		}
		 if (theForm.password1.value.length<6){
			alert("Please enter a password of at least than 6 letters")
		return false; 
		}
		if (Trim(theForm.password.value)!=Trim(theForm.password1.value)) 
		{
		alert("Both Passwords should be identical. Please Re-Enter password");
		return false;
		} 

	return true;
}

 
 
function indLoginForm(theForm)
{

   	       if (!validRequired(theForm.username,"User Name"))
		return false; 
		if (!validRequired(theForm.password,"Password"))
		return false; 

	return true;
}
 
function eintakeValidateForm(theForm)
{

   	if (!validRequired(theForm.company,"Company Name"))
		return false; 
		if (!validRequired(theForm.location,"Location"))
		return false; 
		if (!validRequired(theForm.emp_first_name,"Employee's First Name"))
		return false; 
		if (!validRequired(theForm.emp_last_name,"Employee's Last Name"))
		return false; 
		if (!validRequired(theForm.emp_DOB,"Employee's Date of Birth"))
		return false; 
		if (!theForm.same_as_emp.checked ){
			if (!validRequired(theForm.client_first_name,"Client's First Name"))
			return false;
			if (!validRequired(theForm.client_last_name,"Client's last Name"))
			return false;
			if (!validRequired(theForm.client_DOB,"Client's Date of Birth"))
			return false;
			if (!theForm.relationship[0].checked && !theForm.relationship[1].checked && !theForm.relationship[2].checked && !theForm.relationship[3].checked){
			alert("Please Specify Client's Relationship to Employee")
			return false;
			}
			
		}
	
		if (!validRequired(theForm.addr_street,"Address > Street"))
		return false; 
		if (!validRequired(theForm.addr_city,"Address > City"))
		return false; 
		if (!validRequired(theForm.addr_province,"Address > Province"))
		return false; 
		if (!validRequired(theForm.addr_postal,"Address > Postal Code"))
		return false;
		
	    	if (theForm.telephone_home.value=="" && theForm.telephone_other.value==""){
		alert("You need to provide at least one telephone number");
		return false; 
		}
		 
		if (!validRequired(theForm.password1,"re-type Password"))
		return false; 
		if (theForm.password.value.length<6){
			alert("Please enter a password of at least than 6 letters")
		return false; 
		}

		if (theForm.password.value.length<6){
			alert("Please enter a password of at least than 6 letters")
		return false; 
		}
		if (Trim(theForm.password.value)!=Trim(theForm.password1.value)) 
		{
		alert("Both Passwords should be identical. Please Re-Enter password");
		return false;
		} 
            
	return true;
}
 
function providerValidateForm(theForm)
{

   	if (!validRequired(theForm.file_number,"File Number"))
		return false; 
		if (!validRequired(theForm.counsellor_name,"Counsellor's Name"))
		return false; 
		if (!validRequired(theForm.client_name,"Client's Name"))
		return false; 
		//if (!validRequired(theForm.contact_date,"Contact Date"))
		//return false; 
		if (isDate(theForm.contact_date.value, "Contact Date")==false){
			theForm.contact_date.focus()
			return false
	     }
		
		if (!validRequired(theForm.contact_duration,"Contact Duration"))
		return false; 
		
	
		
         if (Trim(theForm.followup_date.value) != ""){
		    if (isDate(theForm.followup_date.value, "Followup Date")==false){
			theForm.followup_date.focus()
			return false
	     }
		 }
		/* if (Trim(theForm.appointment.value) != ""){
		 if (isDate(theForm.appointment.value, "Appointment Date")==false){
			theForm.appointment.focus()
			return false
	     }
		 }*/
		if (!validRequired(theForm.notes,"notes"))
		return false; 
		
	    	
		 
		if (!validRequired(theForm.password1,"re-type Password"))
		return false; 
		if (theForm.password.value.length<6){
			alert("Please enter a password of at least than 6 letters")
		return false; 
		}

		if (theForm.password.value.length<6){
			alert("Please enter a password of at least than 6 letters")
		return false; 
		}
		if (Trim(theForm.password.value)!=Trim(theForm.password1.value)) 
		{
		alert("Both Passwords should be identical. Please Re-Enter password");
		return false;
		} 
            
	return true;
}

/* used for date validation*/
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

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;
}

/* used for date validation*/
function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
/* used for date validation*/
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}
/* used for date validation*/
function isDate(dtStr, fieldLabel){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert('The date format should be : mm/dd/yyyy for the "' + fieldLabel +'" field.')
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert('Please enter a valid month for the "' + fieldLabel +'" field.')
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
        		alert('Please enter a valid day for the "' + fieldLabel +'" field.')

		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert('Please enter a valid 4 digit year between ' +minYear+ ' and '+maxYear + ' for the "' + fieldLabel +'" field.')        

		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
                		alert('Please enter a valid date for the "' + fieldLabel +'" field.') 

		return false
	}
return true
}
function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
   
function numbersonly(e, char) {
            var key;
            var keychar;

            if (window.event) {
                key = window.event.keyCode;
            }
            else if (e) {
                key = e.which;
            }
            else {
                return true;
            }
            keychar = String.fromCharCode(key);

            if ((key == null) || (key == 0) || (key == 8) || (key == 9) || (key == 13) || (key == 27)) {
                return true;
            }
            else if ((("0123456789").indexOf(keychar) > -1)) {
                return true;
            }
            else if (keychar == ".") {
                return true;
            }
            else
                alert('Only number and dot(.) are allowed');
            return false;
        }



function terminationValidateForm(theForm)
{

   	if (!validRequired(theForm.client_name,"Client Name"))
		return false; 
	if (!validRequired(theForm.file_number,"File Number"))
		return false; 
	if (!validRequired(theForm.employer_group,"Employer Group"))
		return false; 
	if (isDate(theForm.termination_date.value, "Date")==false){
			theForm.termination_date.focus()
			return false
	     }
    
	if (!validRequired(theForm.NumOfSession,"No of one hour sessions utilized"))
		return false; 
	if (!validRequired(theForm.no_shows,"No Shows"))
		return false; 
	if (!validRequired(theForm.cancelled,"Cancelled"))
		return false; 
		
	if (!validRequired(theForm.presenting_problem,"Presenting Problem"))
		return false; 
     if (!validRequired(theForm.outcome_based,"Outcome based on treatment goals .."))
		return false; 
	if (!validRequired(theForm.further_treatment,"Further treatment required.."))
		return false; 
	        
	return true;
}
function bioValidateForm(theForm)
{

   	if (!validRequired(theForm.client_name,"Client Name"))
		return false; 
	if (!validRequired(theForm.file_number,"File Number"))
		return false; 
	
	if (isDate(theForm.termination_date.value, "Date")==false){
			theForm.termination_date.focus()
			return false
	     }
    
        
	return true;
}
 
 
