function OnSubmitContactMain()
{
	var contactDate = getElementContact('contactDate');
    var txtFullName = getElementContact('txtFullName');
    var txtEmail = getElementContact('txtEmail');
    var txtPhone = getElementContact('txtPhone');
    var txtComments = getElementContact('txtComments');
    var txtDate = getElementContact('txtDate');
    var txtTime = getElementContact('txtTime');
    var txtCity = getElementContact('txtCity');
	var txtState = getElementContact('txtState');
	var txtZipCode = getElementContact('txtZipCode');
	
    //var rfvName = document.getElementById("rfvName");
    //var cvEmail = document.getElementById("cvEmail");
    //var cvPhone = document.getElementById("cvPhone");
    //var cvProcedures = document.getElementById("cvProcedures");
    //var cvComments = document.getElementById("cvComments");
    var cvDate = document.getElementById("cvDate");
    var cvTime = document.getElementById("cvTime");
    
    var message = "";
    
    txtFullName.value = trim(txtFullName.value);
    txtEmail.value = trim(txtEmail.value);
    txtPhone.value = trim(txtPhone.value);
    txtComments.value = trim(txtComments.value);
    txtDate.value = trim(txtDate.value);
    txtTime.value = trim(txtTime.value);
	txtCity.value  = trim(txtCity.value);
	txtState.value  = trim(txtState.value);
	txtZipCode.value  = trim(txtZipCode.value);
    
    //rfvName.style.display = "none";
    //cvEmail.style.display = "none";
    //cvPhone.style.display = "none";
    //cvProcedures.style.display = "none";
    //cvComments.style.display = "none";
    cvDate.style.display = "none";
    cvTime.style.display = "none";
    
    if (contactDate.value != "")
	{						
		return (false);
	}
	
	if(txtFullName.value == "")
	{
	    //rfvName.style.display = "";
		message += 'Please, enter your name.\r\n';
	}
	else
	{
		if(!ValidateName(txtFullName.value))
		{
			message += 'Please, check your name.\r\n';
		}
		else
		{
			if (!ValidateConsonants(txtFullName.value))
			{
				message += 'Please, check your name.\r\n';
			}
		}
	}
	
	if(txtEmail.value == "")
	{
	    //cvEmail.style.display = "";
	    message += 'Please, enter your email.\r\n';
	}
	else if(!ValidateEmail(txtEmail.value))
	{
	    //cvEmail.style.display = "";
	    message += 'Please check the emails address.\r\n';
	}
	
	var phone = ValidatePhone(txtPhone.value);
	if(phone == 1)
	{
	    //cvPhone.style.display = "";
	    message += 'Please, enter your phone number.\r\n';
	}
	else if(phone == 2)
	{
	    //cvPhone.style.display = "";
	    message += 'Please, check your phone number.\r\n';
	}
	
	if (txtCity.value!="")
	{
		if (!ValidateCityState(txtCity.value))
		{
			message += 'Please, check your city.\r\n';
		}
	}
	if (txtState.value!="")
	{
		if (!ValidateCityState(txtState.value))
		{
			message += 'Please, check your state.\r\n';
		}
	}
	
	if (txtZipCode.value!="")
	{
		if (!ValidateAreaCode(txtZipCode.value))
		{
			message += 'Please, check your zip.\r\n';
		}
	}
	
	if(!ValidateProcedures())
	{
	    //cvProcedures.style.display = "";
	    message += 'Please, select at least one option.\r\n';
	}
	
	if(txtComments.value == "")
	{
	    //cvComments.style.display = "";
		message += 'Please, enter your questions or comments.\r\n';
	}
	
	if(!ValidateDateM(txtDate.value, txtTime.value))
	{
	    cvDate.style.display = "";
	    message += 'Please, enter a valid date.\r\n';
	}
	
	if(!ValidateTimeM(txtTime.value, txtDate.value))
	{
	    cvTime.style.display = "";
	    message += 'Please, enter a valid time.';
	}
	
	if(message != "")
	{
		alert(message);
	    return false;
	}
	
	return true;
}

