/*Function to replace white space*/
function trim(str)
{
    return str.replace(/^\s+|\s+$/, '');
}
function getSubPractice()
{
	var checkValue = "";
	for(var j=0; j < document.frmStep2.prac_area_c.length; j++)
	{
		if(document.frmStep2.prac_area_c[j].checked)
		{
			var checkValue = checkValue + document.frmStep2.prac_area_c[j].value + '^';
		}
	}
    document.getElementById('light').style.display='block';
    document.getElementById('fade').style.display='block';
    $.ajax({
        url: 'getSubPractice.php?<?=time()?>',
        type: 'POST',
        data: 'pp='+checkValue,
        dataType: 'html',
        //timeout: 1000,
        error: function(){
            document.getElementById('light').style.display='none';
            document.getElementById('fade').style.display='none';
            alert('Error Loading Page.');
        },
        success: function(data){
            document.getElementById('light').style.display='none';
            document.getElementById('fade').style.display='none';
			//alert(data);
            $('#mainBody').html(data);
        }
    });
}

function getSearchPrac()
{
    document.getElementById('light').style.display='block';
    document.getElementById('fade').style.display='block';
    $.ajax({
        url: 'getSearchSubPractice.php?<?=time()?>',
        type: 'POST',
        data: 'pp='+document.getElementById('prac_area').value,
        dataType: 'html',
        //timeout: 1000,
        error: function(){
            document.getElementById('light').style.display='none';
            document.getElementById('fade').style.display='none';
            alert('Error Loading Page.');
        },
        success: function(data){
            document.getElementById('light').style.display='none';
            document.getElementById('fade').style.display='none';
			//alert(data);
            $('#sCat').html(data);
        }
    });
}

function getSearchPrac_edit(param)
{
    document.getElementById('light').style.display='block';
    document.getElementById('fade').style.display='block';
    $.ajax({
        url: 'getSearchSubPractice.php?<?=time()?>',
        type: 'POST',
        data: 'pp='+document.getElementById('prac_area').value+'&sp='+param,
        dataType: 'html',
        //timeout: 1000,
        error: function(){
            document.getElementById('light').style.display='none';
            document.getElementById('fade').style.display='none';
            alert('Error Loading Page.');
        },
        success: function(data){
            document.getElementById('light').style.display='none';
            document.getElementById('fade').style.display='none';
			//alert(data);
            $('#sCat').html(data);
        }
    });
}

