$(document).ready(function(){

	$(".btn-joinnow-small").click(function(event){
		event.preventDefault();
		$('[id$="Error"]').removeClass('error');

		var postData = 'mobilenumber='+ $('#mobilenumber').val();
		var postData = postData + '&provider='+$('#provider option:selected').val();
		var postData = postData + '&emailaddress='+$('#emailaddress').val();
		var postData = postData + '&zipcode='+(('undefined' == $('#zipcode').val())?'':$('#zipcode').val());
		var postData = postData + '&termsagreement='+((false == $('#termsagreement').attr('checked'))?'':'true');

		$.post(
			'http://www.mobilemob.com/actions/index.php/inputactions',
			postData,
			function(data){
				if('undefined' != typeof data.errors){
					for(fieldName in data.errors){
						$('#'+fieldName+'Error').addClass('error');
						$('[id$=duplicateError]').hide();
						if(fieldName == 'duplicate'){
							$('#'+data.errors[fieldName]+fieldName+'Error').addClass('error');
							$('#'+data.errors[fieldName]+fieldName+'Error').fadeIn();
						}
					}
				} else if ( 'undefined' != typeof data.success){
					$('#main-signup-form').fadeOut("slow", function(){
						$('#main-signup-success').fadeIn();
					});
				}
			},
			'json'
		);
	
	})
	;

	$('input[data-value]').each(function(){
		var elem = $(this);
		elem.defaultText( elem.attr('data-value') );
	});
  

	//dynamically add the dynatip div
	$("body").append('<div id="dynatip"></div>');
  
	// initialize tooltip
	$(".help[title]")
	/*
	.tooltip({
	
		// use single tooltip element for all tips
		tip: '#dynatip', 
		
		// tweak the position
		offset: [0, 0],
		
		// use "slide" effect
		effect: 'slide'
		
	// add dynamic plugin 
	})
	.dynamic( {
	
		// customized configuration on bottom edge
		bottom: {
		
			// slide downwards
			direction: 'down',
			
			// bounce back when closed
			bounce: true
		}
	});  
	*/
	
	
	//check for errors
  	checkErrors();
	
});


	
// ***************** functions ******************************
function checkErrors(directError) 
{
	var errorFlag;
	var msgHeader = '';
	var msgBody = '';
	
	if (directError)
	{
		errorFlag = directError;
	}
	else
	{
		//errorFlag = jQuery.url.param('error'); 	
		//errorFlag = $.url.param('error'); 	
	}

	if (errorFlag) {
		//evaluate errorFlag to provide the proper error message;
		switch (errorFlag)
		{
			case 'missingFields':
				msgHeader = header_missingFields;
				msgBody = msg_missingFields;
				break;				
			case 'invalidInput':
				msgHeader = header_inValidInput;
				msgBody = msg_invalidInput;
				break;
			case 'badUser':
				msgHeader = header_badUser;
				msgBody = msg_badUser;
				break;		
			case 'duplicateUser':
				msgHeader = header_duplicateUser;
				msgBody = msg_duplicateUser;
				break;					
			case 'recaptchaFailed':
				msgHeader = header_recaptchaFailed;
				msgBody = msg_recaptchaFailed;
				break;	
			case 'genericFail':
				msgHeader = header_genericFail;
				msgBody = msg_genericFail;
				break;	
			case 'retryPreferences':
				msgHeader = header_retryPreferences;
				msgBody = msg_retryPreferences;
				break;	
			case 'retryDevice':
				msgHeader = header_retryDevice;
				msgBody = msg_retryDevice;
				break;	
			case 'deviceValidation':
				msgHeader = header_deviceValidation;
				msgBody = msg_deviceValidation;
				break;	
			case 'preferenceValidation':
				msgHeader = header_preferenceValidation;
				msgBody = msg_preferenceValidation;
				break;		
			case 'noDeviceInterest':
				msgHeader = header_noDeviceInterest;
				msgBody = msg_noDeviceInterest;
				break;	
			case 'noPreferenceChecked':
				msgHeader = header_noPreferenceChecked;
				msgBody = msg_noPreferenceChecked;
				break;						
		}
		
		//show error message box
		displayErrorMessage(msgHeader,msgBody);
	}
}


function displayErrorMessage(header, message)
{
	var errorBox = '';
	
	//first hide the info message box and remove any error boxes!
	$('div.message-info').css('display','none');
	$('div.message-error').remove();
	
	//build the message
	errorBox = '<div class="message-error"><div class="content"><h2>';
	errorBox += header;
	errorBox += '</h2><p>';
	errorBox += message;
	errorBox += '</p></div></div>';
	
	//attach message to entire content
	$("div.entry-content").prepend(errorBox);

}

function setfocus(a_field_id) {
	document.getElementById(a_field_id).focus();
}

function printTestimonials() 
{
	//this will print specified number of testimonials
	var limit = testimonials.length;
	var randomnumberA = Math.floor(Math.random()*limit);
	
	document.write(testimonials[randomnumberA]);				

}

function printallTestimonials()
{
	//prints all testimonials available
	var limit = testimonials.length;
	
	for (x=0; x < limit; x++)
	{
		document.write(testimonials[x]);
		document.write('<div class="hr_dotted"></div><br>');
	}
}

function isQuiteConfirm()
{
 var where_to= confirm("Are you sure you want to cancel joining the mob?!");
 if (where_to== true)
 {
   window.location="http://127.0.0.1/mobileMob/your-submission-is-canceled/";
 }
 else
 {
  //window.location="#";
  }
}


// validation interceptions
function isEmpty(target)
{
	if (target.value == '') {return 1;}
	else {return 0;}
}


function validationInterceptPage1(form)
{
	var isthisEmpty = 0;
	
	isthisEmpty += isEmpty(form['emailaddress']);
	isthisEmpty += isEmpty(form['password']);
	isthisEmpty += isEmpty(form['password_confirm']);
	isthisEmpty += isEmpty(form['recaptcha_response_field']);
	
	if (isthisEmpty == 0) {signupPart1Javascript( form );} //if all required fields move onto AJAX validation
	else {checkErrors('missingFields');}
}


