// JavaScript Document

/*
The following functions 
fulfil these project requirements:
2. At least one function in an external JavaScript file
*/

/* rollover image */

if (document.images) {
	var ImgOver = new Array();
	ImgOver[1] = new Image();
	ImgOver[2] = new Image();
	ImgOver[3] = new Image();
		ImgOver[1].src = "images/itahome2.gif";
		ImgOver[2].src = "images/nextdestination2.gif";
		ImgOver[3].src = "images/bookflight2.gif";
	
	var ImgOut = new Array();
	ImgOut[1] = new Image();
	ImgOut[2] = new Image();
	ImgOut[3] = new Image();
		ImgOut[1].src = "images/itahome.gif";
		ImgOut[2].src = "images/nextdestination.gif";
		ImgOut[3].src = "images/bookflight.gif";
}
		
function RollOver(i) {
	if (document.images) document.images[i].src = ImgOver[i].src;
}

function RollOut(i) {
	if (document.images) document.images[i].src = ImgOut[i].src;
}

/* random items */

/*
The following functions, lines 46 to 55, 
fulfil these project requirements:
7. At least one math object method call
*/

function randInt(lower, size) {
	return Math.floor(lower + size*Math.random());
}

function chooseRandom(anyArray) {
	var arrayLength = anyArray.length;
	var randNum = randInt(0,arrayLength);
	var randItem = anyArray[randNum];
	return randItem;
}

/* country name */

var start = chooseRandom(countryStart);
var end = chooseRandom(countryEnd);
var countryName = start+end

function name() {
	var id = document.getElementById("destination");
	var txt = document.createTextNode(countryName);
	id.appendChild(txt);
}

function writeCountryName() {
	document.write(countryName);
}

/* description */

/*
The following functions, lines 80 to 88 and 158 to 171, 
fulfil these project requirements:
11. At least one regular expression
*/

function adj1() {
	var adj = chooseRandom(adjArray);
	re = /^[aeiou]/;
	if (re.test(adj) == true) {
		return "an "+adj;
	} else {
		return "a "+adj;
	}
}

function noun1() {
	var randNum = randInt(0,10);
	if (randNum < 2) {
		var noun = chooseRandom(peopleNouns);
	} else if (randNum < 4) {
		var noun = chooseRandom(peopleNounsTwo);
	} else if (randNum < 8) {
		var noun = chooseRandom(geoNouns);
	} else {
		var noun = chooseRandom(geoNounsTwo);
	}
	return noun;
}

function gov() {
	var randNum = randInt(0,10);
	var lastName = chooseRandom(lastNameArray);
	if (randNum < 7) {
		var govType = chooseRandom(govTypeM);
		var firstName = chooseRandom(firstNameM);
	} else {
		var govType = chooseRandom(govTypeF);
		var firstName = chooseRandom(firstNameF);
	}
	return govType+" "+firstName+" "+lastName;
}

function nouns() {
	var randNum = randInt(0,10);
	if (randNum < 5) {
		var noun2 = chooseRandom(peopleNouns);
		var noun3 = chooseRandom(peopleNounsTwo);
	} else {
		var noun2 = chooseRandom(geoNouns);
		var noun3 = chooseRandom(geoNounsTwo);
	}
	return {noun2 : noun2, noun3 : noun3};
}
	
function desc() {
	var type = chooseRandom(countryTypeArray);
	var knownFor = chooseRandom(knownForArray);
	var adj2 = chooseRandom(adjArray);
	var adj3 = chooseRandom(adjArray);
	var famousFor = chooseRandom(famousForArray);
	var word = nouns();
	var id = document.getElementById("description");
	var sentence1 = countryName+" is "+adj1()+" "+type+" "+knownFor+" "+adj2+" "+noun1()+".";
	var sentence2 = " It is governed by "+gov();
	var sentence3 = ". The "+word.noun2+" of "+countryName+" are "+famousFor+" "+adj3+" "+word.noun3+".";
	var txt = document.createTextNode(sentence1+sentence2+sentence3);
	id.appendChild(txt);
}

