//-----------------------------------------------------------------------
// Glogal Arrays that I should use in this program
//-----------------------------------------------------------------------
var QuoteClassVals=new Array("Individual","Ind &#38 Spouse","Ind &#38 Child","Family");
//var QuoteClassVals=new Array("Individual","Ind &#38 Spouse");
var GenderVals = new Array("Male","Female","Child");
var AgeVals= new Array("18-24","25-29","30-34","35-39","40-44","45-49","50-54","55-59","60-64");
var CiVals=new Array("$10,000","$25,000","");
var ABVals=new Array("$1,000","$2,000","$3,000","$4,000","$5,000","$6,000","$7,000","$8,000","$9,000","$10,000","$11,000","$12,000","$13,000","$14,000","$15,000");
var TobaccoVals=new Array('No','Yes');
var isIE=vIE();
		//This is short name variables to make easy work with fields
		var $Q; //Quote class
		var $G0;//Gender
		var $G1;
		var $G2;
		var $A0;//Age
		var $A1;
		//var $A2;
		var $C0;//Critical illness
		var $C1;
		var $C2;
		var $B;//Benefit
		var $T0;//Tobacco
		var $T1;
		var $Prep;//prep for field
		var $Address;//Address
		var $Phone;//Phone


// ----------------------------------------------------------------------
// First Prepearing, adding some data after page loaded
// ----------------------------------------------------------------------
jQuery(document).ready(function(){
		//-----------------------------------------
		// Adding Values
		//-----------------------------------------
		FillVals();	

		//---------------------------------------------------
		// Setting a kind off validation right after the load
		//---------------------------------------------------
			//Some how this funciton dose not works in IE6 so I have to control
		if(7<=isIE||-1==isIE) {
			//console.log("First");
			QuoteValidate();
			}

		//Showing ajax working to user
		$('#Timer').ajaxStart(function(){Show();}).ajaxStop(function(){tb_remove();});

		//Advise for IE6 users
		if(-1<isIE&&6>=isIE) {
			$('.QFlog').append('<br/><p style="color: #777777;">Developer advise: You are using Intenet Explorer 6 (or older version) it is very old brouser better to install newer verson.</p><br/>');
		}


		//Adding Sort names to make this program be easy editible 
		 $Q=$('#SelectQuoteClass'); //Quote class
		 $G0=$('#SelectGender0');//Gender
		 $G1=$('#SelectGender1');
		 $G2=$('#SelectGender2');
		 $A0=$('#SelectAge0');//Age
		 $A1=$('#SelectAge1');
		 //$A2=$('#SelectAge2');
		 $C0=$('#SelectCi0');//Critical illness
		 $C1=$('#SelectCi1');//Critical illness
		 $C2=$('#SelectCi2');
		 $B=$('#SelectAccidentBenefit');//Benefit
		 $T0=$('#SelectTobacco0');//Tobacco
		 $T1=$('#SelectTobacco1');
		$Prep=$('#InputPreparedFor');
		$Address=$('#InputAddress');
		$Phone=$('#InputPhone');

		 //Select no Critical Illness for first
		 $C0.val(2);
		 $C1.val(2);
		 $C2.val(2);
		 //Select woman in secton gender
		 $G1.val(1);

		 
		 //Delete childe from old man, and old man from child
		 $('#SelectGender0 option[value=2]').remove();
		 $('#SelectGender1 option[value=2]').remove();
		 $('#SelectGender2 option[value=0]').remove();
		 $('#SelectGender2 option[value=1]').remove();

		
		//------------------------------------------
		// Binding Events
		//-----------------------------------------
		jQuery('#SelectQuoteClass').change( function(){
				//console.info(jQuery('#SelectQuoteClass').val());
				//Some how this funciton dose not works in IE6 so I have to control
				if(7<=isIE||-1==isIE) {
					//console.log('here to');
					QuoteValidate();
				}
				});
		//Antipidor protection!
		$G0.change( function(){
			if(0==$G0.val()) $G1.val(1);	
			else $G1.val(0);
				});
		$G1.change( function(){
			if(0==$G1.val()) $G0.val(1);	
			else $G0.val(0);
				});



		});
//=======================================================================

//================================================================================
// This function helps me to detect IE version
// cuz somehow this script dose not work at IE6, I nedd to detect this browser
// toked from http://www.thefutureoftheweb.com/blog/detect-ie6-in-javascript
//================================================================================
function vIE(){
	return (navigator.appName=='Microsoft Internet Explorer')?parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]):-1;
}
//-------------------------------------------------------------------------------

//-----------------------------------------------------------------------
// Function for setting Selects with optons
// Arguments: id - selext id, arr - array with options meanings
// Return: nothing
// -----------------------------------------------------------------------
function SetSelect(id,arr){
	var Sel=jQuery(id);
	jQuery.each(arr,function(i,val){
			Sel.append("<option value=\""+i+"\">"+val+"</option>");
			});
}
// =======================================================================

