var matchedEmail = true;
var matchedPassword = true;
var addRows = true;

//Submits a form changing its action script on the go
//Takes the action script path
function submitForm(validated,actionScript)
{
	if(validated && matchedEmail && matchedPassword)
	{
		document.frm.action = actionScript;
		document.frm.target = "_self";
		document.frm.submit();
	}
}

//Opens a new window, uses the name of the file and height and the width
function openNewWindow(openWindow,height,width)
{
  window.open(openWindow, "_blank","height="+height+" width="+width+" scrollbars=1");
}

//Will determine the overall rating, depending on the value of the other ratings
function overallRating(total)
{
	var Sum = 0;
	var numChecked = 0;
	var initialTotal = total;
	
	for (var i = 0; i < document.frm.elements.length; i++)
	{
   		var e = document.frm.elements[i];
   		
		if (e.type == 'radio' && e.checked == true && e.disabled == false && e.name != "airlineRadio") 
		{
			numChecked++;
			var currentVal = parseFloat(e.value);
			
			//If it's 0, it means we put n/a as an answer, therefore
			//we are not including this in our evaluation and we want
			//to reduce the total number of categories included
			if(currentVal == 0)
				total--;
				
			Sum += parseFloat(e.value);
		}
	}
	
	if(numChecked >= initialTotal)
		document.frm.AllRated.value = "OK";
		
	var Avg = Sum/total;
	
	Avg = Math.round(Avg*100.0) / 100.0;
	
	//alert("Average is: " + Avg);
	
	if(Avg == 0 || isNaN(Avg)) Avg = "";
	
	document.frm.Overall.value = Avg;

	if(Avg > 0 && Avg < 1)
		document.frm.stars.src = "images/rating/stars_05.gif";
	else if(Avg >= 1 && Avg < 1.5)
		document.frm.stars.src = "images/rating/stars_10.gif";
	else if(Avg >= 1.5 && Avg < 2)
		document.frm.stars.src = "images/rating/stars_15.gif";
	else if(Avg >= 2 && Avg < 2.5)
		document.frm.stars.src = "images/rating/stars_20.gif";
	else if(Avg >= 2.5 && Avg < 3)
		document.frm.stars.src = "images/rating/stars_25.gif";
	else if(Avg >= 3 && Avg < 3.5)
		document.frm.stars.src = "images/rating/stars_30.gif";
	else if(Avg >= 3.5 && Avg < 4)
		document.frm.stars.src = "images/rating/stars_35.gif";
	else if(Avg >= 4 && Avg < 4.5)
		document.frm.stars.src = "images/rating/stars_40.gif";
	else if(Avg >= 4.5 && Avg < 5)
		document.frm.stars.src = "images/rating/stars_45.gif";
	else if(Avg >= 5)
		document.frm.stars.src = "images/rating/stars_50.gif";
}

//Will make sure that the values of the two passed fields, will be the same
//This will be used when registering a new user, when we have to make
//sure that they repeaded their password and email correctly
function matchValues(fieldName)
{
	var val1;
	var val2;
	
	if(fieldName == "Email")
	{
		val1 = document.frm.Email.value;
		val2 = document.frm.Email_rep.value;
		
		if(val1 == "" || val2 == "")
		{
			matchedEmail = false;
			return;
		}
		
		if(val1 != val2)
		{
			alert("Please repeat your " + fieldName + " correctly.");
			matchedEmail = false;
		}
		else
			matchedEmail = true;
	}
	
	else
	{
		val1 = document.frm.Password.value;
		val2 = document.frm.Password_rep.value;
		
		if(val1 == "" || val2 == "")
		{
			matchedPassword = false;
			return;
		}
		
		if(val1 != val2)
		{
			alert("Please repeat your " + fieldName + " correctly.");
			matchedPassword = false;
		}
		else
			matchedPassword = true;
	}	
}

function enableDropDown()
{
	document.getElementById("airlineDrop").disabled=false;
	document.getElementById("airlineDrop").options[0].value="";
	document.getElementById("airlineText").disabled=true;
}

function enableTextBox()
{
	document.getElementById("airlineDrop").disabled=true;
	//Set a value so that the validation doesn't complain
	document.getElementById("airlineDrop").options[0].value=".";
	document.getElementById("airlineText").disabled=false;
	document.frm.airlineText.value = "Type Airline Name";
}

function displayExplanation()
{
    if(addRows)
	{
		var tbody = document.getElementById
	("myTable").getElementsByTagName("TBODY")[0];
		var row = document.createElement("TR")
		var td = document.createElement("TD")
		
		td.height = "40";
		td.vAlign = "middle";
		 
		var text = document.createTextNode("NOTE: If you rate a custom airline we need to first verify its existence before it appears in our airline list and in the reviews.")
		
		td.appendChild(text)
		
		row.appendChild(td)
		
		tbody.appendChild(row);
	}
	
	addRows = false;
 }
 
 function hideExplanation()
 {
	 if(!addRows)
	 	document.all.myTable.deleteRow(1);
 	
	addRows = true;
 }

function searchAirline()
{
	//var link_a = document.getElementById("searchAirline");
	//link_a.style.fontWeight = "bold";
	//link_a.disabled = false;
	
	a.bgColor ="#b2c9f4";
	
	//var link_b = document.getElementById("searchAirport");
	//link_b.style.fontWeight = "normal";
	//link_b.disabled = true;
	
	b.bgColor = "#FFFFFF";
	
	document.frm.Find.value = "Type Airline here";
	document.frm.btn.value2= "airlineRatings.php";
}

function setActionScript()
{
	if(document.frm.btn.value2 == undefined)
		 document.frm.btn.value2= "airlineRatings.php";
}

function searchAirport()
{
	//var link_a = document.getElementById("searchAirport");
	//link_a.style.fontWeight = "bold";
	//link_a.disabled = false;
	
	a.bgColor ="#FFFFFF";
	
	//var link_b = document.getElementById("searchAirline");
	//link_b.style.fontWeight = "normal";
	//link_b.disabled = true;
	
	b.bgColor = "#b2c9f4";
	
	document.frm.Find.value = "Type Airport here";
	document.frm.btn.value2= "airportRatings.php";
}

function switchPage(goPage,args)
{
	window.location = "?page=" + goPage + "&" + args;
}

function termsOfUseAgree(returnVal)
{
	//Submit the registration form it all required fields were
	//filled out and also if the user agreed to the terms of use
	if(document.frm.policyAgree.checked && returnVal)
		submitForm(returnVal,"scripts/submitNewUser.php");
	else
	{
		//If all fields were filled out properly, display the alert.
		//Else the validation script will complain first.
		if(returnVal)
		{
			alert("In order to register with rateaflight.com you have to accept the Terms of Use");
		}
		
		return false;
	}
}
		
		
		
		
		
		
		
		
		
		
		
		