var pageType="wordpress"; // local/wordpress
var urlTS="";
var and="";
if(pageType=="local")
{
	urlTS="";	
	and="?";
}
else
{
	urlTS="TherapistSearch/";	
	and="&";
}

var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;

function checkmail(e){
    return emailfilter.test(e);
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
function ValidateSendEmail()
{
	if (document.frmSendEmail.email.value == ""){
		document.getElementById('lblError').innerHTML = "E-mail address is required!";
		return false;
	}
	
	if (!checkmail(document.frmSendEmail.email.value)){
	    document.getElementById('lblError').innerHTML = "Please enter a valid email address!";
		return false;
    }	
	if (document.frmSendEmail.subject.value == ""){
		document.getElementById('lblError').innerHTML = "Subject is required!";
		return false;
	}
	if (document.frmSendEmail.message.value == ""){
		document.getElementById('lblError').innerHTML = "Message is required!";
		return false;
	}
	
	return true;	
}


function ValidateLogin()
{
	if (document.frmLogin.myusername.value == ""){
		document.getElementById('lblError').innerHTML = "E-mail address is required!";
		return false;
	}
	
	if (!checkmail(document.frmLogin.myusername.value)){
	    document.getElementById('lblError').innerHTML = "Please enter a valid email address!";
		return false;
    }
	
	if (document.frmLogin.mypassword.value == ''){
		document.getElementById('lblError').innerHTML = "Password is required!";
		return false;
	}
	
	if (document.frmLogin.mypassword.value.trim().length < 6){
		document.getElementById('lblError').innerHTML = "Password must be at least 6 characters long!";
		return false;
	}
	
	return true;	
}

function ValidateUser()
{
	if (document.frmUser.FirstName.value == ''){
		document.getElementById('lblError').innerHTML = "First Name is required!";
		return false;
	}
	if (document.frmUser.LastName.value == ''){
		document.getElementById('lblError').innerHTML = "Last Name is required!";
		return false;
	}
	
	if (document.frmUser.Email.value == ""){
		document.getElementById('lblError').innerHTML = "E-mail address is required!";
		return false;
	}
	
	if (!checkmail(document.frmUser.Email.value)){
	    document.getElementById('lblError').innerHTML = "Please enter a valid email address!";
		return false;
    }
	
	if (document.frmUser.ConfirmEmail.value == ""){
		document.getElementById('lblError').innerHTML = "Confirm E-mail is required!";
		return false;
	}
	
	if (document.frmUser.Email.value != document.frmUser.ConfirmEmail.value){
		document.getElementById('lblError').innerHTML = "Your E-mail Addresses do not match!";
		return false;
	}
	
	
	//ajax
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request!")
		return
	}
	var url=urlTS+"ajax/duplicate_user.php"
	url=url+"?id="+document.frmUser.hdnUserId.value;
	url=url+"&Email="+document.frmUser.Email.value;
	//alert(url);
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);
	if (xmlHttp.responseText == "exists"){
		document.getElementById('lblError').innerHTML = "Email is already in use!";
		return false;
	}
	//end ajax
	//ajax
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request!")
		return
	}
	var url=urlTS+"ajax/duplicate_therapist.php"
	url=url+"?id="+document.frmUser.hdnUserId.value;
	url=url+"&Email="+document.frmUser.Email.value;
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);
	if (xmlHttp.responseText == "exists"){
		document.getElementById('lblError').innerHTML = "Email is already in use!";
		return false;
	}
	//end ajax

	if (document.frmUser.Password.value == ''){
		document.getElementById('lblError').innerHTML = "Password is required!";
		return false;
	}
	if (document.frmUser.Password.value.length < 6){
		document.getElementById('lblError').trim().innerHTML = "Password must be a least 6 characters long!";
		return false;
	}
	
	if (document.frmUser.ConfirmPassword.value == ''){
		document.getElementById('lblError').innerHTML = "Confirm Password is required!";
		return false;
	}
	
	if (document.frmUser.Password.value != document.frmUser.ConfirmPassword.value){
		document.getElementById('lblError').innerHTML = "Passwords do not match!";
		return false;
	}
	
		
	//ajax
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request!")
		return
	}
	var url=urlTS+"ajax/save_user.php"
	url=url+"?id=" +document.frmUser.hdnUserId.value; 
	url=url+"&FirstName="+document.frmUser.FirstName.value;
	url=url+"&LastName="+document.frmUser.LastName.value;
	url=url+"&Email="+document.frmUser.Email.value;
	url=url+"&Password="+document.frmUser.Password.value;
    xmlHttp.open("GET",url,false);
	xmlHttp.send(null);
	if (xmlHttp.responseText != ""){
		document.getElementById('lblError').innerHTML = xmlHttp.responseText;
		return false;
	}
	//end ajax

	return true;	
}
function ValidateTherapist()
{
	if (document.frmTherapist.FirstName.value == ''){
		document.getElementById('lblError').innerHTML = "First Name is required!";
		return false;
	}
	if (document.frmTherapist.LastName.value == ''){
		document.getElementById('lblError').innerHTML = "Last Name is required!";
		return false;
	}
	
	if (document.frmTherapist.Email.value == ""){
		document.getElementById('lblError').innerHTML = "E-mail address is required!";
		return false;
	}
	
	if (!checkmail(document.frmTherapist.Email.value)){
	    document.getElementById('lblError').innerHTML = "Please enter a valid email address!";
		return false;
    }
	
	if (document.frmTherapist.ConfirmEmail.value == ""){
		document.getElementById('lblError').innerHTML = "Confirm E-mail is required!";
		return false;
	}
	
	if (document.frmTherapist.Email.value != document.frmTherapist.ConfirmEmail.value){
		document.getElementById('lblError').innerHTML = "Your E-mail Addresses do not match!";
		return false;
	}
	
	//ajax
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request!")
		return
	}
	var url=urlTS+"ajax/duplicate_user.php"
	url=url+"?id="+document.frmTherapist.id.value;
	url=url+"&Email="+document.frmTherapist.Email.value;
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);
	if (xmlHttp.responseText == "exists"){
		document.getElementById('lblError').innerHTML = "Email is already in use!";
		return false;
	}
	//end ajax
	//ajax
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
		alert ("Browser does not support HTTP Request!")
		return
	}
	var url=urlTS+"ajax/duplicate_therapist.php"
	url=url+"?id="+document.frmTherapist.id.value;
	url=url+"&Email="+document.frmTherapist.Email.value;
	xmlHttp.open("GET",url,false);
	xmlHttp.send(null);
	if (xmlHttp.responseText == "exists"){
		document.getElementById('lblError').innerHTML = "Email is already in use!";
		return false;
	}
	//end ajax
	
	if (document.frmTherapist.Password.value == ''){
		document.getElementById('lblError').innerHTML = "Password is required!";
		return false;
	}
	if (document.frmTherapist.Password.value.length < 6){
		document.getElementById('lblError').trim().innerHTML = "Password must be a least 6 characters long!";
		return false;
	}
	
	if (document.frmTherapist.ConfirmPassword.value == ''){
		document.getElementById('lblError').innerHTML = "Confirm Password is required!";
		return false;
	}
	
	if (document.frmTherapist.Password.value != document.frmTherapist.ConfirmPassword.value){
		document.getElementById('lblError').innerHTML = "Passwords do not match!";
		return false;
	}
	
	return true;	
}
function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) 
		 {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function
function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
} // Ends the "getSelectedCheckBoxValue" function		
//ajax


