//list of things to check

function IsNumeric(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }

//load up the functions of the libafit site
function checkComplete()
{
	//check to see if they are complete, if you are checking obviously the for has not been submitted
	
	//var confirmed = confirm("You have not submitted your form to this clinic, are you sure that you want to cancel?");
	//if(!confirmed){
		//jQuery.ClientSubmissionForm.hideImage();
		//check for the required form parts
		if($("input[name='Name']").val() == ''){
			alert('Your Name is required');
			$("input[name='Name']").focus();
			return false;
		}else if($("input[name='Address']").val() == ''){
			alert('A valid address is required');
			$("input[name='Address']").focus();
			return false;	
		}else if($("input[name='City']").val() == ''){
			alert('A valid city is required');
			$("input[name='City']").focus();
			return false;	
		}else if($("#State").val() == ''){
			alert('A valid State is required');
			$("#State").focus();
			return false;	
		}else if($("input[name='Zip']").val().length != 5 || !IsNumeric($("input[name='Zip']").val())){
				alert('A valid zipcode is required ');
				$("input[name='Zip']").focus();
				return false;	
		}else if(!echeck($("input[name='Email']").val())){
			alert('The email address that you have entered is not valid.');
			$('#email').focus();
			return false;
		}else if(!checkInternationalPhone($("input[name='DayPhone']").val())){
			alert('A valid phone number is required');
			$("input[name='DayPhone']").focus();
			return false;
		}else{
			return true;
		}

};

function submitTheForm()
{
	if(jQuery.ClientSubmissionForm.checkComplete()){
		//actually submit the form
		//load the required info into the submission object for the page
		Submission.init($('#clinicID0').text(),$('#firstName').val(),$('#lastName').val(),$('#email').val(),$('#phone').val());
		//load any extra info into the submission object for the page
		if($('#address').val()!='')Submission.set('address',$('#address').val());
		if($('#city').val()!='')Submission.set('city',$('#city').val());
		if($('#state').val()!='')Submission.set('state',$('#state').val());
		if($('#zip').val()!='')Submission.set('zip',$('#zip').val());
		if($('#country').val()!='')Submission.set('country',$('#country').val());
		//load the images, even if they have not changed.
		Submission.set('frontImage',Submission.stripHttpRoot($("#formImage"+1).attr('src')));
		Submission.set('backImage',Submission.stripHttpRoot($("#formImage"+2).attr('src')));
		Submission.set('leftImage',Submission.stripHttpRoot($("#formImage"+3).attr('src')));
		Submission.set('rightImage',Submission.stripHttpRoot($("#formImage"+4).attr('src')));
		//Submission.debug();
		Submission.sendSubmit('submitContactRequest.php');
		//close the form if submitted successfully
		//jQuery.ClientSubmissionForm.hideImage();
	}else{
		
	}
}




function echeck(str) {

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   return false
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	    return false
	}
	 if (str.indexOf(at,(lat+1))!=-1){
	    return false
	 }
	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    return false
	 }
	 if (str.indexOf(dot,(lat+2))==-1){
	    return false
	 }
	 if (str.indexOf(" ")!=-1){
	    return false
	 }
		 return true;
}

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function formSubmit(){
	$("#paypal").submit();
}

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function checkInternationalPhone(strPhone){
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

$(document).ready(function(){
	$("#submitter").bind('click', function(e){
		return checkComplete();
	});
});