function formValidation()
{
	 if(document.getElementById("month").value=='')
    {
        alert('please select date of birth!');
        document.getElementById("month").focus();
        return false;
    }
	 if(document.getElementById("day").value=='')
    {
        alert('please select date of birth!');
        document.getElementById("day").focus();
        return false;
    }
	 if(document.getElementById("year").value=='')
    {
        alert('please select date of birth!');
        document.getElementById("year").focus();
        return false;
    }
	if(trim(document.getElementById("attorney").value)=='')
    {
        alert('please enter attorney ID!');
        document.getElementById("attorney").focus();
        return false;
    }
	if(trim(document.getElementById("compliance").value)=='')
    {
        alert('please enter compliance group!');
        document.getElementById("compliance").focus();
        return false;
    }
	if(trim(document.getElementById("first_name").value)=='')
    {
        alert('please enter first name!');
        document.getElementById("first_name").focus();
        return false;
    }
	if(trim(document.getElementById("last_name").value)=='')
    {
        alert('please enter last name!');
        document.getElementById("last_name").focus();
        return false;
    }
	if(trim(document.getElementById("organization").value)=='')
    {
        alert('please enter law firm!');
        document.getElementById("organization").focus();
        return false;
    }
    if(trim(document.getElementById("address").value)=='')
    {
        alert('please enter address!');
        document.getElementById("address").focus();
        return false;
    }
	if(trim(document.getElementById("city").value)=='')
    {
        alert('please enter city!');
        document.getElementById("city").focus();
        return false;
    }
	if(trim(document.getElementById("state").value)=='')
    {
        alert('please enter state!');
        document.getElementById("state").focus();
        return false;
    }
	if(trim(document.getElementById("zip").value)=='')
    {
        alert('please enter zip!');
        document.getElementById("zip").focus();
        return false;
    }
	if(trim(document.getElementById("phone").value)=='')
    {
        alert('please enter phone!');
        document.getElementById("phone").focus();
        return false;
    }
	if(trim(document.getElementById("email").value)=='')
    {
        alert('please enter email!');
        document.getElementById("email").focus();
        return false;
    }
    if(!isValidEmail(trim(document.getElementById("email").value)))
    {
        document.getElementById("email").focus();
        return false;
    }
	
	if(trim(document.getElementById("home_address").value)=='')
    {
        alert('please enter home address!');
        document.getElementById("home_address").focus();
        return false;
    }
	if(trim(document.getElementById("city_2").value)=='')
    {
        alert('please enter home city!');
        document.getElementById("city_2").focus();
        return false;
    }
	if(trim(document.getElementById("state_2").value)=='')
    {
        alert('please enter home state!');
        document.getElementById("state_2").focus();
        return false;
    }
	if(trim(document.getElementById("zip_2").value)=='')
    {
        alert('please enter home zip!');
        document.getElementById("zip_2").focus();
        return false;
    }
	if(trim(document.getElementById("home_phone").value)=='')
    {
        alert('please enter home phone!');
        document.getElementById("home_phone").focus();
        return false;
    }
	if(trim(document.getElementById("emergency_contact").value)=='')
    {
        alert('please enter emergency_contact!');
        document.getElementById("emergency_contact").focus();
        return false;
    }
	if(trim(document.getElementById("phone_2").value)=='')
    {
        alert('please enter home phone!');
        document.getElementById("phone_2").focus();
        return false;
    }
	
	
	if(trim(document.getElementById("college").value)=='')
    {
        alert('please enter college!');
        document.getElementById("college").focus();
        return false;
    }
	if(document.getElementById("college_year").value=='')
    {
        alert('please select college year!');
        document.getElementById("college_year").focus();
        return false;
    }
	if(trim(document.getElementById("law_school").value)=='')
    {
        alert('please enter law school!');
        document.getElementById("law_school").focus();
        return false;
    }
	if(document.getElementById("law_year").value=='')
    {
        alert('please select law school year!');
        document.getElementById("law_year").focus();
        return false;
    }
	document.frmStep1.submit();
}

/*Function to check Email Validation*/
function isValidEmail(emstr)
{
    var vEMailID=emstr
    var atPos= vEMailID.indexOf("@") + 1
    var lastDotPos= vEMailID.lastIndexOf(".") + 1
    var firstDotPos = vEMailID.indexOf(".") + 1
    var atInstances=0
    var dotInstances=0
    var inValid = new Array()
    var errorMessages = new Array()
    var errNo = 0

    for(var i=0;i<vEMailID.length;i++)
    {
        if(vEMailID.charAt(i)=="@" )
            atInstances+=1
        else if(vEMailID.charAt(i)==".")
            dotInstances=1
    }

    inValid[errNo] = (atInstances==0)
    errorMessages[errNo] = "@ symbol is missing"
    errNo++

    inValid[errNo] = (dotInstances==0)
    errorMessages[errNo] = ". symbol is missing"
    errNo++

    inValid[errNo] = (atInstances>1)
    errorMessages[errNo] = "@ symbol should not repeat"
    errNo++

    inValid[errNo] = (Math.abs(firstDotPos-atPos)==1)
    errorMessages[errNo] = "@ and . symbols can't come together"
    errNo++

    inValid[errNo] = ((lastDotPos-atPos)<=1)
    errorMessages[errNo] = ". symbol is missing"
    errNo++

    inValid[errNo] = (atPos==vEMailID.length)
    errorMessages[errNo] = "@ symbol can't appear at end"
    errNo++

    inValid[errNo] = (lastDotPos==vEMailID.length)
    errorMessages[errNo] = ". symbol can't appear at end"
    errNo++

    inValid[errNo] = (atPos==0)
    errorMessages[errNo] = "@ symbol is missing"
    errNo++

    inValid[errNo] = (atPos==1)
    errorMessages[errNo] = "@ symbol should not appear at first position"
    errNo++

    inValid[errNo] = (firstDotPos==0)
    errorMessages[errNo] = ". symbol is missing"
    errNo++

    inValid[errNo] = (firstDotPos==1)
    errorMessages[errNo] = ". symbol should not appear at first position"
    errNo++

    inValid[errNo] = (vEMailID.indexOf(" ")>=0)
    errorMessages[errNo] = "E-mail can't contain blank spaces."
    errNo++

    inValid[errNo] = (vEMailID.indexOf("/")>=0) || (vEMailID.indexOf("\\")>=0)
    errorMessages[errNo] = "E-mail can't contain \\ or /"
    errNo++

    var fullErrorMessage = "Invalid " + name + " email address.\n"
    for(var ind=0; ind<errNo; ind++)
    {
        if(inValid[ind])
        {
            fullErrorMessage += errorMessages[ind] + "\n"
            alert(fullErrorMessage)
            return false
        }
    }
    var SingleQuotePos = vEMailID.indexOf("'")
    if(SingleQuotePos!=-1)
    {
        alert("Enter valid characters.Single Quotes ( ' ) are not allowed.")
        return false
    }

    return true;
}

