/*
---------------------------------------------------------------
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+'"><span class="red-hash" style="font-size:14px;">Add Cruise to your Shortlist</span></th>');
	document.write('  </tr>');
	document.write('  <tr>');
	document.write('    <th align="'+align+'"><span class="red-hash">Choose Date:</span>');
	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('	</th>');
	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 Cruise and Date to Shortlist</a>');
	document.write('	</td>');
	document.write('  </tr>');
	document.write('  <tr>');
	document.write('    <td colspan="2" align="'+align+'"><a href="'+rpage+'">Review Cruise Shortlist</a></td>');
	document.write('  </tr>');
	document.write('</form>');
	document.write('</table>');
	document.write('</div>');
}
function ShowButNew(name,val,rpage,align)
{
	var arrVals=val.split(";");
	var arrDates=arrVals[1].split(",");
	var i=0;
	iInstanceCount=iInstanceCount+1;
	
		//document.write('  <dl style="margin-bottom: 4px; margin-top:4px;">');
	document.write('    <dt class="purple" style="margin-top:3px;">Add Cruise to Shortlist</dt>');
		document.write('  <dd class="minibbgbc" style="margin-top:3px; margin-left:0px; padding-bottom:3px; font-size:12px; display:block;">');
	//document.write('<form name="ListForm'+iInstanceCount+'" id="ListForm'+iInstanceCount+'" style="margin-top:0px; margin-bottom:0px;">');
	document.write('		<span style="border:1px solid #999999;"><select id="ListDates'+iInstanceCount+'" name="ListDates'+iInstanceCount+'" style="font-size: 10px; margin-top: 3px; margin-bottom: 3px;" ');
	document.write('onchange="createCookie(\''+name+'\',\'');
	document.write(arrVals[0] +';'+document.location+'\',2,'+iInstanceCount+');alert(\'Your cruise selection has been added to your shortlist.\');"');
	document.write('>');
	//document.write('<option>Date</option>');
	while(i < arrDates.length)
	{
		document.write('			<option value="'+arrDates[i]+'">'+arrDates[i]+'</option>');
		i++;
	}
	document.write('		</select></span><br/>');
	document.write('    <em>Review <a href="'+rpage+'">Cruise Shortlist</a></em>');
	//document.write('</form>');
	document.write('  </dd>');
	//document.write('</dl>');
}
/*
Create ShowList Layer
*/
function ShowList(name)
{
	if (document.getElementById('ShowList') != null){return}
	document.write('<div id="ShowList" class="ListLayer">');
	document.write('</div>');
	document.getElementById("ShowList").innerHTML="<table width='580' cellpadding='2' cellspacing='1' class='CruiseTable'><tr><th class='purple L' align='left'>Cruise Shortlist</th><th class='purple' width='25%' align='left'>Start Date</th><th width='10%' align='Center' class='purple'>Priority</th><th width='11%' align='right' class='purple'>Remove</th></tr><tr><th colspan='4' align='left' class='CruiseRow'><p><em>You can add a cruise to your shortlist from the cruise details. If you are undecided either phone to discuss with our specialists or provide as much information as possible in the 'Your cruise requirements' box below.</em></p></th></tr></table>";
	ReadList(name);
}
/*
---------------------------------------------------------------
Private Functions
---------------------------------------------------------------
*/
/*
Fill ShowList Layer
*/
function ReadList(name)
{
if (isNull(readCookie(name)))
{
	document.getElementById("ShowList").innerHTML="<table width='580' cellpadding='2' cellspacing='1' class='CruiseTable'><tr><th class='purple L' align='left'>Cruise Shortlist</th><th class='purple' width='25%' align='left'>Start Date</th><th width='10%' align='Center' class='purple'>Priority</th><th width='11%' align='right' class='purple'>Remove</th></tr><tr><th colspan='4' align='left' class='CruiseRow'><p><em>You can add a cruise to your shortlist from the cruise details. If you are undecided either phone to discuss with our specialists or provide as much information as possible in the 'Your cruise requirements' box below.</em></p></th></tr></table>";
}
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 width='580' cellpadding='2' cellspacing='1' class='CruiseTable'><tr><th class='purple L' align='left'>Cruise Shortlist</th><th class='purple' width='25%' align='left'>Start Date</th><th width='10%' align='Center' class='purple'>Priority</th><th width='11%' align='right' class='purple'>Remove</th></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 = '';}else{rowNo = 'Alt';}
		sHTML = sHTML + '<tr><td class="CruiseRow' + rowNo + '"><a href="'+arrCruise[2]+'">'+arrCruise[1]+'</a></td><td align="left" class="CruiseRow' + rowNo + '">' + arrCruise[0] + '</td><td class="CruiseRow' + rowNo + '" align="right">'+htmlPriority+'</td><td class="CruiseRow' + 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(';');
	//alert(document.cookie);
	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); }
/*
---------------------------------------------------------------
*/