/*
 * landingpage.js
 *
 * 19-MAR-2008	DNK T1637: Updated
 *
 * Sections:
 *		Load handlers
 *		Event handlers
 *		Validators
 *		Date picker
 *		Utilities
 *		Cookie handling
 *		Country data
 */

////// Load handlers


function load(pageNum)
{
	//alert("1");
	loadImages();
	switch (pageNum)
	{
	case 1:
		$("name").focus();
		loadCountries($("fcountry"));
		loadCountries($("tcountry"));
		break;
	case 2:
		load2();
		$("faddr1").focus();
		break;
	case 3:
		load3();
		$("packaging_option").focus();
		break;
	case 4:
		break;
	}
}

var is_store = false;
var is_local = false;
function load2()
{
	var crumb = getAspCookie("rapidquote", "store"); 
	if (crumb != null && crumb != "" && crumb != "0")
		is_store = true;
	$("move_store_head").innerHTML = ((is_store) ? "Storage Details:" : "Moving to:");
	for (var i = 0; true; i++)
	{
		var m = $("move_block_" + i);
		if (m != null)
			m.style.display = ((is_store) ? "none" : "block");
		var s = $("store_block_" + i);
		if (s != null)
			s.style.display = ((is_store) ? "block" : "none");
		if (m == null && s == null)
			break;
	}
	loadCountries($("fcountry"));
	loadCountries($("tcountry"));
	setCountry();
	setCookies();
	setKentStore();
	setAccessDet();
	setDistances();
	setBedrooms();
}

function load3()
{

	/* Placed these in the webpage as the values will be saved 
	var rooms = 0;
	var crumb = getAspCookie("rapidquote", "bedrooms"); 
	if (crumb != null && crumb != "" && !isNaN(parseInt(crumb)))
		rooms = parseInt(crumb);
	crumb = getAspCookie("rapidquote", "local"); 
	if (crumb != null && crumb != "" && crumb != "0")
		is_local = true;
	var qty = [
		[16,  2,  6, 2,  4, 2,  2, 1, 1],	// 1 room
		[25,  4,  8, 4,  6, 3,  4, 1, 2],
		[40,  6, 20, 6, 10, 4,  6, 1, 3],
		[50,  8, 30, 8, 12, 6,  8, 1, 3],
		[60, 10, 30, 8, 15, 8, 12, 1, 4]];	// 5 room
	var q = qty[rooms - 1];
	$("po1_1").value = q[0];	// multi
	$("po1_3").value = q[2];	// book
	$("po1_5").value = q[4];	// treasure
	$("po1_2").value = q[6];	// fashion
	$("po1_4").value = q[1];	// flexi
	$("po1_6").value = q[3];	// bottle
	$("po2_6").value = q[5];	// tape
	$("po2_8").value = q[7];	// dispenser
	$("po2_9").value = q[8];	// paper
	*/
	setPackOption();
	//setVals();

}

var _images = new Array();
function loadImages()
{
	var gifs = ["Back", "Cancel", "Help", "Home", "Next", "Submit", ];
	for (var i = 0; i < gifs.length; i++)
	{
		var img = new Image;
		img.src = "images/" + gifs[i] + ".gif";
		_images.push(i);
		img.src = "images/" + gifs[i] + "_Over.gif";
		_images.push(i);
	}
}

function loadCountries(select)
{
	while (select.length > 0)
		select.remove(0);
	for (var i = 0; i < countryList.length; i++)
	{
		var opt = document.createElement("option");
		if (i == 0)
			opt.value = "";
		else
			opt.value = countryList[i];
		opt.text = countryList[i];
		try { select.add(opt); }
		catch(ex) { select.add(opt,null); }
	}
	select.selectedIndex = 0;		
}

////// Event handlers