function formValidation2()
{
	/*for (var i=0; i < document.frmStep2.prac_area.length; i++)
	{
		if (document.frmStep2.prac_area[i].checked)
		{
			var rad_val = document.frmStep2.prac_area[i].value;
		}
	}
	if(rad_val)
	{
		
	}else{
		alert('please select practice area!');
		return false;
	}*/
   
	if(trim(document.getElementById("username").value)=='')
    {
        alert('please enter username!');
        document.getElementById("username").focus();
        return false;
    }
	if(trim(document.getElementById("password").value)=='')
    {
        alert('please enter password!');
        document.getElementById("password").focus();
        return false;
    }
	if(trim(document.getElementById("confirm").value)=='')
    {
        alert('please enter confirm password!');
        document.getElementById("confirm").focus();
        return false;
    }
	if(trim(document.getElementById("password").value)!=trim(document.getElementById("confirm").value))
	{
		alert('password mismatch!');
		return false;
	}
	//return getUserName(trim(document.getElementById("username").value));
}

function getUserName()
{

    document.getElementById('light').style.display='block';
    document.getElementById('fade').style.display='block';
    $.ajax({
        url: 'getUserName.php?<?=time()?>',
        type: 'POST',
        data: 'un='+trim(document.getElementById("username").value),
        dataType: 'html',
        //timeout: 1000,
        error: function(){
            document.getElementById('light').style.display='none';
            document.getElementById('fade').style.display='none';
            alert('Error Loading Page.');
        },
        success: function(data){
            document.getElementById('light').style.display='none';
            document.getElementById('fade').style.display='none';
			if(data=='Y')
			{
				return false;
			}else{
				document.getElementById("username").value='';
				$('#mainBodyUN').html(data);
			}
        }
    });
}

function validation()
{
	/*Check Name is blank or not*/
	if(trim(document.getElementById('name').value)=='')
	{
		document.getElementById('name').className = 'inputBoxError';
		document.getElementById('errName').style.display = 'block';
		document.getElementById('errName').innerHTML = 'Enter your name. This is a required field.';
		return false;
	}else{
		document.getElementById('name').className = 'txtField';
		document.getElementById('errName').style.display = 'none';
		document.getElementById('errName').innerHTML = '';
	}
	
	/*Check Email is blank or not*/
	if(trim(document.getElementById('email').value)=='')
	{
		document.getElementById('email').className = 'inputBoxError';
		document.getElementById('errEmail').style.display = 'block';
		document.getElementById('errEmail').innerHTML = 'Enter your email. This is a required field.';
		return false;
	}else{
		document.getElementById('email').className = 'txtField';
		document.getElementById('errEmail').style.display = 'none';
		document.getElementById('errEmail').innerHTML = '';
	}
	
	/*Email Validation*/
	if(!isValidEmail(trim(document.getElementById('email').value)))
	{
		document.getElementById('email').className = 'inputBoxError';
		document.getElementById('errEmail').style.display = 'block';
		document.getElementById('errEmail').innerHTML = 'Enter email is not valid.';
		return false;
	}else{
		document.getElementById('email').className = 'txtField';
		document.getElementById('errEmail').style.display = 'none';
		document.getElementById('errEmail').innerHTML = '';
	}
	
	/*Message*/
	if(trim(document.getElementById('message').value)=='')
	{
		document.getElementById('message').className = 'inputBoxError';
		document.getElementById('errMessage').style.display = 'block';
		document.getElementById('errMessage').innerHTML = 'Enter your message. This is a required field.';
		return false;
	}else{
		document.getElementById('message').className = 'txtField';
		document.getElementById('errMessage').style.display = 'none';
		document.getElementById('errMessage').innerHTML = '';
	}
	
	/*Message*/
	if(trim(document.getElementById('captcha-form').value)=='')
	{
		document.getElementById('captcha-form').className = 'inputBoxError';
		document.getElementById('errCaptcha').style.display = 'block';
		document.getElementById('errCaptcha').innerHTML = 'Enter captcha code. This is a required field.';
		return false;
	}else{
		document.getElementById('captcha-form').className = 'txtField';
		document.getElementById('errCaptcha').style.display = 'none';
		document.getElementById('errCaptcha').innerHTML = '';
	}

}

