//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($('#firstName').val() == ''){
		alert('Your First Name is required');
		$('#firstName').focus();
		return false;
	}else if($('#lastName').val() == ''){
		alert('Your Last Name is required');
		$('#lastName').focus();
		return false;
	}else if($('#username').val() == ''){
		alert('A valid username is required');
		$('#lastName').focus();
		return false;
	}else if($('#password').val() == '' || $('#password').val().length < 6){
		alert('A valid password of at least 6 characters is required');
		$('#lastName').focus();
		return false;
	}
	else if(!echeck($('#email').val())){
		alert('The email address that you have entered is not valid.');
		$('#email').focus();
		return false;
	}else if($('#email').val() != $('#confirmEmail').val()){
		alert('Make certain that the email addresses you have entered match');
		$('#email').focus();
		return false;
	}else if(!checkInternationalPhone($('#phone').val())){
		alert('A valid phone number is required');
		$('#phone').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;
}

// 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);
}

//a submission object that is passed to the request
Submission = {
	options : {
		clinicId	: '',
		firstName	: '',
		lastName	: '',
		email		: '',
		phone		: '',
		address		: '',
		city		: '',
		state		: '',
		zip			: '',
		country		: '',
		frontImage		: '',
		backImage		: '',
		leftImage		: '',
		rightImage		: ''
	},
	init : function(clinicId,firstName,lastName,email,phone){
		this.options.clinicId = clinicId;
		this.options.firstName = firstName;
		this.options.lastName = lastName;
		this.options.email = email;
		this.options.phone = phone;
	},
	set : function(name,value){
		this.options[name] = value;
	},
	debug : function(){
		$('#debug').html(this.options.toSource());
	},
	stripHttpRoot : function(url){
		var pos = url.indexOf('uploadedfiles');
		if(pos > 0) return url.substring(pos+14,url.length);
		return url;
	},
	getSubmission : function(){
		return this.options;
	},
	sendSubmit : function(url){
		var ul_vars = {
		  interval : 1000, //The time in milleseconds between each status request.
		  speeds : []      //Keeps track of the speeds of each upload.
		};
		setTimeout(function(){
			submitter = new RepeatGetAction(url, Submission.options, ul_vars.interval);
			submitter.go();
			submitter.successFunc = function (getBack) {
				//alert the customer that the form has been submitted.
				alert('An Egoscue Therapist will contact you by email or phone soon');
				setTimeout(function(){jQuery.ClientSubmissionForm.hideImage();},1000);
			}
			submitter.failFunc = function (getBack) {
		    	alert(getBack.error_msg);
		  	}
			//this is what we do at the end
		;},100)
	}
	
}



$(document).ready(function(){
	//
	if($('#submitButton').size()){
		$('#loginText').remove();
	}else{
		$('.login').show();
	}

	$('#loginText').bind('click',function(e){
		$('#loginText').hide();
		$('#theLogin').show();
	});
	$('#closeIMG').bind('click',function(e){
		$('#loginText').show();
		$('#theLogin').hide();
	});
	$('#submitButton').bind('click',function(e){
		if(checkComplete()){
			$('#newuser').submit();
		}
	});
	if($('#moveLoginToHere').is("div") && $('#registerLink').is("a")){
		$('#moveLoginToHere').attr('align','center');
		$('#moveLoginToHere').append($('#loginForm'));
		$('#moveRegisterToHere').append($('#registerLink').append(document.createTextNode(' at www.libafit.com.')));
		var link = 
		$('#registerLink').attr('href',$('#registerLink').attr('href')+'?loginRequired=purchase.php&loginRequiredName=Login%20and%20Purchase%20Services');
		$('#closeIMG').remove();
		$('#loginText').hide();
		$('#theLogin').show();
	}
	$('#gallery a').lightBox();
});