/* image */

function writeImg() {
	var randNum = randInt(0,10);
	var flickrTag = chooseRandom(tagArray);
	if (randNum < 8) {
		document.write("<script type='text/javascript' src='http://www.flickr.com/badge_code_v2.gne?count=1&display=random&size=m&layout=h&source=all_tag&tag="+flickrTag+"'><\/sc" + "ript>");	
	} else {
		document.write("<script type='text/javascript' src='http://www.flickr.com/badge_code_v2.gne?show_name=1&count=1&display=random&size=m&layout=h&source=all&user=9875630%40N02'><\/sc" + "ript>");	
	}	
}

/* currency */

	var startCurrency = chooseRandom(worldCurrency);
	var regex1 = /^[A-Z]/;
	var newConsonent = chooseRandom(consonents);
	var secondCurrency = startCurrency.replace(regex1,newConsonent);
	var regex2 = /[aeiou]/;
	var newVowel = chooseRandom(vowels);
	var newCurrency = secondCurrency.replace(regex2,newVowel);
	var randNum = chooseRandom(numbers);

function money() {
	var id = document.getElementById("currency");
	var txt = document.createTextNode("The currency of "+countryName+" is the "+newCurrency+". How far will your money go in "+countryName+"? Enter a dollar amount below to convert it to "+newCurrency+"s.");
	id.appendChild(txt);
}

/*
The following function, lines 179 to 193, 
fulfils these project requirements:
4a. At least one while or do while loop
*/

function writeCurrencyValue() {
		var li = document.createElement("li");
		var currencylist = document.getElementById("currencylist");
		currencylist.appendChild(li);
		var dollarValue = document.forms.currency.dollars.value;
		secondValue = parseInt(dollarValue);
		var i = 0;
		while (i<3) {
			thirdValue = secondValue*randNum;
			i++;
		}
		fourthValue = thirdValue+"";
		var result = document.createTextNode(dollarValue+" dollars equals "+fourthValue+" "+newCurrency+"s");
		li.appendChild(result);
	}
	
/* language */

var langEnd = chooseRandom(langEndArray);
var newLanguage = start+langEnd;

function lang() {
	var id = document.getElementById("language");
	var txt = document.createTextNode("The official language of "+countryName+" is "+newLanguage+". How do you say Hello in "+countryName+"? Enter an English word to translate it into "+newLanguage+".");
	id.appendChild(txt);
}

/* bookflight functions */

function writeUrl() {
	document.write("<a href='bookflight.html\?countryName="+countryName+"'>");
}

passedName = document.location.search.substring(13);
var fixedName = unescape(passedName);


function writePassed() {
	if (passedName == "") {
		document.write("Return to the Home Page to choose a destination.");
	} else {
		document.write("Book Your Flight to "+fixedName);
	}
}

function book() {
	if (passedName == "") {
		document.book.destination.value="";
	} else {
		document.book.destination.value=fixedName;
	}
}

/*
The following variables and function, lines 238 to 306, 
fulfil these project requirements:
12. At least one cookie that gets saved and retrieved
*/

var thisDate = new Date();
var thisYear = thisDate.getFullYear();

function year() {
	document.book.dyear.value=thisYear;
}

/*
The following functions, lines 251 to 243, 
fulfil these project requirements:
6. At least one date object method call
*/

function makeCookie(tourist,lastTrip) {
	document.cookie = "tourist = "+tourist;
	document.cookie = "lastTrip = "+lastTrip;
}

/*
The following function, lines 262 to 282, 
fulfil these project requirements:
4c. if or if else control structure
10. At least form field validated on the client using JavaScript
*/