function poChanged(po)
{
	var po_value = parseInt(po.options[po.selectedIndex].value);
	for (var i = 1; i <= 3; i++)
		$("po" + i).style.display = ((i == po_value) ? "block" : "none");
	$("po_content").style.display = ((1 <= po_value && po_value <= 3) ? "block" : "none");
	$("po_consumables").style.display = ((po_value != 1) ? "block" : "none");


	var rooms = 0;
	var crumb = getAspCookie("rapidquote", "bedrooms"); 
	if (crumb != null && crumb != "" && !isNaN(parseInt(crumb)))
		rooms = parseInt(crumb);

	var qty = [
		[16,  2,  6, 2,  4, 2,  2, 1, 1],	// 1 room
		[25,  4,  8, 4,  6, 3,  4, 1, 2],
		[40,  6, 20, 6, 10, 4,  6, 1, 3],
		[50,  8, 30, 8, 12, 6,  8, 1, 3],
		[60, 10, 30, 8, 15, 8, 12, 1, 4]];	// 5 room
	var q = qty[rooms - 1];
   if (po_value == 4 ) {
      $("po1_1").value = 0;	// multi
      $("po1_3").value = 0;	// book
      $("po1_5").value = 0;	// treasure
      $("po1_2").value = 0;	// fashion
      $("po1_4").value = 0;	// flexi
      $("po1_6").value = 0;	// bottle
      $("po2_6").value = 0;	// tape
      $("po2_8").value = 0;	// dispenser
      $("po2_9").value = 0;	// paper

   } else {
      $("po1_1").value = q[0];	// multi
      $("po1_3").value = q[2];	// book
      $("po1_5").value = q[4];	// treasure
      $("po1_2").value = q[6];	// fashion
      $("po1_4").value = q[1];	// flexi
      $("po1_6").value = q[3];	// bottle
      $("po2_6").value = q[5];	// tape
      $("po2_8").value = q[7];	// dispenser
      $("po2_9").value = q[8];	// paper
   }

   if (po_value == 1) {
      $("po2_6").value = 0;	// tape
      $("po2_8").value = 0;	// dispenser	
      $("po2_9").value = 0;	// paper
   } 
   /*else {
      $("po2_6").value = 0;	// tape
      $("po2_8").value = 0;	// dispenser	
      $("po2_9").value = 0;	// paper
   }

   if (po_value == 4) {
      alert("br01");
      $("po2_6").value = 0;	// tape
      $("po2_8").value = 0;	// dispenser	
      $("po2_9").value = 0;	// paper
   } */
}
 
function inc(id)
{
	var v;
	v = Number(V(id));
	if (isNaN(v) )
		v = 0;
	v += 1;
	$(id).value = v;
}

function dec(id)
{
	var v;
	v = Number(V(id));
	if (isNaN(v))
		v = 0;
	if (v > 0) v -= 1;
	$(id).value = v;
}

function incItem(e)
{
	if (!e)
		e = window.event;
	var o;
	if (e.srcElement)
		o = e.srcElement;
	else if (e.target)
		o = e.target;
	var td = o.parentNode;
	while (td.tagName.toLowerCase() != "td")
		td = td.parentNode;
	var input = td.parentNode.cells[2].childNodes[0];
	if (!input || input.tagName.toLowerCase() != "input")
		input = td.parentNode.cells[1].childNodes[0];
	var n = Number(input.value);
	if (isNaN(n))
		n = 1;
	else 
		n += 1;
		
	input.value = n;	
}

function decItem(e)
{
	if (!e)
		e = window.event;
	var o;
	if (e.srcElement)
		o = e.srcElement;
	else if (e.target)
		o = e.target;
	var td = o.parentNode;
	while (td.tagName.toLowerCase() != "td")
		td = td.parentNode;
	var input = td.parentNode.cells[2].childNodes[0];
	if (!input || input.tagName.toLowerCase() != "input")
		input = td.parentNode.cells[1].childNodes[0];
	var n = Number(input.value);
	if (isNaN(n))
		n = 0;
	else 
		n -= 1;
	if (n < 0) n = 0;
	input.value = n;	
	
}

function vdate(o, id)
{
	if (!o.value.parseDMY())
		$(id).innerHTML = "(dd/mm/yyyy)";
	else
		$(id).innerHTML = o.value.parseDMY().format("dd mmmm yyyy");
}

////// Validators

