/*
---------------------------------------------------------------
Public Functions
---------------------------------------------------------------
*/
/*
Draw Dropdown & Add Button
*/
var iInstanceCount=0;

function ShowBut(name,val,rpage,align)
{
	var arrVals=val.split(";");
	var arrDates=arrVals[1].split(",");
	var i=0;
	iInstanceCount=iInstanceCount+1;
	
	document.write('<div id="ATSL">');
	document.write('<table width="100%" border="0" cellspacing="0" cellpadding="0">');
	document.write('<form name="ListForm'+iInstanceCount+'" id="ListForm'+iInstanceCount+'">');
	document.write('  <tr>');
	document.write('    <th colspan="2" scope="col" align="'+align+'">Add cruise choice(s) to your shortlist </th>');
	document.write('  </tr>');
	document.write('  <tr>');
	document.write('    <td align="'+align+'">Date:');
	document.write('		<select id="ListDates'+iInstanceCount+'" name="ListDates'+iInstanceCount+'">');
	//document.write('<option>Date</option>');
	while(i < arrDates.length)
	{
		document.write('			<option value="'+arrDates[i]+'">'+arrDates[i]+'</option>');
		i++;
	}
	document.write('		</select>');
	document.write('	</td>');
	document.write('	</tr>');
	document.write('	<tr>');
	document.write('    <td align="'+align+'">');
	document.write('		<a href="javascript:;" onclick="createCookie(\''+name+'\',\'');
	document.write(arrVals[0] +';'+document.location+'\',2,'+iInstanceCount+');alert(\'Your holiday selection has been added to your shortlist.\');" class="ATSLBut">Add To Shortlist</a>');
	document.write('	</td>');
	document.write('  </tr>');
	document.write('  <tr>');
	document.write('    <td colspan="2" align="'+align+'"><a href="'+rpage+'">Review Shortlist</a></td>');
	document.write('  </tr>');
	document.write('</form>');
	document.write('</table>');
	document.write('</div>');
}
/*
Create ShowList Layer
*/
function ShowList(name)
{
	document.write('<div id="ShowList" class="ListLayer">');
	document.write('</div>');
	document.getElementById("ShowList").innerHTML="You must add at least one cruise to your short list.";
	ReadList(name);
}
/*
---------------------------------------------------------------
Private Functions
---------------------------------------------------------------
*/
/*
Fill ShowList Layer
*/
function ReadList(name)
{
if (isNull(readCookie(name)))
{
	document.getElementById("ShowList").innerHTML="You must add at least one cruise to your short list.";
}
else
{
	var arrList=readCookie(name).split(",");
	var i=0;
	var j=0;
	var sHTML='';
	var rowNo=0;
	var arrCruise;
	var htmlPriority;
	var selectedPriority;
	sHTML = '<table border="1" cellpadding="0" cellspacing="0" width="100%"><tr><td><table class="ListTable" cellpadding="0" cellspacing="0" width="100%"><tr class="ListTitleRow"><td class="ListTitleCell">Cruises Shortlist</td><td class="ListTitleCell" align="right">Start Date</td><td class="ListTitleCell" align="right">&nbsp;Priority</td><td class="ListTitleCell" align="right">Remove</td></tr>';
	while(i < arrList.length)
	{
		htmlPriority='<select id="Priority_'+(i+1)+'" name="Priority_'+(i+1)+'">';
		//while(j < arrList.length){htmlPriority=htmlPriority+'<option>'+(j+1)+'</option>';j++;}
		while(j < arrList.length)
		{
			htmlPriority=htmlPriority+'<option'
			if (j+1==arrList.length){htmlPriority=htmlPriority+' selected';}
			htmlPriority=htmlPriority+'>'+(j+1)+'</option>';
			j++;
		}
		j=0;
		htmlPriority=htmlPriority+'</select>';
		arrCruise=arrList[i].split("#");
		if (isEven(i)){rowNo = 0;}else{rowNo = 1;}
		sHTML = sHTML + '<tr class="ListRow' + rowNo + '"><td class="ListCell' + rowNo + '"><a href="'+arrCruise[2]+'">'+arrCruise[1]+'</a></td><td align="right" class="ListCell' + rowNo + '">' + arrCruise[0] + '</td><td class="ListCell' + rowNo + '" align="right">'+htmlPriority+'</td><td class="ListCell' + rowNo + '" align="right"><input type="button" onclick="RemoveItem(\'' + name + '\',\'' + arrList[i] + '\');" value="X" class="ButRemove"><input type="hidden" value="'+arrCruise[1]+'('+arrCruise[0]+')" name="Cruise_'+(i+1)+'"></td></tr>';
		i++;
	}
	sHTML = sHTML + '</table></td></tr></table>';
	//Stamp = new Date();
	//sHTML = sHTML + '<p>'+Stamp.getHours()+':'+Stamp.getMinutes()+'</p>';
	document.getElementById("ShowList").innerHTML=sHTML;
}
}
/*
Creates Cookies
*/
function createCookie(name,value,days,iICount)
{
var dateCheck = document.getElementById("ListDates"+iICount);
if (isNull(dateCheck))
{
	var selectedDate='NONE';
}
else
{
	var selectedDate=document.getElementById("ListDates"+iICount).options[document.getElementById("ListDates"+iICount).selectedIndex].value;
}
	var NewValue;
	var checked;
	if (days)
		{
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
	if (selectedDate)
	{
		if (selectedDate=='NONE'){checked=false;}else{checked=checkCookie(name,value,iICount);}
		if (checked == false)
			{
				if (isNull(readCookie(name)) != true)
					{
					NewValue = readCookie(name) + ","
					if(value.indexOf('#')>0)
						{
							NewValue=NewValue+value
						}
					else
						{
							NewValue=NewValue+selectedDate+"#"+value.replace(";","#")
						}
					}
				else
					{
						if(value.indexOf('#')>0)
							{
								NewValue=value
							}
					else
						{
							NewValue=selectedDate+"#"+value.replace(";","#")
						}
				}
			}
		else
			{
				NewValue = readCookie(name)
			}
			document.cookie = name+"="+NewValue+expires+"; path=/";
			if (document.all['ShowList'])
				{
					ReadList(name)
				}
	}
	else
	{
	if (value=='DEAD')
		{
			document.cookie = name+"="+value+expires+"; path=/";
		}
		else
		{
			alert('Please select a date from the drop down list.');
		}
	}
}
/*
Reads Cookies
*/
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
/*
Deletes Cookies
*/
function eraseCookie(name)
{
	createCookie(name,"DEAD",-1,1);
}
/*
Makes sure there isn't an entry matching the value
*/
function checkCookie(name,value,iICount)
{
	if (isNull(readCookie(name)) == true)
	{
		return false;
	}
	var arrCookie=readCookie(name).split(",");
	var part_num=0;
	var repeatValue;
		
	while (part_num < arrCookie.length)
	{
		var selectedDate=document.getElementById("ListDates"+iICount).options[document.getElementById("ListDates"+iICount).selectedIndex].value;
		if (arrCookie[part_num] != selectedDate+"#"+value.replace(";","#") && repeatValue != true)
			{
			repeatValue = false;
			}
			else
			{
			repeatValue = true;
			}
	part_num++;
	}
	return repeatValue;
}
/*
Called when x button is clicked
*/
function RemoveItem(name,item)
{
var arrList=readCookie(name).split(",");
var newCookie='';
var i=0;
var deletedFirst=false;

eraseCookie(name);
	while(i < arrList.length)
	{
		if (arrList[i]!=item)
		{
			if (newCookie=='')
			{
				newCookie=arrList[i]
			}
			else
			{
				newCookie=newCookie+','+arrList[i]
			}
		}		
		i++;
	}
	if (newCookie.substring(newCookie.length,1)==','){newCookie=newCookie.substring(0,newCookie.length-1);}
	if (newCookie!='')
	{
		createCookie(name,newCookie,2);
	}
	ReadList(name);
}
/*
---------------------------------------------------------------
Generic Functions
---------------------------------------------------------------
*/
function isNull(val){return(val==null);}
function isEven(x) { return (x%2)?false:true; }
function isOdd(x) { return !isEven(x); }
/*
---------------------------------------------------------------
*/
