/**
 * @author Vivek Bajpai
 */

	var mapURL = new Array(
		"California~http://teen.idrivesafely.com/signup/?rm=displayCourseSelection&STATEID=CA&COUPON=CETEENDRCAV",
		"Colorado~http://teen.idrivesafely.com/signup/?rm=displayCourseSelection&STATEID=CO&COUPON=CETEENDRCOV",
		"Florida~http://teen.idrivesafely.com/signup/?rm=displayCourseSelection&STATEID=FL&COUPON=CETEENDRFLV",
		"Georgia~http://teen.idrivesafely.com/signup/?rm=displayCourseSelection&STATEID=GA&COUPON=CETEENDRGAV",
                "Indiana~http://teen.idrivesafely.com/signup/?rm=displayCourseSelection&STATEID=IN&COUPON=CETEENDRINV",
		"Nevada~http://teen.idrivesafely.com/signup/?rm=displayCourseSelection&STATEID=NV&COUPON=CETEENDRNVV",
		"Oklahoma~http://teen.idrivesafely.com/signup/?rm=displayCourseSelection&STATEID=OK&COUPON=CETEENDROKV",
		"Pennsylvania~http://teen.idrivesafely.com/signup/?rm=displayCourseSelection&STATEID=PA&COUPON=CETEENDRPAV",
		"Texas~http://teen.idrivesafely.com/signup/?rm=displayCourseSelection&STATEID=TX&COUPON=CETEENDRTXV",
		"Virginia~http://teen.idrivesafely.com/signup/?rm=displayCourseSelection&STATEID=VA&COUPON=CETEENDRVAV"
	);

    var mapLinkCourse = new Array(
        "California~california-drivers-education.html",
		"California~online-drivers-ed-california.html",
        "Colorado~colorado-drivers-education.html",
        "Florida~florida-learners-permit.html",
        "Georgia~georgia-drivers-education.html",
        "Indiana~indiana-drivers-education.html",
        "Nevada~nevada-drivers-education.html",
        "Oklahoma~oklahoma-drivers-education.html",
        "Pennsylvania~pa-drivers-permit.html",
        "Texas~texas-drivers-education.html",
        "Virginia~virginia-driver-education.html",
        "California~drivers-education-for-teen-california.html",
        "California~california-drivers-ed-online-permit.html",
        "Colorado~colorado-drivers-permit-requirements.html",
        "Colorado~colorado-learners-permit-practice-test.html",
        "Georgia~georgia-learners-permit-practice-test.html",
        "Oklahoma~oklahoma-drivers-license-practice-test.html"
    );

	var url = window.location.toString();
	url = url.split("/");

	var lengthUrl = url.length;
	var pageName = url[lengthUrl-1];
	var referrer = document.referrer;
      if(pageName.indexOf("?") >= 0) {
               var pageParams = pageName.split('?');
               if(pageParams.length==2){
               for(var p=0; p<pageParams.length; p++)
               {
                       if(pageParams[p] != '')
                       {
                               if(pageParams[p].indexOf("=") >= 0)
                               {
                                       var reqVals = pageParams[p].split('=');
                                       if(reqVals[0] == 'COUPON')
                                       {
                                               pageName='';
                                       }
                               }
                       }
               }
               }
       }

	if(pageName != "state-selection.html" && pageName != "driver-education-course.html" && pageName != "drivers-permit.html" && pageName != "learners-permit.html" && pageName != "auto-insurance-discount.html" && pageName != "defensive-driving-traffic-school.html" && pageName != "dmv-practice-test.html" && pageName != "drivers-ed.html" && pageName != "driving-record.html" && pageName != "free-autoinsurancediscount.html" && pageName != "mature-insurance-reduction-course.html" && pageName != "trafficschool-defensivedriving.html" && pageName != "Drivers Education" && pageName != "Parent Taught Drivers Education" && pageName != "Drivers Education Online" && pageName != "Cheap Drivers Ed" && pageName != "Driver Education Program" && pageName != "Teen Drivers Ed" && pageName != "Drivers Education Program" && pageName != "DMV Drivers Ed" && pageName != "Teen Driver Training" && pageName != "Drivers Education Tests" && pageName != "cheap-drivers-ed.html" && pageName != "dds-learners-permit-practice-test.html" && pageName != "dmv-drivers-education.html" && pageName != "driver-education-program.html" && pageName != "drivers-education-for-teen-california.html" && pageName != "drivers-education-online.html" && pageName != "drivers-education-program.html" && pageName != "drivers-education-tests.html" && pageName != "drivers-education.html" && pageName != "parent-taught-drivers-education.html" && pageName != "teen-driver-training.html" && pageName != "teen-drivers-ed.html" && pageName.indexOf("-")>0) {
		var statePageCookie = pageName.split('-');
		var cookieCreated = 0;
		var i=0;
		while(cookieCreated==0 && i<statePageCookie.length){
			var stateNamePageSelection = statePageCookie[i].slice(0,1).toUpperCase() + statePageCookie[i].slice(1);			
			var stateNamePageSelectionArr = stateNamePageSelection.split('.');
			var stateNamePageSelection = stateNamePageSelectionArr[0];
			if(stateNamePageSelection == "Pa") { stateNamePageSelection = "Pennsylvania"; cookieCreated=1;}
			else if(stateNamePageSelection == "Ca") { stateNamePageSelection = "California"; cookieCreated=1;}
			else if(stateNamePageSelection == "Co") { stateNamePageSelection = "Colorado"; cookieCreated=1;}
			else if(stateNamePageSelection == "Ga") { stateNamePageSelection = "Georgia"; cookieCreated=1;}
        	else if(stateNamePageSelection == "In") { stateNamePageSelection = "Indiana"; cookieCreated=1;}
			else if(stateNamePageSelection == "Nv") { stateNamePageSelection = "Nevada"; cookieCreated=1;}
			else if(stateNamePageSelection == "Ok") { stateNamePageSelection = "Oklahoma"; cookieCreated=1;}
			else if(stateNamePageSelection == "Tx") { stateNamePageSelection = "Texas"; cookieCreated=1;}
			else if(stateNamePageSelection == "Fl") { stateNamePageSelection = "Florida"; cookieCreated=1;}
			else if(stateNamePageSelection == "Va") { stateNamePageSelection = "Virginia"; cookieCreated=1;}
			else if(stateNamePageSelection == "Pennsylvania") { stateNamePageSelection = "Pennsylvania"; cookieCreated=1;}
			else if(stateNamePageSelection == "Florida") { stateNamePageSelection = "Florida"; cookieCreated=1;}
   			else if(stateNamePageSelection == "California") { stateNamePageSelection = "California"; cookieCreated=1;}
			else if(stateNamePageSelection == "Colorado") { stateNamePageSelection = "Colorado"; cookieCreated=1;}
			else if(stateNamePageSelection == "Georgia") { stateNamePageSelection = "Georgia"; cookieCreated=1;}
        	else if(stateNamePageSelection == "Indiana") { stateNamePageSelection = "Indiana"; cookieCreated=1;}
			else if(stateNamePageSelection == "Nevada") { stateNamePageSelection = "Nevada"; cookieCreated=1;}
			else if(stateNamePageSelection == "Oklahoma") { stateNamePageSelection = "Oklahoma"; cookieCreated=1;}
			else if(stateNamePageSelection == "Texas") { stateNamePageSelection = "Texas"; cookieCreated=1;}
			else if(stateNamePageSelection == "Virginia") { stateNamePageSelection = "Virginia"; cookieCreated=1;}			

			else { 	i++; continue;	}
			if(statePageCookie[i] != "") { createCookie("state", stateNamePageSelection); break;}
		}
	}

	if(pageName.indexOf("?") >= 0) {
		var stateVal = pageName.split("=");
		var lengthStateVal = stateVal.length;
		var state;
		if (stateVal[lengthStateVal - 1] != "") {
			state = stateVal[lengthStateVal - 1];
			if(state.indexOf("#") > 0) {
				var strLen = state.length;
				state = state.slice(0, strLen-1); 
			}
			createCookie("state", state);
		}
		pageName = pageName.split("?");
		pageName = pageName[0];
		location.href = getValue(readCookie("state"), 'course');
	}

	if(readCookie("state") == null) {
		if (readCookie("stateSel") == 1) {
			if (readCookie("pageSel") != 1) {
				createCookie("pageSel", "1");
				location.href = pageName;
			}
		}
	} else if(pageName == "state-selection.html" && readCookie("state")) {
		location.href = getValue(readCookie("state"), 'course');
	}


	function getValue(state, type) {
		var mapType = getType(type);
		flag = 0;
		for(var i=0; i<mapType.length; i++) {
			var stateVal = mapType[i].split("~");
			if(stateVal[0] == state) {
				flag = 1;
				return stateVal[1];
			}
		}
		if(!flag) { return 0; }
	}

	function getType(type) {
		if (type == "url") {
			mapType = mapURL;
		}
		if (type == "course") {
			mapType = mapLinkCourse;
		}
		return mapType;
	}

	$().ready(function(){
		if($('#stateBox').length) {
			var totalItems = document.getElementById("stateBox").length;
			for(var i=0; i<totalItems; i++) {
				if(readCookie("state") == document.getElementById("stateBox").options[i].text) {
					document.getElementById("stateBox").selectedIndex = i;
				}
			}
		}
		$('#courseLink').click(function(eve){
			if (readCookie("state")) {
				$('#courseLink').attr("href", getValue(readCookie("state"), 'course'))
			} else {
				location.href = "state-selection.html";
			}
		});
		$('#calink').click(function(eve){
			createCookie("state", "California");
			createCookie("stateSel", "1");
		});
		$('#colink').click(function(eve){
			createCookie("state", "Colorado");
			createCookie("stateSel", "1");
		});
		$('#fllink').click(function(eve){
			createCookie("state", "Florida");
			createCookie("stateSel", "1");
		});
		$('#galink').click(function(eve){
			createCookie("state", "Georgia");
			createCookie("stateSel", "1");
		});
		$('#nvlink').click(function(eve){
			createCookie("state", "Nevada");
			createCookie("stateSel", "1");
		});
		$('#oklink').click(function(eve){
			createCookie("state", "Oklahoma");
			createCookie("stateSel", "1");
		});
		$('#palink').click(function(eve){
			createCookie("state", "Pennsylvania");
			createCookie("stateSel", "1");
		});
		$('#txlink').click(function(eve){
			createCookie("state", "Texas");
			createCookie("stateSel", "1");
		});
		$('#valink').click(function(eve){
			createCookie("state", "Virginia");
			createCookie("stateSel", "1");
		});
		$('#calink0').click(function(eve){
			createCookie("state", "California");
			createCookie("stateSel", "1");
		});
		$('#colink0').click(function(eve){
			createCookie("state", "Colorado");
			createCookie("stateSel", "1");
		});
		$('#fllink0').click(function(eve){
			createCookie("state", "Florida");
			createCookie("stateSel", "1");
		});
		$('#galink0').click(function(eve){
			createCookie("state", "Georgia");
			createCookie("stateSel", "1");
		});
		$('#nvlink0').click(function(eve){
			createCookie("state", "Nevada");
			createCookie("stateSel", "1");
		});
		$('#oklink0').click(function(eve){
			createCookie("state", "Oklahoma");
			createCookie("stateSel", "1");
		});
		$('#palink0').click(function(eve){
			createCookie("state", "Pennsylvania");
			createCookie("stateSel", "1");
		});
		$('#txlink0').click(function(eve){
			createCookie("state", "Texas");
			createCookie("stateSel", "1");
		});
		$('#valink0').click(function(eve){
			createCookie("state", "Virginia");
			createCookie("stateSel", "1");
		});

		$('#calink0').click(function(eve){
			createCookie("state", "California");
			createCookie("stateSel", "1");
		});
		$('#colink0').click(function(eve){
			createCookie("state", "Colorado");
			createCookie("stateSel", "1");
		});
		$('#fllink0').click(function(eve){
			createCookie("state", "Florida");
			createCookie("stateSel", "1");
		});
		$('#galink0').click(function(eve){
			createCookie("state", "Georgia");
			createCookie("stateSel", "1");
		});
		$('#nvlink0').click(function(eve){
			createCookie("state", "Nevada");
			createCookie("stateSel", "1");
		});
		$('#oklink0').click(function(eve){
			createCookie("state", "Oklahoma");
			createCookie("stateSel", "1");
		});
		$('#palink0').click(function(eve){
			createCookie("state", "Pennsylvania");
			createCookie("stateSel", "1");
		});
		$('#txlink0').click(function(eve){
			createCookie("state", "Texas");
			createCookie("stateSel", "1");
		});
		$('#valink0').click(function(eve){
			createCookie("state", "Virginia");
			createCookie("stateSel", "1");
		});

		$('#calink2').click(function(eve){
			createCookie("state", "California");
			createCookie("stateSel", "1");
		});
		$('#colink2').click(function(eve){
			createCookie("state", "Colorado");
			createCookie("stateSel", "1");
		});
		$('#fllink2').click(function(eve){
			createCookie("state", "Florida");
			createCookie("stateSel", "1");
		});
		$('#galink2').click(function(eve){
			createCookie("state", "Georgia");
			createCookie("stateSel", "1");
		});
		$('#nvlink2').click(function(eve){
			createCookie("state", "Nevada");
			createCookie("stateSel", "1");
		});
		$('#oklink2').click(function(eve){
			createCookie("state", "Oklahoma");
			createCookie("stateSel", "1");
		});
		$('#palink2').click(function(eve){
			createCookie("state", "Pennsylvania");
			createCookie("stateSel", "1");
		});
		$('#txlink2').click(function(eve){
			createCookie("state", "Texas");
			createCookie("stateSel", "1");
		});
		$('#valink2').click(function(eve){
			createCookie("state", "Virginia");
			createCookie("stateSel", "1");
		});

		$('#calink3').click(function(eve){
			createCookie("state", "California");
			createCookie("stateSel", "1");
		});
		$('#colink3').click(function(eve){
			createCookie("state", "Colorado");
			createCookie("stateSel", "1");
		});
		$('#fllink3').click(function(eve){
			createCookie("state", "Florida");
			createCookie("stateSel", "1");
		});
		$('#galink3').click(function(eve){
			createCookie("state", "Georgia");
			createCookie("stateSel", "1");
		});
		$('#nvlink3').click(function(eve){
			createCookie("state", "Nevada");
			createCookie("stateSel", "1");
		});
		$('#oklink3').click(function(eve){
			createCookie("state", "Oklahoma");
			createCookie("stateSel", "1");
		});
		$('#palink3').click(function(eve){
			createCookie("state", "Pennsylvania");
			createCookie("stateSel", "1");
		});
		$('#txlink3').click(function(eve){
			createCookie("state", "Texas");
			createCookie("stateSel", "1");
		});
		$('#valink3').click(function(eve){
			createCookie("state", "Virginia");
			createCookie("stateSel", "1");
		});

		$('#calink4').click(function(eve){
			createCookie("state", "California");
			createCookie("stateSel", "1");
		});
		$('#colink4').click(function(eve){
			createCookie("state", "Colorado");
			createCookie("stateSel", "1");
		});
		$('#fllink4').click(function(eve){
			createCookie("state", "Florida");
			createCookie("stateSel", "1");
		});
		$('#galink4').click(function(eve){
			createCookie("state", "Georgia");
			createCookie("stateSel", "1");
		});
		$('#nvlink4').click(function(eve){
			createCookie("state", "Nevada");
			createCookie("stateSel", "1");
		});
		$('#oklink4').click(function(eve){
			createCookie("state", "Oklahoma");
			createCookie("stateSel", "1");
		});
		$('#palink4').click(function(eve){
			createCookie("state", "Pennsylvania");
			createCookie("stateSel", "1");
		});
		$('#txlink4').click(function(eve){
			createCookie("state", "Texas");
			createCookie("stateSel", "1");
		});
		$('#valink4').click(function(eve){
			createCookie("state", "Virginia");
			createCookie("stateSel", "1");
		});

		$('#calink5').click(function(eve){
			createCookie("state", "California");
			createCookie("stateSel", "1");
		});
		$('#colink5').click(function(eve){
			createCookie("state", "Colorado");
			createCookie("stateSel", "1");
		});
		$('#fllink5').click(function(eve){
			createCookie("state", "Florida");
			createCookie("stateSel", "1");
		});
		$('#galink5').click(function(eve){
			createCookie("state", "Georgia");
			createCookie("stateSel", "1");
		});
		$('#nvlink5').click(function(eve){
			createCookie("state", "Nevada");
			createCookie("stateSel", "1");
		});
		$('#oklink5').click(function(eve){
			createCookie("state", "Oklahoma");
			createCookie("stateSel", "1");
		});
		$('#palink5').click(function(eve){
			createCookie("state", "Pennsylvania");
			createCookie("stateSel", "1");
		});
		$('#txlink5').click(function(eve){
			createCookie("state", "Texas");
			createCookie("stateSel", "1");
		});
		$('#valink5').click(function(eve){
			createCookie("state", "Virginia");
			createCookie("stateSel", "1");
		});

	});