//------------------------------------------------------------------------
//  This function looks that form whas changing depend on QuoteClass state
//------------------------------------------------------------------------
function QuoteValidate(){
	var state=jQuery('#SelectQuoteClass').val();
	//First Step clear all, prerearing to select
	jQuery('#SelectGender1').attr('disabled','yes');
	jQuery('#SelectGender2').attr('disabled','yes');
	jQuery('#SelectAge1').attr('disabled','yes');
	//jQuery('#SelectAge2').attr('disabled','yes');
	jQuery('#SelectCi1').attr('disabled','yes');
	jQuery('#SelectCi2').attr('disabled','yes');
	jQuery('#SelectTobacco1').attr('disabled','yes');
	//Spose
	if(1==state){
		jQuery('#SelectGender1').removeAttr('disabled');
		jQuery('#SelectAge1').removeAttr('disabled');
		jQuery('#SelectCi1').removeAttr('disabled');
		jQuery('#SelectTobacco1').removeAttr('disabled');
		return;
	}
	//Child
	if(2==state){
		jQuery('#SelectGender2').removeAttr('disabled');
		//jQuery('#SelectAge2').removeAttr('disabled');
		jQuery('#SelectCi2').removeAttr('disabled');
		return;
	}
	//family
	if(3==state){
		jQuery('#SelectGender1').removeAttr('disabled');
		jQuery('#SelectAge1').removeAttr('disabled');
		jQuery('#SelectCi1').removeAttr('disabled');
		jQuery('#SelectTobacco1').removeAttr('disabled');
		jQuery('#SelectGender2').removeAttr('disabled');
		//jQuery('#SelectAge2').removeAttr('disabled');
		jQuery('#SelectCi2').removeAttr('disabled');
		return;
	}
}
//========================================================================


//--------------------------------------------------
// Function Filling Selectbox with Valls
// ------------------------------------------------
function FillVals(){
		//-----------------------------------------
		// Adding Values
		//-----------------------------------------
		//Adding data to Quote Class
		SetSelect('#SelectQuoteClass',QuoteClassVals);
		//Adding data to Gender;
		SetSelect("#SelectGender0",GenderVals);
		SetSelect("#SelectGender1",GenderVals);
		SetSelect("#SelectGender2",GenderVals);
		//Adding data to Age
		SetSelect("#SelectAge0",AgeVals);
		SetSelect("#SelectAge1",AgeVals);
		//SetSelect("#SelectAge2",AgeVals);
		//Adding data to Critical Illness
		SetSelect("#SelectCi0",CiVals);
		SetSelect("#SelectCi1",CiVals);
		SetSelect("#SelectCi2",CiVals);
		//Addin data to Accident Benefit
		SetSelect("#SelectAccidentBenefit",ABVals);
		//Adding data to Tobacco
		SetSelect("#SelectTobacco0",TobaccoVals);
		SetSelect("#SelectTobacco1",TobaccoVals);
}
//========================================================

//---------------------------------------------------------
// This function I use for look what happend in parsing file
// --------------------------------------------------------
function parse(){
	$.post('parse/parse.php',function(data){
			console.info(data);
			});
}
// =========================================================

//-----------------------------------------------------------
// This function call Ajax to get values from parsing data
// -----------------------------------------------------------
function QuoteFormCounter(type){
	// Only two types of arguments
	// calc == when only calculate
	// req ==when calculate and send e mail
	if(!('calc'==type||'req'==type)){
		return	false;
	}
	if(!$Prep.val()&&'req'==type){
		alert('Please fill the "Prepare For" field');
		return false;
	}
	var request={'Q':$Q.val(),
		'G[0]':$G0.val(),'G[1]':$G1.val(),'G[2]':$G2.val(),
		'A[0]':$A0.val(),'A[1]':$A1.val(),
		'C[0]':$C0.val(),'C[1]':$C1.val(),'C[2]':$C2.val(),
		'B':$B.val(),
		'T[0]':$T0.val(),'T[1]':$T1.val(),
		Prep:$Prep.val(),Address:$Address.val(),Phone:$Phone.val(),
		'ToDo':type};
	$.post('quoteform_/quoteformhandler.php',request,function(data){
			$(".QFAns").html(data);
			//console.info(data);
			});
}
//==========================================================

//-----------------------------------------------------------
// I use this function for shoing modal windows
//-----------------------------------------------------------
function Show(){
		  tb_show('Wait', '#TB_inline?height=300&width=400&inlineId=Timer&modal=true', null);
		  jQuery("#TB_window").css({ border: "0", background:"transparent" });
}
//=============================================================

function test(){
	console.info($B.val(),"hoy");
	console.info($T0.val());
	console.info($T1.val());
	console.info($C2.val());
}