function continue1()
{
	var errorMsg = "";
	
	if (V("name") == "") { $("name").focus(); errorMsg += "- Name \n"; }
	if (V("email") == "") { if (errorMsg == "")$("email").focus(); errorMsg += "- Email \n"; }
	if (V("contactno") == "") { if (errorMsg == "") $("contactno").focus(); errorMsg += "- Contact Number \n"; }
	
	if (errorMsg != "") return alert("Please fill in your contact details:\n\n"+errorMsg);
	
	var c, s;
	if ((s = (c = $("email")).value.parseEmail()) == null)
		return warn(c, "The Email Address is invalid.\n\nPlease enter a valid Email Address.");
	c.value = s
	
	var checked = false;
	var i;
	var isOverseas=false;

	var arr = document.getElementsByName("movetype");
	setAspCookie("rapidquote", "store", ((arr[3].checked) ? 1 : 0));
	setAspCookie("rapidquote", "local", ((arr[0].checked || arr[1].checked) ? 1 : 0));
	
	for (i = 0; i < arr.length; i++)
		if (arr[i].checked){
			checked = true;
			if(i==4 || i==5) isOverseas = true;
		}
	if (!checked) return alert("Please select a move type.");

	checked = false;

	var serv = document.getElementsByName("service");
	for (i = 0; i < serv.length; i++) if (serv[i].checked) checked = true;

	if (!checked) return alert("Please select how can we be of service.");
	
	//errorMsg == "" at this point or wuld have exited
	//need the service & movetype tp be selected before we bother testing these
	if (serv[1].checked ) {
		if(isOverseas==true){
			var fc = document.getElementById("fcountry");
			var tc = document.getElementById("tcountry");
			if (fc.selectedIndex < 1) errorMsg += "Please select the Country you are moving From\n";
			if (tc.selectedIndex < 1) errorMsg += "Please select the Country you are moving To\n";
		}
		var nb = document.getElementById("nbrch");
		if (nb.selectedIndex < 1) errorMsg += "Please select your nearest Kent branch\n";
	}
	
	if (document.getElementById("hearfrom").selectedIndex < 1) errorMsg += "Please select where you heard about Kent\n";
	if (errorMsg != "") return alert(errorMsg);

	$("pageaction").value = "next";
	frmMain.action =  "processpage.p"; //"LandingPage2.html";
        frmMain.submit();
	
}
function cancel(){
	frmMain.action =  "http://www.kentmoving.com"; 
        frmMain.submit();
}

function back2()
{	
		$("pageaction").value = "back";
		frmMain.action =  "processpage.p"; //"LandingPage2.html";
                frmMain.submit();


/*
	var oeMsg = "", deMsg = "";
	
	if (V("faddr1") == "" && V("faddr2") == "") { $("faddr1").focus(); oeMsg += "- Address\n"; }
	if (V("fcity") == "") { if (oeMsg == "")$("fcity").focus(); oeMsg += "- City\n"; }
	if (V("fstate") == "") { if (oeMsg == "")$("fstate").focus(); oeMsg += "- State\n"; }
	if (V("fpcode") == "") { if (oeMsg == "")$("fpcode").focus(); oeMsg += "- Postcode\n"; }

	var c;
	if ((c = $("bedrooms")).selectedIndex < 1)
		oeMsg += "- Number of Bedrooms\n";
	else
		setAspCookie("rapidquote", "bedrooms", c.options[c.selectedIndex].value);
	
	if (I("fbldg") <= 0) oeMsg += "- Access Details\n";
	
	if (!is_store)
	{
		if (V("tcity") == "") { if (oeMsg == "" && deMsg == "")$("tcity").focus(); deMsg += "- City\n"; }
		if (V("tstate") == "") { if (oeMsg == "" && deMsg == "")$("tstate").focus(); deMsg += "- State\n"; }
		if (I("tbldg") <= 0) deMsg += "- Access Details\n";
	}
	else
	{
		var checked = false;
		var arr = document.getElementsByName("storagetime");
		for (var i = 0; i < arr.length; i++)
			if (arr[i].checked)
				checked = true;
		if (!checked)
			deMsg += "- Store\n";
	}

	var msg = "";
	if (oeMsg != "") msg = "Origin\n" + oeMsg;
	if (deMsg != "") msg += (msg == ""?"":"\n")+"Destination\n"+deMsg;
	
	if (msg != "") return alert("Please fill in your move details:\n\n" + msg);
	
	var date1 = V("moving_out_date").parseDMY();
	if (!date1) return alert("Please input the date you are moving out.");
	var temp = new Date();
	var today = new Date(temp.getFullYear(),temp.getMonth(),temp.getDate());
	
	if (date1 < today) return alert("The moving out date must be the present day or a future date.");
		checked = false;
	location.href = "LandingPage3.html";
	*/
}

