//<!-- Hide from other browsers
window.onerror = null;
//Global Arrays
var planksArray = new Array(new Array("Strongly support a constitutional amendment allowing women the right to vote.","Favor a constitutional amendment allowing woman's suffrage, but do so in a manner which will not alienate anti-suffrage voters","Refuse to address woman's suffrage as a federal issue.","Strongly oppose any legislation which would support a woman's right to vote."),
new Array("Strongly advocate racial equality under the law.","Appeal to African-American voters with only general assurances, so as not to alienate some voters.","Ignore race issues in order to appeal to certain blocks of voters."),
new Array("Break up the trusts into several small, independent companies.","Regulate the trusts by federal oversight.","Promote American business, including the trusts.","Hand over control of the trusts to collective worker ownership."),
new Array("Lower tariffs across the board.","Maintain tariff protections, but form an expert administrative body to scientifically regulate rates according to an industry-by-industry basis.","Enforce and expand the tariff as a means to promote American business and protect American labor.","The tariff issue is an inconsequential 'sham battle' relative to the more compelling plight of labor."),
new Array("The highest priority of my campaign is to make laws protecting and enhancing labor's rights at all costs.","Favor a laissez-faire approach to the labor-capital relationship, allowing the courts to rule against labor unions as current laws rightly dictate.","Advocate the much-needed rights of labor, in order to find a fine balance between the needs of the economy and the needs of the worker."));
var planksPointValueArray = new Array(new Array(.855714,3.912857,5.977143,3.311429),new Array(.855714,5.977143,3.612143),
new Array(5.977143,3.912857,3.311429,.855714),new Array(5.977143,3.912857,3.311429,.855714),new Array(.855714,3.311429,5.977143));
var pageArray = new Array("sf_register.html","sf_platform.html","sf_suffrage.html","sf_race.html","sf_trusts.html","sf_tariff.html",
                "sf_labor.html","sf_campaign.html","sf_results.html");
//Global vars
//flag for debugging
var debug = false;

//What browser is it?
var version="unknown";
var browserName = navigator.appName;
var browserVersion = parseInt(navigator.appVersion);

if (browserName == "Netscape")
{
   if (browserVersion >= 3)
   {
      version = "n3";
   }
   else
   {
      version = "n2";
   }
}
if (browserName == "Microsoft Internet Explorer")
{
   if (browserVersion >= 4)
   {  
      version = "e4";
   }
   else
   {
      version = "e3";
   }
 }

//Set variables for this document
//form name
var formName="appForm";

//name of the cookie for form fields to be stored in
var cookieName = "1912ElectionInteractiveGame";

//delimter for storing fields in the cookie
var separator = "|";
//default values for cookie if does not exist

var platformArray = new Array("Enter party name",0,0,0,0,0)

initialize();
//load values in text field if cookie exist

function initialize()
{
   if (version == "n3" || version == "e4")
   {  
      var electionCookie = getCookie(cookieName);
	  if (electionCookie == null)
	  {
	    //create the cookie with default values
		var data ="";
		for (i=0 ; i<platformArray.length ; i++)
		{
		    var temp = platformArray[i] + separator;
		   data += temp;
		}
	if (debug == true) 
	{
	    alert('creating new cookie for ' + getName() + ' this data ' + data);
	}
		setCookie(cookieName,data.substring(0,data.length-1),expiry); //clip off extra delimiter  
	  }
	  else
	  {
	   
		 //load data from cookie and put temp array
	    var  tempArray = electionCookie.split(separator);
		var testlook = "";
		for (i=0; i<tempArray.length;i++)
		{
		    platformArray[i] = tempArray[i];
			testlook +=platformArray[i]; 
        }
		if (debug == true)
		{
	     	alert('loading cookie for ' + getName() + ' with this data ' + testlook );
	 	}
	  }
	}// end if (version
	
}// end initialize
function next(frm,currentPageIndex)
{
	var nextPage = 0;
	
	with (frm)
	{
	 	if (!isRadioOnForm(frm))
		{
	 		if (currentPageIndex == 0)
			{
				setName(document.appForm.name.value);
				saveElectionInfo();
				self.location.href=pageArray[currentPageIndex + 1];
			}
			else
			{
				self.location.href=pageArray[currentPageIndex + 1];
			}
		}
		else
		{
			if(isRadioSelected(frm))
			{
				getSelectedValue(frm);
				saveElectionInfo();
				self.location.href=pageArray[currentPageIndex + 1];
			}
			else
			{
				alert('Please make a selection before continuing to the next page.');
			}
		}	
	}//end with
	
}
function previous(currentPageIndex)
{
	if (currentPageIndex == 8|| currentPageIndex==7)
	{
	   resetPlanks();
	   self.location.href = pageArray[0];
	}
	else
	{
	   self.location.href = pageArray[currentPageIndex - 1];
    }
}
function getSelectedValue(frm) 
{
   var selectedValue = "undefined";
   
   with (frm) 
   {
       for (var i = 0; i < document.appForm.radioButton.length; i++) 
	   {
           if (document.appForm.radioButton[i].checked) 
		   {
              selectedValue = document.appForm.radioButton[i].value;
			  var question = selectedValue.substring(0,1);
			  platformArray[question] = selectedValue;
			  
			  if (debug == true)
			  {
					alert(' platformarray value is ' + platformArray[question]);
		      }
        	break;
            }
        }	
	}
}
function isRadioOnForm(frm) 
{
   with (frm) 
   {
       for (var i = 0; i < document.appForm.elements.length; i++) 
	   {
           if (document.appForm.elements[i].type == "radio") 
		   {
              return true;
			  
		      if (debug == true)
		      {
		          alert('radio button on form');
		       }
               break;
           }
        }
		return false;	
	}
}
function radioInit() 
{
   for (var i = 0; i < document.appForm.radioButton.length; i++) 
   {
    	for (var j = 1; j<platformArray.length;j++)
     	{
 		 	if (document.appForm.radioButton[i].value == platformArray[j])
			{
			  document.appForm.radioButton[i].checked = true;
			}
		}
    }
}
						