function resetValidation()
{
	document.getElementById('name').className = 'txtField';
	document.getElementById('errName').style.display = 'none';
	document.getElementById('errName').innerHTML = '';
	
	document.getElementById('email').className = 'txtField';
	document.getElementById('errEmail').style.display = 'none';
	document.getElementById('errEmail').innerHTML = '';
	
	document.getElementById('message').className = 'txtField';
	document.getElementById('errMessage').style.display = 'none';
	document.getElementById('errMessage').innerHTML = '';
	
	document.getElementById('captcha-form').className = 'txtField';
	document.getElementById('errCaptcha').style.display = 'none';
	document.getElementById('errCaptcha').innerHTML = '';
}

function change_password()
{
	if(trim(document.getElementById("old_pass").value)=='')
    {
        alert('please enter old password!');
        document.getElementById("old_pass").focus();
        return false;
    }
	if(trim(document.getElementById("new_pass").value)=='')
    {
        alert('please enter new password!');
        document.getElementById("new_pass").focus();
        return false;
    }
	if(trim(document.getElementById("cnf_pass").value)=='')
    {
        alert('please enter confirm password!');
        document.getElementById("cnf_pass").focus();
        return false;
    }
	if(trim(document.getElementById("new_pass").value) != trim(document.getElementById("cnf_pass").value))
    {
        alert('Password mismatch!');
        return false;
    }
}

function loginVal()
{
	if(trim(document.getElementById("username_l").value)=='')
    {
        alert('please enter username!');
        document.getElementById("username_l").focus();
        return false;
    }
	if(trim(document.getElementById("password_l").value)=='')
    {
        alert('please enter password!');
        document.getElementById("password_l").focus();
        return false;
    }
}
function fPass()
{
	if(trim(document.getElementById("username_f").value)=='')
    {
        alert('please enter username!');
        document.getElementById("username_f").focus();
        return false;
    }
	if(trim(document.getElementById("email_f").value)=='')
    {
        alert('please enter email!');
        document.getElementById("email_f").focus();
        return false;
    }
	if(!isValidEmail(trim(document.getElementById("email_f").value)))
    {
        document.getElementById("email_f").focus();
        return false;
    }
}

function sendMailInvitaion(fname,cemail)
{
	if(trim(document.getElementById('m_name').value)=='')
	{
		alert('Please enter your name');
		document.getElementById('m_name').focus();
		return false;
	}
	if(trim(document.getElementById('m_email').value)=='')
	{
		alert('Please enter your email');
		document.getElementById('m_email').focus();
		return false;
	}
	if(!isValidEmail(trim(document.getElementById("m_email").value)))
    {
        document.getElementById("m_email").focus();
        return false;
    }
	if(trim(document.getElementById('m_phone').value)=='')
	{
		alert('Please enter your phone');
		document.getElementById('m_phone').focus();
		return false;
	}
	if(trim(document.getElementById('m_message').value)=='')
	{
		alert('Please enter message');
		document.getElementById('m_message').focus();
		return false;
	}
	
    $.ajax({
        url: 'sendMail.php?<?=time()?>',
        type: 'POST',
        data: 'm_name='+document.getElementById('m_name').value+'&m_email='+document.getElementById('m_email').value+'&m_com='+document.getElementById('m_com').value+'&m_phone='+document.getElementById('m_phone').value+'&m_message='+document.getElementById('m_message').value+'&cname='+fname+'&cemail='+cemail,
        dataType: 'html',
        //timeout: 1000,
        error: function(){
            alert('Error Loading Page.');
        },
        success: function(data){
			document.getElementById('m_name').value='';
			document.getElementById('m_email').value='';
			document.getElementById('m_phone').value='';
			document.getElementById('m_com').value='';
			document.getElementById('m_message').value='';
            $('#mMailSend').html(data);
        }
    });
}