function continue2()
{
	var oeMsg = "", deMsg = "";
	

	if (V("faddr1") == "" && V("faddr2") == "") { $("faddr1").focus(); oeMsg += "- Address\n"; }
	if (V("fcity") == "") { if (oeMsg == "")$("fcity").focus(); oeMsg += "- City\n"; }
	if (V("fstate") == "") { if (oeMsg == "")$("fstate").focus(); oeMsg += "- State\n"; }
	if (V("fpcode") == "") { if (oeMsg == "")$("fpcode").focus(); oeMsg += "- Postcode\n"; }

	var c;
	if ((c = $("bedrooms")).selectedIndex < 1)
		oeMsg += "- Number of Bedrooms\n";
	else
		setAspCookie("rapidquote", "bedrooms", c.options[c.selectedIndex].value);
	
	if (I("fbldg") <= 0) oeMsg += "- Access Details\n";
	
	if (!is_store)
	{
		if (V("tcity") == "") { if (oeMsg == "" && deMsg == "")$("tcity").focus(); deMsg += "- City\n"; }
		if (V("tstate") == "") { if (oeMsg == "" && deMsg == "")$("tstate").focus(); deMsg += "- State\n"; }
		if (I("tbldg") <= 0) deMsg += "- Access Details\n";
	}
	else
	{
		var checked = false;
		var arr = document.getElementsByName("storagetime");
		for (var i = 0; i < arr.length; i++)
			if (arr[i].checked)
				checked = true;
		if (!checked)
			deMsg += "- Store\n";
	}

	var msg = "";
	if (oeMsg != "") msg = "Origin\n" + oeMsg;
	if (deMsg != "") msg += (msg == ""?"":"\n")+"Destination\n"+deMsg;
	
	
	if (msg != "") return alert("Please fill in your move details:\n\n" + msg);
	
	var date1 = V("moving_out_date").parseDMY();
	if (!date1) msg += "Please input the date you are moving out.\n\n";					//return alert("Please input the date you are moving out.");
	var temp = new Date();
	var today = new Date(temp.getFullYear(),temp.getMonth(),temp.getDate());
	
	if (date1 < today) msg += "The moving out date must be the present day or a future date.\n\n";  //return alert("The moving out date must be the present day or a future date.");
		
	var arr = document.getElementsByName("customertype");
	if(arr[1].checked){
		if(V("wqpname") == "") msg += "Please Supply a company name.\n\n";   //return alert("Please Supply a company name");
	}
	
	if (msg != "") return alert(msg);	
		
	
	$("pageaction").value = "next";
	frmMain.action =  "processpage.p"; //"LandingPage2.html";
        frmMain.submit();

	//location.href = "LandingPage3.html";
}

function back3()
{
	$("pageaction").value = "back";
	frmMain.action =  "processpage.p"; //"LandingPage2.html";
   frmMain.submit();

}

function continue3()
{
	if (I("po") <= 0)
		return alert("Please choose your packaging option.");
	else if (is_local) {
		$("pageaction").value = "next";
		frmMain.action =  "processpage.p"; //"LandingPage2.html";
	        frmMain.submit();
		//showDialog();
	}
	else {
		$("pageaction").value = "next";
		frmMain.action =  "processpage.p"; //"LandingPage2.html";
	        frmMain.submit();
		//location.href = "LandingPage4.html";
	}
}

function submit4()
{
   $("pageaction").value = "next";
   frmMain.action =  "processpage.p"; //"LandingPage2.html";
   frmMain.submit();
    //showDialog();
}

function showDialog()
{
    var d = document;
    var div = document.createElement("div");
    var height = document.body.scrollHeight;
    var width = document.body.scrollWidth;
    if (height < document.body.clientHeight) height = document.body.clientHeight;
    if (width < document.body.clientWidth) width = document.body.clientWidth;
    
    var w = 300,h = 80;
    var left = (document.body.clientWidth-w) / 2 + document.body.scrollLeft - 150;
    var top = (document.body.clientHeight-h) / 2 + document.body.scrollTop - 150;
    d.body.appendChild(div);
    div.style.position = "absolute";
    div.style.left = "0";
    div.style.top = "0";
    div.style.width = width + "px";
    div.style.height = height + "px";
    div.style.backgroundColor = "#fff";
    div.style.filter = "alpha(Opacity = 60)";
    div.style.opacity = "0.6";
    
    var div2 = document.createElement("div");
    div2.style.position = "absolute";
    div2.style.backgroundColor = "#686e6f";
    	    
    //div2.style.width = w + "px";
    //div2.style.height = h + "px";
    div2.style.left = left + "px";
    div2.style.top = top + "px";
    div2.style.padding = "30px";
    div2.innerHTML = "<table><tr><td style = \"color:#fff;font-weight:bold;font-size:10pt;white-space:nowrap;\">"
	   +"Thank you for considering Kent for your move.<br>"
	   +"We look forward to providing you with<br>"
	   +"exceptional service.</td></tr>"
	   +"<tr><td align = \"right\"><img style = \"cursor:pointer;\" onmouseover = \"this.src = '/wrfq/images/Home_Over.gif';\" onmouseout = \"this.src = '/wrfq/images/Home.gif';\" onclick = \"location.href = 'http://www.kentmoving.com';\" src = \"/wrfq/images/Home.gif\" /></td></tr></table>";
    d.body.appendChild(div2);
}