function ProcessMain()
{
	//var form = document.forms.commentform;
    var formMain = document.forms.contactmain;
    //var fullname = form.txtFullName.value;    
    //formMain.contact_greeting.value = trim(fullname);
    //formMain.contact_email.value = form.txtEmail.value;
    //formMain.contact_home_phone.value = form.txtPhone.value;
    //formMain.contact_comment.value = form.txtComments.value;
    formMain.submit();
    //window.location = "/success.php";
}

function ValidateName(valor) 
{
      	valor = valor.toLowerCase();
	if (/^[a-z ]+$/i.test(valor))
		return true;
	else
		return false;
} 

function ValidateConsonants(valor) 
{
       valor = valor.toLowerCase();
	if (/[bcdfghjklmnpqrstvwxyz]{7}/.test(valor))
		return false;
	else
		return true;
} 

function ValidateEmail(valor) 
{    
	var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]{2,60}(\.[a-zA-Z]{2,4}){1,2}$/;  
    return emailPattern.test(valor); 
}


function ValidatePhone(phone)
{
    var ValidChars = "0123456789.()- ";
    var IsCorrect=true;
    var Char;
    var IsValid=false;
    
    if (phone == "")
    {
        return 1;
    }
    else
    {
        for (cont = 0; cont < phone.length && IsCorrect == true; cont++) 
        { 
            Char = phone.charAt(cont); 
            if (ValidChars.indexOf(Char) == -1) {
                 return 2;
            }
        }
    }
    return 0
}

function ValidateAreaCode(incoming)
{
	var ValidChars = "0123456789";			
	var Char;
	var IsCorrect=true;
	
	for (cont = 0; cont < incoming.length && IsCorrect == true; cont++) 
	{ 
		Char = incoming.charAt(cont); 
		if (ValidChars.indexOf(Char) == -1) {
			 
			 return false;
		}
	}
	return true;
}

function CustomValidatePhone(source, arguments)
{
    var ValidChars = "0123456789.()- ";
    var IsCorrect=true;
    var Char;
    var IsValid=false;
    incoming= document.getElementById(source.controltovalidate).value;
    
    if (incoming == "")
    {
        source.errormessage = 'Please, enter your phone number.';   
        arguments.IsValid=false;
        return;
    }
    else
    {
        for (cont = 0; cont < incoming.length && IsCorrect == true; cont++) 
        { 
            Char = incoming.charAt(cont); 
            if (ValidChars.indexOf(Char) == -1) {
                 source.errormessage = 'Please, check your phone number.';
                 arguments.IsValid= false;
                 return;
            }
        }
    }
    arguments.IsValid= true;
}

function CustomValidateEmail(source, arguments)
{
    if(arguments)
    {
        var Email = getElementContact(source.controltovalidate);
        if (Email.value == "")
        {
	        source.errormessage = 'Please, enter your email.';
    	   
	        arguments.IsValid=false;
	        return;
        }
        else
        {
        if(!ValidateEmail(Email.value))
	        {
		        source.errormessage = "Please check the emails address.";
    		 
		        arguments.IsValid=false;
		        return;
	        }
        }
        arguments.IsValid=true;

    }    
}

function CustomValidateComments(source, arguments)
{
    var Comment = getElementContact(source.controltovalidate);
    if (Comment.value == "")
    {
	    source.errormessage = 'Please, enter your questions or comments.';
	    //Comment.focus();
	    arguments.IsValid=false;
	    return;
    }		
    arguments.IsValid=true;
}

function ValidateProcedures()
{
    if (!checkBoxListChecked("cblTreatments") && !checkBoxListChecked("cblSkinCareProducts") && !checkBoxListChecked("cblWrinkleFillers"))
    {
        return false;
	}
    return true;
}

function CustomValidateProcedures(source, arguments)
{
    if (!checkBoxListChecked("cblTreatments") && !checkBoxListChecked("cblSkinCareProducts") && !checkBoxListChecked("cblWrinkleFillers"))
    {
        source.errormessage = 'Please, select at least one option.';
	    arguments.IsValid=false;
	    return;
	}
    arguments.IsValid=true;
}