function isRadioSelected(frm) 
{
   with (frm) 
   {
       for (var i = 0; i < document.appForm.radioButton.length; i++) 
	   {
           if (document.appForm.radioButton[i].checked) 
		   {
              return true;
			  
				if (debug == true)
				{
					alert('No radio button was checked');
				}
            	break;
           }
        }
		return false;	
	}
}

function printMessage()
{
    var message ="";
    var totalValues = getResults();
    if ( totalValues >= 24.764)
	{
		document.location = "sf_victory.html";
	   // message = " Congratulations!!! You have won the Election of 1912 by a landslide!";
	}
	/*
	else if ( totalValues >= 18.362 && totalValues < 24.764)
	{
	    message = " Congratulations. You have won the Election of 1912!";
	}
	else if ( totalValues >= 17.082 && totalValues < 18.362)
	{
	    message = " Whew! It was a close one, but you won the Election of 1912!";
	}
	else if ( totalValues >= 15.801 && totalValues < 17.082)
	{
	    message = " What a close race. You were just barely edged out of the Election of 1912.";
	}
	else if ( totalValues >= 9.399 && totalValues < 15.801)
	{
	    message = " You lost the Election of 1912.";
	}
	else if ( totalValues < 9.399)
	{
        message =" It was a crushing defeat. Maybe you should review the planks of your platform.";
	}
	*/
	else
	{
	
		document.location = "sf_loss.html"
	}
	
    //document.write(message);
	
}
function getResults()
{
    //sum of your answer values here
	var totalValue = 0.0;   
    for(var i = 1;i<platformArray.length;i++)
	{
        var temp = platformArray[i];
		totalValue = totalValue + planksPointValueArray[i-1][temp.substring(1,2)-1]; 
	}	
	return totalValue;
}
function getName()
{
   return this.platformArray[0];
}   
function setName(value)
{
   this.platformArray[0]=value;
}
//prints name of candidate
function printName()
{
    document.write(getName());
}
// prints planks of platform
function printPlanks()
{
 	for(var i = 1;i<platformArray.length;i++)
	{
        var temp = platformArray[i];
		document.writeln(planksArray[i-1][temp.substring(1,2)-1]+ '<br><br>'); 
	}
}

function resetPlanks()
{
	for (var i=1;i<platformArray.length;i++)
	{
		platformArray[i] = 0;
	}
	saveElectionInfo();
}

// save field values to cookie
function saveElectionInfo()
{
   if (version == "n3" || version == "e4")
   {
      	var data ="";
		for (i=0 ; i<platformArray.length ; i++)
		{
		   temp = platformArray[i] + separator;
		   data += temp;
		}
		//alert(data);
	  setCookie(cookieName,data.substring(0,data.length-1),expiry); //clip off extra delimiter 
	
	}// end if 	version
 }// end saveDefaults
//-->
///*****************************************************************************************************************************
// I would like to send a shout out to Bill Dortch code at http://www.hidaho.com/cookies/cookie.txt of which this code is derived from

var today = new Date();
var expiry = new Date(today.getTime() + 30 * 24 * 60 * 60 * 1000);

function getCookieVal (offset)
{
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) 
	{ 
	   endstr = document.cookie.length; 
	}
	return unescape(document.cookie.substring(offset, endstr));
}

function getCookie (name) 
{
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	//alert('clen is '+ clen);
	var i = 0;
	while (i < clen) 
	{
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) 
		{
			var temp = getCookieVal(j);
			if ((temp=="n") || (temp==null))
			{
			   temp = "X";
			}
			return temp;
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break; 
	}
	return null;
}

function deleteCookie (name,path,domain) 
{
	if (getCookie(name)) 
	{
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function setCookie (name,value,expires,path,domain,secure) 
{
  document.cookie = name + "=" + escape (value) +
    ((expiry) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}