////// Date picker

var monthNames = ["January","February","March","April","May","June","July","August","September","October","November","December"];
var dayNames = ["Monday","Wednesday","Thursday","Friday","Saturday","Sunday"];
var monthDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var datePicker, trigger;
var tempDate = [];

function prevMonth()
{

	var date = tempDate[datePicker.id]; //datePicker.value.parseDMY();
	if (date.getMonth() == 0)
	{
		date.setFullYear(date.getFullYear() - 1);
		date.setMonth(11);
	}
	else 
		date.setMonth(date.getMonth() - 1);
		
	//datePicker.value = date.format("mm/dd/yyyy");
	tempDate[datePicker.id] = date;
	trigger.onclick();
	
}

function nextMonth()
{
	var date = tempDate[datePicker.id]; //datePicker.value.parseDMY();
	if (date.getMonth() == 11)
	{
		date.setFullYear(date.getFullYear() + 1);
		date.setMonth(0);
	}
	else 
		date.setMonth(date.getMonth() + 1);
		
	//datePicker.value = date.format("mm/dd/yyyy");
	tempDate[datePicker.id] = date;
	trigger.onclick();
}

function setClass(cell, dateValue,td,currentMonth)
{
	cell.onclick = function() { pd(td.getFullYear(), td.getMonth(), td.getDate()); };
	if ((td.getFullYear() == dateValue.getFullYear()) && (td.getMonth() == dateValue.getMonth()) && (td.getDate() == dateValue.getDate()))
		{
		cell.style.border = 'solid 1px #650700';
		cell.style.color = "#650700";
		cell.style.fontWeight = "bold";
		}
   	else 
	{
		cell.onmousemove = function() { tbg = cell.style.backgroundColor;cell.style.backgroundColor = '#ccc'; }
		cell.onmouseout = function() { cell.style.backgroundColor = "#fff"; }
	}
	if ((td.getDay() == 0 || td.getDay() == 6 ) && currentMonth) 
	{
		cell.style.color = "#650700";
	}
	else if (currentMonth)
	{
		cell.style.color = "#373737";
	}
	else 
	{
		cell.style.color = "#999";
	}
}

function pd(y,m,d)
{
	datePicker.value = d.zf(2) + "/" + (m + 1).zf(2) + "/" + y.zf(4);
	datePicker.onkeyup();
	hideCalendar();
}

function calendar(picker, src)
{
    var calendar = $("calendar");
	
	datePicker = picker;
	 
	picker.onclick = function(){hideCalendar(); }
	picker.onkeydown = function(){hideCalendar(); }
	trigger = src;
	var date, dateValue;
	dateValue = picker.value.parseDMY();
	if (!dateValue)
	{
		dateValue = new Date();
		picker.value = dateValue.format("dd/mm/yyyy");
	}
	if (!tempDate[picker.id])
	{
		date = new Date(dateValue.getFullYear(), dateValue.getMonth(), dateValue.getDate());
		tempDate[picker.id] = date;
	}
	else 
		date = tempDate[picker.id];
	
	var pos = getPosition(picker);		 
	 
	var tbl = $("days");
	while (tbl.rows.length > 1)
		tbl.deleteRow(1);
	var year = date.getFullYear() ;
	 	 var month = date.getMonth();
	$("monthName").innerHTML = monthNames[month] + "  " + year;
	 	 var thisDay = date.getDate();
	if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) monthDays[1] = 29;
	var prevMonth = month - 1, nextMonth = month + 1, prevYear = year, nextYear = year;
	
	if (prevMonth < 0)  { prevMonth = 11; prevYear = year-1; }
	if (nextMonth > 11) { nextMonth = 0; nextYear = year+1; }
	
	 var nDays = monthDays[month];
	 var nDays2 = monthDays[prevMonth];
	 
   	 var firstDay = date;
   	 firstDay.setDate(1); 
	  		
   	 var startDay = firstDay.getDay()-1;
   	 if (startDay == -1) //Sunday?
	  	startDay = 6; //Sunday is the last day
	  var row = tbl.insertRow(tbl.rows.length);
	var cell;
	if (startDay != 0) //Not Monday
	{
		for (var i = startDay; i > 0; i--)
		{
			cell = row.insertCell(row.cells.length);
			cell.innerHTML = nDays2-i+1;
			setClass(cell, dateValue, new Date(prevYear, prevMonth, nDays2 - i + 1), false);
		}
	}
	var weekDay = startDay;
	for (var i = 1; i <= nDays; i++,weekDay++)
	{
		if (weekDay > 6)
		{
			weekDay = 0;
			row = tbl.insertRow(tbl.rows.length);
		}
		cell = row.insertCell(row.cells.length);
		cell.innerHTML = i;
		cell.setAttribute("class","cm");
		setClass(cell,dateValue,new Date(year,month,i),true);
	}
	if (weekDay < 6)
	{
		for (var i = weekDay+1; i <= 7; i++)
		{
			cell = row.insertCell(row.cells.length);
			cell.innerHTML = i - weekDay;
			setClass(cell,dateValue,new Date(nextYear,nextMonth,i-weekDay),false);
		}
		
	}
	calendar.style.left = pos[0] + "px";
	calendar.style.top = pos[1] + "px";
	calendar.style.display = "block";
}