function editValidate()
{
	if(document.getElementById("month").value=='')
    {
        alert('please select birth month!');
        document.getElementById("month").focus();
        return false;
    }
	 if(document.getElementById("day").value=='')
    {
        alert('please select birth day!');
        document.getElementById("day").focus();
        return false;
    }
	 if(document.getElementById("year").value=='')
    {
        alert('please select birth year!');
        document.getElementById("year").focus();
        return false;
    }
	if(trim(document.getElementById("attorney").value)=='')
    {
        alert('please enter attorney ID!');
        document.getElementById("attorney").focus();
        return false;
    }
	if(trim(document.getElementById("compliance").value)=='')
    {
        alert('please enter compliance group!');
        document.getElementById("compliance").focus();
        return false;
    }
	if(trim(document.getElementById("first_name").value)=='')
    {
        alert('please enter first name!');
        document.getElementById("first_name").focus();
        return false;
    }
	if(trim(document.getElementById("last_name").value)=='')
    {
        alert('please enter last name!');
        document.getElementById("last_name").focus();
        return false;
    }
	if(trim(document.getElementById("organization").value)=='')
    {
        alert('please enter lew firm!');
        document.getElementById("organization").focus();
        return false;
    }
    if(trim(document.getElementById("address").value)=='')
    {
        alert('please enter address!');
        document.getElementById("address").focus();
        return false;
    }
	if(trim(document.getElementById("city").value)=='')
    {
        alert('please enter city!');
        document.getElementById("city").focus();
        return false;
    }
	if(trim(document.getElementById("state").value)=='')
    {
        alert('please enter state!');
        document.getElementById("state").focus();
        return false;
    }
	if(trim(document.getElementById("zip").value)=='')
    {
        alert('please enter zip!');
        document.getElementById("zip").focus();
        return false;
    }
	if(trim(document.getElementById("phone").value)=='')
    {
        alert('please enter phone!');
        document.getElementById("phone").focus();
        return false;
    }
	if(trim(document.getElementById("email").value)=='')
    {
        alert('please enter email!');
        document.getElementById("email").focus();
        return false;
    }
    if(!isValidEmail(trim(document.getElementById("email").value)))
    {
        document.getElementById("email").focus();
        return false;
    }
	
	if(trim(document.getElementById("home_address").value)=='')
    {
        alert('please enter home address!');
        document.getElementById("home_address").focus();
        return false;
    }
	if(trim(document.getElementById("city_2").value)=='')
    {
        alert('please enter home city!');
        document.getElementById("city_2").focus();
        return false;
    }
	if(trim(document.getElementById("state_2").value)=='')
    {
        alert('please enter home state!');
        document.getElementById("state_2").focus();
        return false;
    }
	if(trim(document.getElementById("zip_2").value)=='')
    {
        alert('please enter home zip!');
        document.getElementById("zip_2").focus();
        return false;
    }
	if(trim(document.getElementById("home_phone").value)=='')
    {
        alert('please enter home phone!');
        document.getElementById("home_phone").focus();
        return false;
    }
	if(trim(document.getElementById("emergency_contact").value)=='')
    {
        alert('please enter emergency_contact!');
        document.getElementById("emergency_contact").focus();
        return false;
    }
	if(trim(document.getElementById("phone_2").value)=='')
    {
        alert('please enter home phone!');
        document.getElementById("phone_2").focus();
        return false;
    }
	
	
	if(trim(document.getElementById("college").value)=='')
    {
        alert('please enter college!');
        document.getElementById("college").focus();
        return false;
    }
	if(document.getElementById("college_year").value=='')
    {
        alert('please select college year!');
        document.getElementById("college_year").focus();
        return false;
    }
	if(trim(document.getElementById("law_school").value)=='')
    {
        alert('please enter law school!');
        document.getElementById("law_school").focus();
        return false;
    }
	if(document.getElementById("law_year").value=='')
    {
        alert('please select law school year!');
        document.getElementById("law_year").focus();
        return false;
    }
}