//=============HOME.php=====================================
function SearchbyLastName()
{
	//alert(document.frmSearchbyLastName.LastName.value);
	if (document.frmSearchbyLastName.LastName.value == '')
	{
		document.getElementById('lblErrorL').innerHTML = "Last Name is required!";
		return false;
	}	
}
function SearchbyCityCountyZip()
{
	//alert(document.frmSearchbyLastName.LastName.value);
	if (document.frmSearchbyCityCountyZip.CityCountyZip.value == '')
	{
		document.getElementById('lblErrorCCZ').innerHTML = " City,County or Zip is required!";
		return false;
	}	
}


//==================TherapistSearch.php=====================

function SearchbyLastNameState()
{
	//alert(document.frmSearchbyLastName.LastName.value);
	if (document.frmSearchbyLastNameState.LastName.value == '')
	{
		document.getElementById('lblErrorLS').innerHTML = "Last Name is required!";
		return false;
	}
	if (document.frmSearchbyLastNameState.State.value == '0')
	{
		document.getElementById('lblErrorLS').innerHTML = "State is required!";
		return false;
	}	
}

function SearchbyGeographicC()
{
	if ((document.frmSearchbyGeographicC.Miles.value == '') & (document.frmSearchbyGeographicC.Zip.value == '') & (document.frmSearchbyGeographicC.State.value == '0') & (document.frmSearchbyGeographicC.County.value == ''))
	{
		document.getElementById('lblErrorG').innerHTML = "Miles and Zip or State or County is required!";
		return false;
	}
}