function hideCalendar()
{
	$("calendar").style.display = "none";
	if (tempDate[datePicker.id])
		tempDate[datePicker.id] = null;
}

function getPosition(picker)
{
	var o = picker;
	var left = 0, top = 0;
	while (o)
	{
		left += o.offsetLeft;
		top += o.offsetTop;
		o = o.offsetParent;
	}
	return [left,top+picker.clientHeight+3];
}

////// Utilities

Number.prototype.zf = function(n)
{
	var s = String(this.valueOf());
	s = s.trim();
	for (var i = s.length; i < n; i++)
	s = "0" + s;
	return s;
}

String.prototype.trim = function() { return this.replace(/^\s+(.*?)\s+$/, "$1"); }

function $(arg)
{
	if (document.getElementById(arg)) return document.getElementById(arg);
	else if (document.forms[0][arg]) return document.forms[0][arg];
	return null;
}

function V(arg) { return $(arg).value.trim(); }
function I(arg) { return $(arg).selectedIndex; }

String.prototype.parseDMY = function()
{
	var date = new Date();
	var y = date.getFullYear();
	var m = date.getMonth() + 1;
	var d = 0;	// day of month must be specified.
	// Ignore any single instance of a day name.
	var s = this.replace(/\b(mon|monday|tue|tues|tuesday|wed|wednesday|thu|thurs|thursday|fri|friday|sat|saturday|sun|sunday)\b/i, "");
	// Single embedded alpha string must be named month. Multiple alpha strings are an error.
	
	// D-M-Y
	if (/^(\d+)\/(\d+)\/(\d+)$/.exec(s) != null)
	{
		d = RegExp.$1;
		m = RegExp.$2;
		y = RegExp.$3;
	}
	else
		return null;	// Can't parse
	// Strip leading zeros
	y = y.toString().replace(/^0+/, "");
	m = m.toString().replace(/^0+/, "");
	d = d.toString().replace(/^0+/, "");
	// Cast to integers
	if (isNaN(y = parseInt(y)))
		return null;	// Shouldn't happen
	if (isNaN(m = parseInt(m) - 1))
		return null;	// Shouldn't happen
	if (isNaN(d = parseInt(d)))
		return null;	// Shouldn't happen
	// Good ol' Y2K
	if (y < 100)
		y += (y <= 30) ? 2000 : 1900;
	date = new Date(y, m, d);
	// Defeat overflow handling of Date constructor (handles leap years).
	if (date.getFullYear() != y || date.getMonth() != m || date.getDate() != d)
	{
		//alert(d + ' ' + m + ' ' + y);
		return null;
	}
	return date;
}

Date.prototype.format = function(f)
{
    if (!this.valueOf())
	   return ' ';

    var d = this;

    return f.replace(/(yyyy|mmmm|mmm|mm|dddd|ddd|dd|hh|nn|ss|a\/p)/gi,
	   function($1)
	   {
			switch ($1.toLowerCase())
			{
			case 'yyyy': return d.getFullYear();
			case 'mmmm': return monthNames[d.getMonth()];
			case 'mmm':  return monthNames[d.getMonth()].substr(0, 3);
			case 'mm':   return (d.getMonth() + 1).zf(2);
			case 'dddd': return dayNames[d.getDay()];
			case 'ddd':  return dayNames[d.getDay()].substr(0, 3);
			case 'dd':   return d.getDate().zf(2);
			case 'hh':   return ((h = d.getHours() % 12) ? h : 12).zf(2);
			case 'nn':   return d.getMinutes().zf(2);
			case 'ss':   return d.getSeconds().zf(2);
			case 'a/p':  return d.getHours() < 12 ? 'a' : 'p';
			}
	   }
    );
}

