$j = jQuery.noConflict();

$j(window).load(function() {
	$j('#popUp').length ? bundlePopUp() : null;
	
	$j('#bundle').length || $j('#summaryContainer').length ? bundleCalculation($j('.group').length) : null;
	$j('#extraNumbers').length ? checkFields() : null;
	
}); 
function checkFields() {

	var promptMessage = "Please enter a valid mobile number";
	
	//if an input field gets the focus then select the default radio button
	$j('.extraNumbers').focus(function() {
	
		//check the value of the input
		
		if($j(this).attr('value')==promptMessage) {
			$j(this).attr('value','');
		} else {
		
			$j(this).parent('div').next('div').children('input[type="radio"]').attr('checked',true);
		}
	});
	//if the radio button is checked give focus to the input and display prompt text
	$j('#extraNumbers input[type="radio"]').click(function(){
	
		
		var checkifEmpty =$j(this).parent().parent('div').siblings('.in-txt').children('input[type="text"]').attr('value')

		checkifEmpty=="" || checkifEmpty==undefined ? $j(this).parent().parent('div').siblings('.in-txt').children('input[type="text"]').attr('value',promptMessage) : null;
		
	});
}
function bundlePopUp() {
	$j('#popHover').mouseover(function() {$j('#popUp').show();});
	$j('#popHover').mouseout(function() {$j('#popUp').hide();});
}



function bundleCalculation(noOfGroups) {
	
	//set the start value to be the value of the hidden input
	var	startValue = parseFloat($j('.hiddenInput').attr('value'));
	
	//create total variable and set it to be the same as the startValue
	var total = startValue;
	
	//if the inputs gets focus and are populated with default values then clear the fields
	$j('#bundle input.text').focus(function() { $j(this).attr('value').indexOf("Phone") == 0 || $j(this).attr('value')=="Enter your home phone number" || $j(this).attr('value')=="Enter your home phone number" || $j(this).attr('value')=="Enter your mobile number" ? $j(this).attr('value','') : null;});
	
	//hide the print page
	$j('#printPage').hide();
	
	//show the submit buttons (hidden as default for users without javascript turned on)
	$j('.submitChanges').show();

	//check to see if user is on page 1
	var isFirst;
	$j('.default').length ? isFirst=true : isFirst=false; 
		
   //reset the form to its defualt settings
   resetForm();
   
   
	/////////////////////////////////
	 ////////////////////////////////HANDLE WHAT HAPPENS WHEN RADIO BUTTONS ARE CLICKED
	 ////////////////////////////////
	$j('input[type="radio"]').click(function(){
		
		//if the radio button is checked
	    if ($j(this).is(':checked')) {
		    var theParent = $j(this).parent().parent().parent().parent();
			var thisValue;
			var thisGroup;
			//check to see if the radio button clicked is from the extraNumbers category, if so set the value to be 5.00 (as this values cannot be set on these radio buttons for .NET reasons)
			if($j(theParent).attr('id')=='extraNumbers') {
				thisValue = parseFloat('5.00');
				thisGroup = parseInt($j(this).parent().attr('class'));
			} else {
				thisValue = parseFloat($j(this).attr('value'));
				thisGroup = parseInt($j(this).attr('class'));
			}

		    theGroup[thisGroup]!==parseFloat($j(this).attr('value')) ? theGroup[thisGroup]=thisValue : null;//check to make sure the same radio button hasnt been pressed
		    
		    //if user is on page set the total to be zero, otherwise set it to the start value
		    isFirst ? total = 0 : total = startValue;
		    
		   //calculate the total by adding all the values from the array
		   for(i = 0; i < theGroup.length; i++){
			  total+=theGroup[i];
		   }
		   $j('.price span').html('&pound;'+total.toFixed(2)+'');//display the price in the correct areas on the page, note the .toFixed rounds the decimal point
    	} 
    		//add the total value to the hidden input field
    		$j('.hiddenInput').attr('value',total.toFixed(2));
    		
	 });
	 /////////////////////////////////
	 ////////////////////////////////HANDLE WHAT HAPPENS WHEN RESET BUTTON IS CLICKED
	 ////////////////////////////////
	 //FOR PAGE 1
	 $j('#resetForm').click (function() {
		 resetForm(false);
	 });
	 //FOR PAGE 2
	 $j('#resetForm2').click (function() {
		 resetForm(true);
	 });	 
	 
	 if($j('#extraNumbers').length) {
		var hiddenValueTemp = parseFloat($j('.hiddenInputTemp').attr('value'))
		$j('.hiddenInput').attr('value',hiddenValueTemp.toFixed(2))
	 }
	  //////////////////////////////////////////////
	 ////////////////////////////////INITIALISE THE FORM
	 //////////////////////////////////////////////
	 function resetForm(page) {
		//create new array to store the values of the page components
		 theGroup=new Array(noOfGroups);
		 
		 //reset the values from this array to be zero
		 for(i = 0; i < theGroup.length; i++){
		 
			theGroup[i]=0;
		 }
		 //if it is page 1, hard code the default values
		 if($j('.default').length) {
			 theGroup[0]=0;theGroup[1]=16.70;theGroup[2]=8.85;startValue = 25.55;
		 }
		 
		 //Set the default input fields to be checked (only applies to page 1)
		 $j('input.default').attr('checked',true);
		 
		 //set the total cost to be equal to the startValue
		 total=parseFloat(startValue);
		 //put this value into the visible price text and hidden input
		 $j('.price span').html('&pound;'+total.toFixed(2));
		 $j('.hiddenInput').attr('value',total.toFixed(2));
		 
		//reset the inputs for page 1, store the selectors
			var $input1 = $j('.bundle1 input[type="text"]:eq(0)'), $input2 = $j('.bundle1 input[type="text"]:eq(1)'), $input3 = $j('.bundle1 input[type="text"]:eq(2)');
			//reset the string values
			$j($input3).attr('value')!=='Enter your home phone number' ? $j($input3).attr('value','Enter your home phone number') : null;
			$j($input1).attr('value')!=='Enter your home phone number' ? $j($input1).attr('value','Enter your home phone number') : null;
			$j($input2).attr('value')!=='Enter your mobile number' ? $j($input2).attr('value','Enter your mobile number') : null;
			
		 	
			//deselect all the radio buttons
			$j('input[type="radio"]').attr('checked',false);
			
			//if page 2 then clear the text inputs
			$j('#extraNumbers input[type="text"]').attr('value','');
			
			
			//Set the default input fields to be checked
			$j('input.default').attr('checked',true);
			$j('input.def').attr('checked',true);
		 
    	
	 }
}
