﻿	
var onStateSelect = function(nStateID) 
{
    // called either by flash or the ddl
    Home.SetStateID(nStateID.toString());
}

var Home = {

	Err: null,
	Init: function() {
		
		//replace select with flash map
		Home.InsertMap("flashContainer");
		
		//init errir message container
		Home.InitErr();
		
		$('a[id$=\'_linkButtonGetQuote\']').click(function(event){
			try {
				Home.Validate(event);
			}
			catch(err) {
				alert('Error validating form: ' + err);
				event.preventDefault();
			}
		});
	},
	
	SetStateID: function(stateID) {
	    $('input[type=hidden][id$=\'_hiddenStateID\']').val(stateID);
	},
	
	InitErr: function(){
		//fade to fully opaque and show (will still be hidden)
		Home.Err = $('#errMessage'); //add message container and set it as Home.Err
		Home.Err.fadeTo(1, 0);
		Home.Err.show();
	},
	
	Validate: function(event){
	
        var isNumber = function (val) 
        {
            return /^-?((\d+\.?\d?)|(\.\d+))$/.test(val);
        }
        
		// get the age
		var strAge = $('input[type=text][id$=\'_textBoxAge\']').val();
		
		// get the cover type
		var strCoverType = $('input[name$=SortOfCover]:radio:checked').val(); // will be undefined if nothing is selected
		
		//reset errors
		Home.Err.fadeTo(1, 0);
		var err = '';
		
		//check age
		if(!isNumber(strAge))
		{
			//alert("You must enter an age");
			err += (strAge == '' || strAge == '?') 
								? 'You must enter an age<br />'
								: 'You must enter a valid age<br />';
		}
		//check cover type
		if(!strCoverType)
		{
			err += 'Please select a cover type<br />';
		}
		//errors? cancel the sh** ..
		if (err.length > 0)
		{
			Home.Err.html(err);
			Home.Err.fadeTo(500, 1);
			event.preventDefault();
		}
	},	
	
	InsertMap: function(objId) {
	
		// change these to point to the flash map file
		var swfPath = "/swf/"
		var swfName = "ausmap.swf";
		
		// map these value to match the id assigned to each state
		// on the HTML form interface.  The id you enter here will 
		// allow you to set which state is active on startup, and the 
		// is parameter dispatched by flash to JS when a new state is selected
		// NOTE that the value of thes properies match the form option values in the non-flash container.  
		// this is SUPER important if you want the two elements to operate interchangeably
		var state_vic = 'V';
		var state_sa =  'S';
		var state_wa =  'W';
		var state_nt =  'X';
		var state_qld = 'Q';
		var state_nsw = 'N';
		var state_act = 'A';
		var state_tas = 'T';
		
		// set the value to match of of the property names above
		var default_state = $('input[type=hidden][id$=\'_hiddenStateID\']').val();
		
		// stateSelectionHandler:
		// name of the JS method called from the falsh app when a state is selected by the user
		var stateSelectionHandler = "onStateSelect";
		
		// --------------------------------------
		//  No touchy-touchy
		// --------------------------------------
		var width = "190";
		var height = "180";
		var flashVersion = "9.0.0";		
		var flashvars = {state_vic:state_vic,state_sa:state_sa,state_wa:state_wa,state_nt:state_nt,
						  state_qld:state_qld,state_nsw:state_nsw,state_act:state_act,
						 state_tas:state_tas,default_state:default_state,stateSelectionHandler:stateSelectionHandler};
		var params = {};
		var attributes = {id: "mapFlash"}

		swfobject.embedSWF(swfPath + swfName, objId, width,height,
							flashVersion,swfPath + "expressInstall.swf",
							flashvars, params, attributes);
	}
}

var HomeBanner = {

	Err: null,
	Init: function() {

        // check for referral information.  The config will determine whether a flash file is specified for the 
        // offer and it will overwrite the default flash file to use
        
		var referralInfo = window.REFERRALINFO;
		var flashFile = "homepage_frankview.swf";
		
		if (referralInfo != null)
		{
			for(var i = 0; i < referralInfo.length; i++)
			{
				var flsh = referralInfo[i].flsh;
			    
				if(flsh != "")
				{
					flashFile = flsh;
				}
			}
		}
		
		var params = {};
		var attributes = {id: "homeBannerFlash"}
		var flashVersion = "9.0.0";		
		var homepagebannervars = {frankview:"/frankview.aspx"};
		
		var flashPath = "/swf/" + flashFile;
        //homepage banner - frank view					
		swfobject.embedSWF(flashPath, "flashBannerContainer", "310","150",
							flashVersion,"/swf/expressInstall.swf",
							homepagebannervars, params, attributes);
    }
}


$(document).ready(Home.Init);
$(document).ready(HomeBanner.Init);

							