String.prototype.parseEmail = function()
{
	if (/^\s*([\w-\.]+@([\w-]+\.)+[a-z]{2,4})\s*$/i.exec(this) != null)
		return RegExp.$1;
	else
		return null;
}

function warn(c, msg)
{
	alert(msg);
	if (c != null && typeof(c) == "object")
	{
		if (typeof(c.focus) != "undefined")
			c.focus();
		if (typeof(c.select) != "undefined")
			c.select();
	}
	return false;
}

////// Cookie handling

function getAspCookie(sCookie, sCrumb)
{
	return _manipulateAspCookie(sCookie, sCrumb);
}

function setAspCookie(sCookie, sCrumb, sNewValue, sQualifiers)
{
	return _manipulateAspCookie(sCookie, sCrumb, sNewValue, sQualifiers);
}

function escapeAspCookie(s)
{
	return escape(s).replace(/[+]/g, "%2B").replace(
		/%20/g, "+").replace(/[*]/g, "%2A").replace(
		/-/g, "%2D").replace(/[.]/g, "%2E").replace(
		/[\/]/g, "%2F").replace(/@/g, "%40").replace(/_/g, "%5F");
}	
				
function _manipulateAspCookie(sCookie, sCrumb, sNewValue, sQualifiers)
{
	var sRetVal, i, j;
	var bCookieFound = false;
	var bCrumbFound = false;
	if (sNewValue != undefined)
		sNewValue = String(sNewValue);
	var aCookies = document.cookie.split(";");
	for (i = 0; i < aCookies.length; i++)
	{
		var s = aCookies[i];
		var sCookieName = s.substring(0, s.indexOf("=")).trim();
		var aCrumbs = s.substr(s.indexOf("=") + 1).split("&");
		aCookies[i] = new Array(sCookieName, new Array());
		var bThisCookie = (unescape(sCookieName).toUpperCase() == sCookie.toUpperCase());
		if (bThisCookie)
			bCookieFound = true;
		for (j = 0; j < aCrumbs.length; j++)
		{
			s = aCrumbs[j];
			var sCrumbName = s.substring(0, s.indexOf("="));
			var bThisCrumb = (unescape(sCrumbName).toUpperCase() == sCrumb.toUpperCase());
			if (bThisCrumb)
				bCrumbFound = true;
			var sValue = s.substr(s.indexOf("=") + 1);
			if (bThisCookie && bThisCrumb)
			{
				sRetVal = sValue;
				if (sNewValue == undefined)
					return unescape(sRetVal);
				s = sCrumbName + "=" + escapeAspCookie(sNewValue);
			}
			aCookies[i][1][j] = s;
		}
		if (sNewValue != undefined && bThisCookie)
		{
			if (!bCrumbFound)
				aCookies[i][1][j] = escapeAspCookie(sCrumb) + "=" + escapeAspCookie(sNewValue);
			s = aCookies[i][0] + "=" + aCookies[i][1].join("&");
			if (typeof sQualifiers != "undefined")
			s += sQualifiers;
			document.cookie = s;
		}
	}
	if (sNewValue != undefined && !bCookieFound)
	{
		document.cookie = escapeAspCookie(sCookie) + "=" + escapeAspCookie(sCrumb) + "=" + escapeAspCookie(sNewValue);
	}
	return unescape(sRetVal);
}

////// Country data

