/*
* @File		 	patient-registration.js
* @Author	 	Rohitsing
* @Created	 	11-Dec-2008
* @Description  js file for patient-registration.html.php
*/				

function open_win(url_add)
{
	//function to open new window with given position
	window.open(url_add,'Terms','width=650,height=400,left=300,top=170,scrollbars=yes');
}

//function to hide or show states
	function fnStates()
	{
		if($("#sltCountry").val()==229)
		{
			$("#tdUSState").show();
			$("#tdTxtState").hide();
		}
		else
		{
			$("#tdUSState").hide();
			$("#tdTxtState").show();
		}
		
	}//end function
	

$().ready(function()
{
	
	$('#txtBirthDate').datepicker();//apply datepicker to birth date txt box
	
	
	$("#btnPatientRegistration").click(function()
	{
		var errorFlag = 0; 
		
		if($("#txtUsername").val()=='')//check username is empty!!
		{
			alert("Please enter username");
			$("#txtUsername").focus();
			return false;
		}
		if($('#txtUsername').val().length<6)//check length of username
		{
			alert('Username must be at least 6 characters long');
			$("#txtUsername").focus();
			return false;				
		}
		if($("#txtPassword").val()=='')//check password is empty!!
	 	{
			alert("Please enter password");
			$("#txtPassword").focus();
			return false;
		}
		if($('#txtPassword').val().length<6)//check length of password
		{
			alert('Password must be at least 6 characters long');
			$("#txtPassword").focus();
			return false;				
		}
		if($("#txtConfirmPassword").val()=='')//check confirm password is empty!!
	 	{
			alert("Please enter confirm Password");
			$("#txtConfirmPassword").focus();
			return false;
		}
		if(($("#txtPassword").val()!='') && ($("#txtConfirmPAssword").val()!=''))//compare password with confirm password
		{
			if(($("#txtPassword").val()!=$("#txtConfirmPassword").val()))
			{
				alert('Password and confirm password must be same');
				$("#txtConfirmPassword").focus();
				return false;
			}
		}	
		if($("#txtFirstName").val()=='')//check first name is empty!!
	 	{
			alert("Please enter first name");
			$("#txtFirstName").focus();
			return false;
		}
		if($("#txtLastName").val()=='')//check last name is empty!!
	 	{
			alert("Please enter last name");
			$("#txtLastName").focus();
			return false;
		}
		var value = $("input[@name='radGender']:checked").val();
		if(value != 'F' && value != 'M')//check last name is empty!!
	 	{
			alert("Please select gender");
			return false;
		}
		if($("#txtBirthDate").val()=='')//check birth date is empty!!
	 	{
			alert("Please enter birth date");
			$("#txtBirthDate").focus();
			return false;
		}
		var itemdate = $("#txtBirthDate").val();
		var d = new Date();
		var curr_date = d.getDate();
		var curr_month = d.getMonth();
		curr_month++;
		var curr_year = d.getFullYear();
		var todaydate=curr_month + "-" + curr_date + "-" + curr_year;
		if(todaydate!='')
		var result=date_range_validation(todaydate,itemdate);
		if(result==0)
		 {
			alert("Please enter valid birth date");
			$('#txtBirthDate').focus();
			return false;
		 }	
		if($("#txtEmail").val()=='')//check email is empty!!
	 	{
			alert("Please enter email");
			$("#txtEmail").focus();
			return false;
		}
		if($("#txtEmail").val()!='')//validate email address
		{
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			var email_id=$("#txtEmail").val();
			if(reg.test(email_id) == false)
			{
				alert('Please enter valid email address');
				$("#txtEmail").focus();
				return false;
			}
		}
		
		if(document.getElementById("tdTxtState").style.display!='none')
		{
			if($("#txtState").val()=='')//check if state is empty!!
			{
				alert("Please enter State");
				$("#txtState").focus();
				return false;
			}
		}//end if

		if($("#txtCity").val()=='')//check city is empty!!
	 	{
			alert("Please enter city");
			$("#txtCity").focus();
			return false;
		}
		if($("#txtZip").val()=='')//check zipcode is empty!! 
	 	{
			alert("Please enter zip code");
			$("#txtZip").focus();
			return false;
		}
		if($("#txtZip").val()!='')//validate zip code
		{
			//var reg = /^\d{5}(-\d{4})?$/;
			var reg = /^[0-9]+$/;
			var zip=$("#txtZip").val();
			if(reg.test(zip) == false)
			{
				alert('Please enter valid zip code');
				$("#txtZip").focus();
				return false;
			}
		}
		if($("#taAddress").val()=='')//check address is empty!!
	 	{
			alert("Please enter address");
			$("#taAddress").focus();
			return false;
		}
		if($("#txtPhone").val()=='')//check phone is empty!!
	 	{
			alert("Please enter phone number");
			$("#txtPhone").focus();
			return false;
		}
		if($("#txtPhone").val()!='')//validate phone number
	 	{
	 		//var reg = /^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*[-\/\.]?\s*(\d{3})\s*[-\/\.]?\s*(\d{4})\s*(([xX]|[eE][xX][tT])\.?\s*(\d+))*$/;
			var reg = /^(\(?\+?[0-9]*\)?)?[0-9_\- \(\)]*$/;
			var phone_id=$("#txtPhone").val();
			if(reg.test(phone_id) == false)
			{
				alert('Please enter valid phone number');
				$("#txtPhone").focus();
				return false;
			}
		}
		if($("#txtEnterText").val()=='')//validate captcha image
	 	{
			alert("Please enter valid captha");
			$("#txtEnterText").focus();
			return false;
		}
		if($("#txtEnterText").val()!='')//validate captcha image
	 	{
	 		var captchaCode = $("#hdnCaptcha").val();
	 		var validCode = $("#txtEnterText").val();
	 		if(captchaCode != validCode){
	 			alert("Captcha code does not match with image");
				$("#txtEnterText").focus();
				return false;
	 		}
			
		}
		if(!document.getElementById('chkPatient').checked)//validate terms and conditions checkbox
		{
			alert('Please accept the terms and conditions');
			$("#chkPatient").focus();
		
			return false;	
			
			
		}
		
		if(($("#txtEmail").val()!=''))
		{
		
			$.post("inc/ajax.inc.php",{check_email:$("#txtEmail").val(),txtUsername:$("#txtUsername").val()},function(data)
			{
				
				if(data == "1") //condition to check if username and email matches
				
				{
					alert('Username and Email already available in contacts');
					$("#txtUsername").focus();
					return false;
				}
				if(data == "2") //condition to check email matches but not username

				{
					alert('Email already available in contacts');
					$("#txtEmail").focus();
					return false;
				}
				if(data == "3") //condition to check username matches but not the email

				{
					alert('Username already available in contacts');
					$("#txtUsername").focus();
					return false;
				}
				if(data == "4")	//condition to check none email and username matches

				{
					$("#frmPatientRegistration").submit();
				}
			});
		}


	});
});




