$(document).ready(function()
{
	function validate(i)
	{
		var errors = '';
		var msg = checkEmailAjx($("#txtEmail").val());
		if (msg != '')
		{
			errors += "<br>- " + msg;
			$("#txtEmail").focus();
		}
		if(i == 1)
		{
			if ($("#txtPassword").val()=='')
			{
				errors += "<br>- Please enter 'Password'";
				$("#txtPassword").focus();
			}
			if ($("#txtRePassword").val()=='')
			{
				errors += "<br>- Please enter 'Confirm Password'";
				$("#txtRePassword").focus();
			}
			if ($("#txtPassword").val() != $("#txtRePassword").val())
			{
				errors += "<br>- 'Password and Conform Password do not match'";
				$("#txtPassword").focus();
			}
			if ($("#txtFNameB").val()=='')
			{
				errors += "<br>- Please enter 'First Name' of Billing Information";
				$("#txtFNameB").focus();
			}
			if ($("#txtLNameB").val()=='')
			{
				errors += "<br>- Please enter 'Last Name' of Billing Information";
				$("#txtLNameB").focus();
			}
			if ($("#txtPhone").val()=='')
			{
				errors += "<br>- Please enter 'Phone Number' of Billing Information";
				$("#txtPhone").focus();
			}
			if ($("#txtMobile").val()=='')
			{
				errors += "<br>- Please enter 'Mobile Number' of Billing Information";
				$("#txtMobile").focus();
			}
			if ($("#txtAddress1B").val()=='')
			{
				errors += "<br>- Please enter 'Address of Billing Information'";
				$("#txtAddress1B").focus();
			}
			if ($("#txtCityB").val()=='')
			{
				errors += "<br>- Please enter 'City' of Billing Information";
				$("#txtCityB").focus();
			}
			if ($("#countryB").val()==0)
			{
				errors += "<br>- Please select 'Country' of Billing Information";
				$("#countryB").focus();
			}
			
			if ($("#txtCCType").val()=='')
			{
				errors += "<br>- Please select 'Card Type'";
				$("#txtCCType").focus();
			}
			
			if ($("#txtCCNumber").val()=='')
			{
				errors += "<br>- Please enter 'Card Number'";
				$("#txtCCNumber").focus();
			}
			var cardtypeinfo=document.getElementById('txtCCType').value;
			var ccNumber=document.getElementById('txtCCNumber').value;
			if (!checkCreditCard (ccNumber,cardtypeinfo))
			{
				errors += ccErrors[ccErrorNo];
				$("#txtCCNumber").focus();
			}
			
			if ($("#txtCCcode").val()=='')
			{
				errors += "<br>- Please enter 'Security Code'";
				$("#txtCCcode").focus();
			}
			if ($("#CCMonth").val()=='')
			{
				errors += "<br>- Please select 'Expiration Month'";
				$("#CCMonth").focus();
			}
			if ($("#CCYear").val()=='')
			{
				errors += "<br>- Please select 'Expiration Year'";
				$("#CCYear").focus();
			}

			if ($("#txtCCOwner").val()=='')
			{
				errors += "<br>- Please enter 'Card Owner'";
				$("#txtCCOwner").focus();
			}
			if ($("#txtBankName").val()=='')
			{
				errors += "<br>- Please enter 'Bank Name'";
				$("#txtBankName").focus();
			}
			if ($("#txtBankPhone").val()=='')
			{
				errors += "<br>- Please enter 'Bank Phone'";
				$("#txtBankPhone").focus();
			}


			if (!$("#ShipToSame").checked)
			{
				if ($("#txtFNameS").val()=='')
				{
					errors += "<br>- Please enter 'First Name' of Shipping Information";
					$("#txtFNameS").focus();
				}
				if ($("#txtLNameS").val()=='')
				{
					errors += "<br>- Please enter 'Last Name' of Shipping Information";
					$("#txtLNameS").focus();
				}
				if ($("#txtAddress1S").val()=='')
				{
					errors += "<br>- Please enter 'Address of Shipping Information'";
					$("#txtAddress1S").focus();
				}
				if ($("#txtCityS").val()=='')
				{
					errors += "<br>- Please enter 'City' of Shipping Information";
					$("#txtCityS").focus();
				}
				if ($("#countryS").val()==0)
				{
					errors += "<br>- Please select 'Country' of Shipping Information";
					$("#countryS").focus();
				}
				
			}			
		}
		if(errors == '' && i == 1)
		{
			if (!document.getElementById('chkTerms').checked)
			{
				alert("Please confirm that you have read and understood our 'Terms and Conditions'!");
				return false;
			}
		}
		if (errors=='')
			return true;
		else
		{
			$("#errors_div").html("<b>Errors:</b><br />"+errors);
			$("#errors_div").fadeIn("slow");
			return false;
		}
	}
	
	$("#btnSave").click(function(){
		if(validate(1))
		{			
			$('#statusLoader').ajaxStart(function() {
				$(this).show();
	
			}).ajaxStop(function() {
				$(this).hide();
			});
			
			var chkSubcribe=0;
			if(document.getElementById('chkSubcribe').checked)
				chkSubcribe=1;
			var findus=$('#findus').val();
			if(document.getElementById('txtdescription'))
				findus= findus+" | " + $('#txtdescription').val();
			
			countryBTxt = document.getElementById('countryB').options[document.getElementById('countryB').selectedIndex].text;
			countrySTxt = document.getElementById('countryS').options[document.getElementById('countryS').selectedIndex].text;			
			$.post("include/ajax/modules/ajxsignup.php", 
			   {
				   email:$('#txtEmail').val(), 
				   purpose:'signup', 
				   password:$('#txtPassword').val(), 
				   fNameB:$('#txtFNameB').val(), 
				   lNameB:$('#txtLNameB').val(),
				   phone:$('#txtPhone').val(),
				   mobile:$('#txtMobile').val(),
				   companyB:$('#txtCompanyB').val(),
				   address1B:$('#txtAddress1B').val(),
				   address2B:$('#txtAddress2B').val(),
				   cityB:$('#txtCityB').val(),
				   stateB:$('#txtStateB').val(),
				   zipB:$('#txtZipB').val(),
				   countryBTxt:countryBTxt,
				   countryB:$('#countryB').val().split('|')[0],
				   abvCountryB:$('#countryB').val().split('|')[1],
				   fNameS:$('#txtFNameS').val(), 
				   lNameS:$('#txtLNameS').val(),				   
				   companyS:$('#txtCompanyS').val(),
				   address1S:$('#txtAddress1S').val(),
				   address2S:$('#txtAddress2S').val(),
				   cityS:$('#txtCityS').val(),
				   stateS:$('#txtStateS').val(),
				   zipS:$('#txtZipS').val(),
				   countrySTxt:countrySTxt,
				   countryS:$('#countryS').val().split('|')[0],
				   abvCountryS:$('#countryS').val().split('|')[1],
				   ccType:$('#txtCCType').val(),
				   ccNumber:$('#txtCCNumber').val(),
				   ccMonth:$('#CCMonth').val(),
				   ccYear:$('#CCYear').val(),
				   ccOwner:$('#txtCCOwner').val(),
				   ccBankName:$('#txtBankName').val(),
				   ccBankPhone:$('#txtBankPhone').val(),
				   securityCode:$('#txtCCcode').val(),
				   chkSubcribe:chkSubcribe,
				   findus:findus,
				   
				},
				function(response){
					if(response)
					{	
						
						 arrResponse = response.split('~');
						  if(arrResponse[1] == 1)
						  {
							
							var path="success.html?CustomerId="+arrResponse[2]+"&CustomerName="+arrResponse[3];
							window.location = path;
							
						  }
						$('#txtBankPhone').focus();
						$('#btnSave').disabled = false;
						$("#message_div").html(arrResponse[0]);
						$("#message_div").fadeIn("slow");
						if(document.getElementById('frmLaunchACS'))
						{
							document.getElementById('frmLaunchACS').submit();
						}
						
					}
				});	
		}
	});


	$("#btnCheck").click(function(){
		if(validate(0))
		{			
			$('#statusLoader').ajaxStart(function() {
				$(this).show();
	
			}).ajaxStop(function() {
				$(this).hide();
			});
			$.post("include/ajax/modules/ajxsignup.php", 
			   {
				   email:$('#txtEmail').val(),
				   purpose:'chkAvlblty'
			   },
				function(response){
					$('#btnCheck').disabled = true;
					if(response)
					{
						$('#txtEmail').focus();
						$('#btnCheck').disabled = false;
						$("#errors_div").html(response);
						$("#errors_div").fadeIn("slow");
					}
				});	
		}
	});


});
function setShipToSame()
{	
	if (document.getElementById('ShipToSame').checked)
	{
		document.getElementById('txtFNameS').value = document.getElementById('txtFNameB').value;
		document.getElementById('txtLNameS').value = document.getElementById('txtLNameB').value;		
		document.getElementById('txtCompanyS').value = document.getElementById('txtCompanyB').value;
		document.getElementById('txtAddress1S').value = document.getElementById('txtAddress1B').value;
		document.getElementById('txtAddress2S').value = document.getElementById('txtAddress2B').value;
		document.getElementById('txtCityS').value = document.getElementById('txtCityB').value;
		document.getElementById('txtStateS').value = document.getElementById('txtStateB').value;
		document.getElementById('txtZipS').value = document.getElementById('txtZipB').value;
		document.getElementById('countryS').value = document.getElementById('countryB').value;
	}
}
function uncheckSameB()
{
	document.getElementById('ShipToSame').checked = false;
}