function ValidateDateM(dateValue, timeValue)
{
	if (dateValue == "" && timeValue == "")
    {
        return true;
    }
    else if (!validateDate(dateValue))
    {
        return false;
	}
    return true;
}

function CustomValidateDate(source, arguments)
{
    var timeValue = document.getElementById("appttime").value;
    var dateValue = getElementContact(source.controltovalidate).value;
    
    if (trim(dateValue) == "" && trim(timeValue) == "")
    {
        arguments.IsValid=true;
        return;
    }
    else if (!validateDate(dateValue))
    {
        source.errormessage = 'Please, enter a valid date.';
	    arguments.IsValid=false;
	    return;
	}
    arguments.IsValid=true;
}

function ValidateTimeM(timeValue, dateValue)
{
	if (dateValue == "" && timeValue == "")
    {
        return true;
    }
    else if (!validateTime(timeValue))
    {
        return false;
	}
    return true;
}

function CustomValidateTime(source, arguments)
{
    var dateValue = document.getElementById("txtDate").value;
    var timeValue = getElementContact(source.controltovalidate).value;
    
    if (trim(dateValue) == "" && trim(timeValue) == "")
    {
        arguments.IsValid=true;
        return;
    }
    else if (!validateTime(timeValue))
    {
        source.errormessage = 'Please, enter a valid time.';
	    arguments.IsValid=false;
	    return;
	}
    arguments.IsValid=true;
}

function getElementContact(name)
{
    var object = null;
    var tbSmallContact=document.getElementById('tbContact'); 
    for (var i=0; i<tbSmallContact.rows.length; i++)
    {
        for (var j=0; j<tbSmallContact.rows[i].cells.length; j++)
        {
            for (var k=0; k<tbSmallContact.rows[i].cells[j].childNodes.length; k++)
            {                
                if(tbSmallContact.rows[i].cells[j].childNodes[k].id == name)
                {
                   object = tbSmallContact.rows[i].cells[j].childNodes[k];
                   return object;
                }
            }
        }
       
    }    
}

function checkBoxListChecked(name)
{
    var checkBoxName = "";
    var object = null;
    var tbCheckBoxList = document.getElementById(name); 
    for (var i=0; i < tbCheckBoxList.rows.length; i++)
    {
        for (var j=0; j < tbCheckBoxList.rows[i].cells.length; j++)
        {
            for (var k=0; k < tbCheckBoxList.rows[i].cells[j].childNodes.length; k++)
            {
                checkBoxName = name + "_" + i;
                if(tbCheckBoxList.rows[i].cells[j].childNodes[k].id == checkBoxName)
                {
                   object = tbCheckBoxList.rows[i].cells[j].childNodes[k];
                   if (object.checked)
                        return true;
                }
            }
        }
       
    }
    return false;
}

function isUnsignedInteger(s)
{
    return (s.toString().search(/^[0-9]+$/) == 0);
}

function isCorrectDate(dateValue)
{
    var arrDate = dateValue.split("-");
    var dateMonth = ltrim(arrDate[0], "0");
    var dateDay = ltrim(arrDate[1], "0");
    var dateYear = ltrim(arrDate[2], "0");
    
    if(parseInt(dateMonth) <= 0 || parseInt(dateDay) <= 0 || parseInt(dateYear) <= 0)
    {
        return false;
    }

    if(dateDay > 31 || dateMonth > 12)
    {
        return false;
    }
    else if(dateMonth == 4 || dateMonth == 6 || dateMonth == 9 || dateMonth == 4)
    {
        if(dateDay > 30)
        {
            return false;
        }
    }
    else if(dateMonth == 2)
    {
        if(dateDay > 29)
        {
            return false;
        }
        else if((dateYear % 4 != 0) && (dateDay == 29))
        {
            return false;
        }
    }
    return true;
}