var countryList = [
	"Select...",
	"Australia",
	"Afghanistan",
	"Albania",
	"Algeria",
	"American Samoa",
	"Andorra",
	"Angola",
	"Anguilla",
	"Antigua and Barbuda",
	"Argentina",
	"Armenia",
	"Aruba",
	"Austria",
	"Azerbaijan",
	"Bahamas",
	"Bahrain",
	"Bangladesh",
	"Barbados",
	"Belarus",
	"Belgium",
	"Belize",
	"Benin",
	"Bermuda",
	"Bhutan",
	"Bolivia",
	"Bosnia-Herzegovina",
	"Botswana",
	"Bouvet Island",
	"Brazil",
	"Brunei",
	"Bulgaria",
	"Burkina Faso",
	"Burundi",
	"Cambodia",
	"Cameroon",
	"Canada",
	"Cape Verde",
	"Cayman Islands",
	"Central African Republic",
	"Chad",
	"Chile",
	"China",
	"Christmas Island",
	"Cocos (Keeling) Islands",
	"Colombia",
	"Comoros",
	"Conch Republic",
	"Congo, Dem. Rep. (Zaire)",
	"Congo, Republic of",
	"Cook Islands",
	"Costa Rica",
	"Croatia",
	"Cuba",
	"Cyprus",
	"Czech Republic",
	"Denmark",
	"Djibouti",
	"Dominica",
	"Dominican Republic",
	"Ecuador",
	"Egypt",
	"El Salvador",
	"Equatorial Guinea",
	"Eritrea",
	"Estonia",
	"Ethiopia",
	"Falkland Islands",
	"Faroe Islands",
	"Fiji",
	"Finland",
	"France",
	"French Guiana",
	"Gabon",
	"Gambia",
	"Georgia",
	"Germany",
	"Ghana",
	"Gibraltar",
	"Greece",
	"Greenland",
	"Grenada",
	"Guadeloupe (French)",
	"Guam (USA)",
	"Guatemala",
	"Guinea",
	"Guinea Bissau",
	"Guyana",
	"Haiti",
	"Holy See",
	"Honduras",
	"Hong Kong",
	"Hungary",
	"Iceland",
	"India",
	"Indonesia",
	"Iran",
	"Iraq",
	"Ireland",
	"Israel",
	"Italy",
	"Ivory Coast (Cote D`Ivoire)",
	"Jamaica",
	"Japan",
	"Jordan",
	"Kazakhstan",
	"Kenya",
	"Kiribati",
	"Kuwait",
	"Kyrgyzstan",
	"Laos",
	"Latvia",
	"Lebanon",
	"Lesotho",
	"Liberia",
	"Libya",
	"Liechtenstein",
	"Lithuania",
	"Luxembourg",
	"Macau",
	"Macedonia",
	"Madagascar",
	"Malawi",
	"Malaysia",
	"Maldives",
	"Mali",
	"Malta",
	"Marshall Islands",
	"Martinique (French)",
	"Mauritania",
	"Mauritius",
	"Mayotte",
	"Mexico",
	"Micronesia",
	"Moldova",
	"Monaco",
	"Mongolia",
	"Montenegro",
	"Montserrat",
	"Morocco",
	"Mozambique",
	"Myanmar",
	"Namibia",
	"Nauru",
	"Nepal",
	"Netherlands",
	"Netherlands Antilles",
	"New Caledonia (French)",
	"New Zealand",
	"Nicaragua",
	"Niger",
	"Nigeria",
	"Niue",
	"Norfolk Island",
	"North Korea",
	"Northern Mariana Islands",
	"Norway",
	"Oman",
	"Pakistan",
	"Palau",
	"Panama",
	"Papua New Guinea",
	"Paraguay",
	"Peru",
	"Philippines",
	"Pitcairn Island",
	"Poland",
	"Polynesia (French)",
	"Portugal",
	"Puerto Rico",
	"Qatar",
	"Reunion",
	"Romania",
	"Russia",
	"Rwanda",
	"Saint Helena",
	"Saint Kitts and Nevis",
	"Saint Lucia",
	"Saint Pierre and Miquelon",
	"Saint Vincent and Grenadines",
	"Samoa",
	"San Marino",
	"Sao Tome and Principe",
	"Saudi Arabia",
	"Schengen countries",
	"Senegal",
	"Serbia",
	"Seychelles",
	"Sierra Leone",
	"Singapore",
	"Slovakia",
	"Slovenia",
	"Solomon Islands",
	"Somalia",
	"South Africa",
	"South Korea",
	"Spain",
	"Sri Lanka",
	"Sudan",
	"Suriname",
	"Svalbard and Jan Mayen Islands",
	"Swaziland",
	"Sweden",
	"Switzerland",
	"Syria",
	"Taiwan",
	"Tajikistan",
	"Tanzania",
	"Thailand",
	"Timor-Leste (East Timor)",
	"Togo",
	"Tokelau",
	"Tonga",
	"Trinidad and Tobago",
	"Tunisia",
	"Turkey",
	"Turkmenistan",
	"Turks and Caicos Islands",
	"Tuvalu",
	"Uganda",
	"Ukraine",
	"United Arab Emirates",
	"United Kingdom",
	"United States",
	"Uruguay",
	"Uzbekistan",
	"Vanuatu",
	"Venezuela",
	"Vietnam",
	"Virgin Islands",
	"Wallis and Futuna Islands",
	"Yemen",
	"Zambia",
	"Zimbabwe"
	];

// EOF