function checkForm() {
	var tourist = document.book.name.value;
	var lastTrip = document.book.destination.value;
	if (document.book.name.value == "") {
		alert ("Please enter your name, or the name of someone you know, or, if you prefer, a completely made-up appellation.");
		return false;
		} else if (document.book.destination.value == "") {
		alert ("Please enter a destination, any destination, doesn't matter where it is, we'll fly you there, or your money back.");
		return false;
		} else if (document.book.cardnumber.value == "") {
		alert ("Please enter an imaginary card number. No actual credit card numbers will be accepted.");
		return false;
		} else if (document.book.roundtrip.checked) {
			if (document.book.ryear.value =="") {
		alert ("You have chosen a round-trip ticket but have not specified a return date. Please specify a return date or change your selection to one-way. Remember, once you're there you can always choose to stay. We won't force you to come home.");
			return false;
			} else {
			makeCookie(tourist,lastTrip);
			}
		}
}

function findCookie(val) {
	var cookie = null;
	var findVal = val + "=";
	var dc = document.cookie;
	if (dc.length > 0) {
		var start = dc.indexOf(findVal);
		if (start >= 0) {
			start += findVal.length;
			lastVal = dc.indexOf(";", start);
			if (lastVal == -1) {
				lastVal = dc.length;
			}
			cookie = (dc.substring(start, lastVal));
		} else {
			return cookie;
		}
	}
	return cookie;
}

tourist = findCookie("tourist");
lastTrip = findCookie("lastTrip");

function writeGreeting() {
	if (tourist == null) {
		document.write ("ITA will take you to places no other travel agency can. That's because all our destinations are purely imaginary. So, if you're ready for a truly unusual vacation, check out our featured destinations below. When you find one you like, simply book your flight and you'll be on your way. No waiting in line. Remember, this journey is all in your mind. Happy Flying!");
	} else {
	document.write ("Welcome back "+tourist+"! Did you enjoy your trip to "+lastTrip+"? If so, check out some of our other great destinations.");
	}
}

function writePrice () {
	var price = chooseRandom(dollarArray);
	document.write (price);
}

/* thankyou functions */

function writeFlightInfo() {
	var confCode = chooseRandom(confArray);
	searchString = location.search.slice(1);
	searchString = unescape(searchString);
	formString = searchString.replace(/\+/g, " ");
	data = formString.split(/[&=]/g);
	if (data[5] == "roundtrip") {
	document.write("Congratulations "+data[1]+"! You have successfully booked your round-trip flight to "+data[3]+", departing on "+data[7]+" "+data[9]+" "+data[11]+" at "+data[13]+", and returning on "+data[15]+" "+data[17]+" "+data[19]+" at "+data[21]+". Your confirmation code is: "+confCode+". Please take your code to the imagination-port on the date and time of your departure. Have a great trip!");
	} else if (data[5] == "oneway") {
	document.write("Congratulations "+data[1]+"! You have successfully booked your one-way flight to "+data[3]+", departing on "+data[7]+" "+data[9]+" "+data[11]+" at "+data[13]+". Your confirmation code is: "+confCode+". Please take your code to the imagination-port on the date and time of your departure. Have a great trip!");
	} else {
		document.write("Return to the Home Page to choose a destination.");
	}
}

function xCoord(id) {
	if (document.getElementById) {
  	var obj = document.getElementById(id);
    xc = parseInt(obj.style.left);
    return xc;
	}
}

function yCoord(id) {
	if (document.getElementById) {
  	var obj = document.getElementById(id);
    yc = parseInt(obj.style.top);
    return yc;
  }
}

function placeIt(id, x, y) {
  if (document.getElementById) {
  	var obj = document.getElementById(id);
    obj.style.left = x + "px";
    obj.style.top = y + "px";
  }
}

function shiftIt(id, dx, dy) {
	if (document.getElementById) {
		obj = document.getElementById(id);
    obj.style.left = xCoord(id) + dx + "px";
    obj.style.top = yCoord(id) + dy + "px";
  }
}

var index = 0;

function moveAnim() {
	if (index < 34) {
  	shiftIt("anim", 17, 0);
    index++;
    setTimeout("moveAnim()", 50);
    } else { 
  }
}