function isCorrectTime(timeValue)
{
    var arrTime = timeValue.split(":");
    var hour = ltrim(arrTime[0], "0");
    var minute = ltrim(arrTime[1], "0");
    
    if(parseInt(hour) <= 0 || parseInt(hour) > 12)
    {
        return false;
    }

    if(parseInt(minute) < 0 || parseInt(minute) > 59)
    {
        return false;
    }
    
    return true;
}

function compareDate(date1, date2)
{
    fec = date1.split("-");
    fec1 = date2.split("-");
    if(parseInt(ltrim(fec[2], "0")) > parseInt(ltrim(fec1[2], "0")))
    {
        return 1;
    }
    else if(parseInt(ltrim(fec[2], "0")) < parseInt(ltrim(fec1[2], "0")))
    {
        return -1;
    }
    else
    {
        if(parseInt(ltrim(fec[0], "0")) > parseInt(ltrim(fec1[0], "0")))
        {
            return 1;
        }
        else if(parseInt(ltrim(fec[0], "0")) < parseInt(ltrim(fec1[0], "0")))
        {
            return -1;
        }
        else
        {
            if(parseInt(ltrim(fec[1], "0")) > parseInt(ltrim(fec1[1], "0")))
            {
                return 1;
            }
            else if(parseInt(ltrim(fec[1], "0")) < parseInt(ltrim(fec1[1], "0")))
            {
                return -1;
            }
            else
            {
                return 0;
            }
        }
    }
}

function validateDate(dateValue)
{
    var arrDate = dateValue.split("-");
    if(arrDate.length < 3 || arrDate.length > 3)
        return false
    else
    {
        var dateMonth = arrDate[0];
        var dateDay = arrDate[1];
        var dateYear = arrDate[2];

        var today = new Date();
        var day = today.getDate();
        var month = today.getMonth() + 1;
        var year  = today.getYear();
        if (navigator.appName == "Netscape")
            year = year + 1900;

        var todayString = month + "-" + day + "-" + year;
        
        if(!isUnsignedInteger(dateMonth) || !isUnsignedInteger(dateDay) || !isUnsignedInteger(dateYear))
        {
			return false;
        }
        else if(!isCorrectDate(dateValue))
        {
			return false;
        }
        else if(compareDate(todayString, dateValue) > 0)
        {
			return false;
        }
        return true;
    }
}

function validateTime(timeValue)
{
    var arrTime = timeValue.split(":");
    if(arrTime.length < 2 || arrTime.length > 2)
        return false
    else
    {
        var hour = arrTime[0];
        var minute = arrTime[1].replace(" a.m.", "").replace(" p.m.", "");

		if(!isUnsignedInteger(hour) || !isUnsignedInteger(minute))
        {
			return false;
        }
        else if(!isCorrectTime(timeValue))
        {
			return false;
        }
        return true;
    }
}

function ValidateCityState(valor) 
{
	valor = valor.toLowerCase();
	if (/^[a-z ]+$/i.test(valor))
		return true;
	else
		return false;
}

function InsertDate(evt)
{
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
    var respuesta = true;
    
    if (charCode > 31 && (charCode < 48 || charCode > 57))
    {
        if(charCode == 37 || charCode == 39 || charCode == 45 || charCode == 46)
			respuesta = true;
		else
			respuesta = false;
    }

    return respuesta;
}

function InsertTime(evt)
{
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
    var respuesta = true;
    
    if (charCode > 31 && (charCode < 48 || charCode > 57))
    {
        if(charCode == 37 || charCode == 39 || charCode == 46 || charCode == 58)
			respuesta = true;
		else
			respuesta = false;
    }

    return respuesta;
}

function SetTimeValue(timeValue)
{
    var ampm = document.getElementById("ddlampm").value;
    var timeControl = document.getElementById("txtTime");
    if(timeValue.value == "")
        timeControl.value = "";
    else
        timeControl.value = timeValue.value + " " + ampm;
}

function trim(myString)
{
 return myString.replace(/^\s+/g,'').replace(/\s+$/g,'')
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function SetValueCheckBox(checkbox)
{ 
    checkbox.value="No";
    if (checkbox.checked)
        checkbox.value="Yes";
}