function SearchbySpecialty()
{
	if (
			(document.frmSearchbySpecialty.AcceptingNewClients.value == '3') & 
			(document.frmSearchbySpecialty.ProfessionLicensure.value == '0') & 
			(document.frmSearchbySpecialty.FeeSchedule.value == '0') & 
			(document.frmSearchbySpecialty.Services.value == '0') & 
			(document.frmSearchbySpecialty.PracticeSpecInterests.value == '0') & 
			(document.frmSearchbySpecialty.TreatmentApproaches.value == '0') & 
			(document.frmSearchbySpecialty.AgeSpecialties.value == '0') & 
			(document.frmSearchbySpecialty.SessionFormat.value == '0') & 
			(document.frmSearchbySpecialty.Gender.value == '0') & 
			(document.frmSearchbySpecialty.Demographics.value == '0') & 
			(document.frmSearchbySpecialty.AcceptedInsurance.value == '0') & 
			(document.frmSearchbySpecialty.Languages.value == '0') 
		)
	{
		document.getElementById('lblErrorS').innerHTML = "You must select at least one specialty!";
		return false;
	}
	
}


function urlencode (str) 
{
    var hexStr = function (dec) 
	{
        return '%' + dec.toString(16).toUpperCase();
    };
 
    var ret = '',
            unreserved = /[\w.-]/; // A-Za-z0-9_.- // Tilde is not here for historical reasons; to preserve it, use rawurlencode instead
    str = (str+'').toString();
 
    for (var i = 0, dl = str.length; i < dl; i++) {
        var ch = str.charAt(i);
        if (unreserved.test(ch)) {
            ret += ch;
        }
        else {
            var code = str.charCodeAt(i);
            // Reserved assumed to be in UTF-8, as in PHP
            if (code === 32) {
                ret += '+'; // %20 in rawurlencode
            }
            else if (code < 128) { // 1 byte
                ret += hexStr(code);
            }
            else if (code >= 128 && code < 2048) { // 2 bytes
                ret += hexStr((code >> 6) | 0xC0);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 2048 && code < 65536) { // 3 bytes
                ret += hexStr((code >> 12) | 0xE0);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
            else if (code >= 65536) { // 4 bytes
                ret += hexStr((code >> 18) | 0xF0);
                ret += hexStr(((code >> 12) & 0x3F) | 0x80);
                ret += hexStr(((code >> 6) & 0x3F) | 0x80);
                ret += hexStr((code & 0x3F) | 0x80);
            }
        }
    }
    return ret;
}
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	 {
	 // Firefox, Opera 8.0+, Safari
	 xmlHttp=new XMLHttpRequest();
	 }
	catch (e)
	 {
	 //Internet Explorer
	 try
	  {
	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  }
	 catch (e)
	  {
	  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	 }
	return xmlHttp;
}
//end ajax
