var cal_months = new Array();

cal_months[1] = 'Jan';
cal_months[2] = 'Feb';
cal_months[3] = 'Mar';
cal_months[4] = 'Apr';
cal_months[5] = 'May';
cal_months[6] = 'Jun';
cal_months[7] = 'Jul';
cal_months[8] = 'Aug';
cal_months[9] = 'Sep';
cal_months[10] = 'Oct';
cal_months[11] = 'Nov';
cal_months[12] = 'Dec';

function rollover(imgName) {
	doOver(imgName);
}

function rollout(imgName) {
	doOut(imgName);
}

function doOver(imgName) {
	if (document.images)
		document.images[imgName].src = eval(imgName + "on.src");
}

function doOut(imgName) {
	if (document.images)
		document.images[imgName].src = eval(imgName + "off.src");
}

var mapFlash = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="163" height="151" align="middle">'
				+'<param name="allowScriptAccess" value="sameDomain" />'
				+'<param name="movie" value="/swf/wc_home.swf" />'
				+'<param name="menu" value="false" />'
				+'<param name="quality" value="high" />'
				+'<param name="scale" value="noscale" />'
				+'<param name="bgcolor" value="#ffffff" />'
				+'<embed src="/swf/wc_home.swf" menu="false" quality="high" scale="noscale" bgcolor="#ffffff" width="163" height="151" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'
				+'</object>';

var mapAlternate = '<div><a title="View More Photos" href="/albums">'
					+'<img src="/images/gallery/thumbs/wc0001.jpg" alt="View More Photos" />'
					+'</a></div>';

var resformoff = -300;
//var resformoff = 40;
var toolmenuoff = -121;
var resformboundary = 50;
var css;
var slideOn;
var slideOff;
var busySliding = false;

function getCSS(id) {
	return document.getElementById(id).style;
}

function slideOut(which) {
	css = getCSS(which);
	hideForm();
	if (parseInt(css.top)>=(resformboundary)) {
		if (!busySliding)
			slideOff = setInterval("slide('off')",10);
	} else {
		if (!busySliding)
			slideOn = setInterval("slide('on')",10);
	}
}

function hideForm() {
	document.getElementById('miniroom').style.display = 'none';
	document.getElementById('miniarrDay').style.display = 'none';
	document.getElementById('miniarrMonth').style.display = 'none';
	document.getElementById('miniarrYear').style.display = 'none';
	document.getElementById('mininights').style.display = 'none';
	document.getElementById('miniguests').style.display = 'none';
}

function showForm() {
	document.getElementById('miniroom').style.display = 'inline';
	document.getElementById('miniarrDay').style.display = 'inline';
	document.getElementById('miniarrMonth').style.display = 'inline';
	document.getElementById('miniarrYear').style.display = 'inline';
	document.getElementById('mininights').style.display = 'inline';
	document.getElementById('miniguests').style.display = 'inline';
}

function slide(way) {
	busySliding = true;
	var topvalue = parseInt(css.top);
	
	if (way=='on') {
		if (topvalue < resformboundary) {
			topvalue += 10;
			css.top = topvalue+'px';
		} else {
			showForm();
			busySliding = false;
			clearInterval(slideOn);
		}
	} else if (way=='off') {
		if (topvalue > resformoff) {
			topvalue -= 10;
			css.top = topvalue+'px';
		} else {
			busySliding = false;
			clearInterval(slideOff);
		}
	}
}


function popDrop(html, start, stop, today) {
	for(i=start;i<(stop+1);i++) {
		if (today==i) {
			if (stop==60) {
				if ((i > 7) && (i < 15) ) {
					//html = html + '<option value="'+i+'" style="background: #FFEA00;" selected="selected">'+i+'</option>';
					html = html + '<option value="'+i+'" selected="selected">'+i+'</option>';
				} else {
					if ( (i > 14) && (i < 61) ) {
						//html = html + '<option value="'+i+'" style="background: #FF9600;" selected="selected">'+i+'</option>';
						html = html + '<option value="'+i+'" selected="selected">'+i+'</option>';
					} else {
						html = html + '<option value="'+i+'" selected="selected">'+i+'</option>';
					}
				}
			} else {
				html = html + '<option value="'+i+'" selected="selected">'+i+'</option>';
			}
		} else {
			if (stop==60) {
				if ((i > 7) && (i < 15) ) {
					//html = html + '<option value="'+i+'" style="background: #FFEA00;">'+i+'</option>';
					html = html + '<option value="'+i+'">'+i+'</option>';
				} else {
					if ( (i > 14) && (i < 61) ) {
						//html = html + '<option value="'+i+'" style="background: #FF9600;">'+i+'</option>';
						html = html + '<option value="'+i+'">'+i+'</option>';
					} else {
						html = html + '<option value="'+i+'">'+i+'</option>';
					}
				}
			} else {
				html = html + '<option value="'+i+'">'+i+'</option>';
			}
		}
	}
	return html;
}

function popDropMonth(html, thismonth) {
	for(i=1;i<cal_months.length;i++) {
		if (thismonth==i) {
			html = html + '<option value="'+i+'" selected="selected">'+cal_months[i]+'</option>';
		} else {
			html = html + '<option value="'+i+'">'+cal_months[i]+'</option>';
		}
	}
	//alert(html);
	return html;
}

function popDropYear(html, thisyear) {
	for(i=thisyear;i<(thisyear+2);i++) {
		if (thisyear==i) {
			html = html + '<option value="'+i+'" selected="selected">'+i+'</option>';
		} else {
			html = html + '<option value="'+i+'">'+i+'</option>';
		}
	}
	return html;
}

function show(active, total, imgswap, nav) {
	for(i=0;i<total;i++) {
		document.getElementById("block"+i).style.display = 'none';
	}
	document.getElementById("block"+active).style.display = 'inline';
	nav.className="selected";
	if (imgswap) {
		document.getElementById("current").src = document.getElementById("current").src.replace();
	}
}

function addBookmark(title,url) {
	if (window.sidebar) {
		window.sidebar.addPanel(title, url,"");
	} else if ( document.all ) {
		window.external.AddFavorite( url, title);
	} else if ( window.opera && window.print ) {
		return true;
	}
}

function closeit(id, off) {
	document.getElementById(id).style.top = '-800px';
}

function showit(id, top, left) {
	if ((id=='converter')&& (document.getElementById(id).style.top=='177px')) {
		closeit(id, '-220px');
	} else {
		if (top) document.getElementById(id).style.top = top+'px';
		if (left) document.getElementById(id).style.left = left+'px';
	}
}


var high_season = 0;	// [dec-jan]
var mid_season = 1;		// [sep-nov,feb-apr]
var low_season = 2;		// [may-aug]

// in the order (high,mid,low)
var rate_selfcater_single = new Array(400, 350, 260);
var rate_selfcater_sharing = new Array(300, 260, 200);
var rate_budget_communal = new Array(190, 170, 170);


function updateTotal() {
	
	var theForm = document.getElementById('miniresform');
	var spanTotal = document.getElementById('minitotal');
	var curConverter = document.getElementById('Amount');
	
	var day = eval(theForm.miniarrDay[theForm.miniarrDay.selectedIndex].value);
	var month = eval(theForm.miniarrMonth[theForm.miniarrMonth.selectedIndex].value - 1);
	var year = eval(theForm.miniarrYear[theForm.miniarrYear.selectedIndex].value);
	var num_days_arrival_month = days_in_month(year, month);
	
	var arrival_date = new Date(year,month,day);
	var room = theForm.miniroom[theForm.miniroom.selectedIndex].value;
	var season = getSeason(arrival_date.getMonth());
	var nights = eval(theForm.mininights[theForm.mininights.selectedIndex].value);
	var guests = eval(theForm.miniguests[theForm.miniguests.selectedIndex].value);
	var total = 0;

	var days_before = 0;
	var next_month = 0;
	var days_after = 0;
	
	// if its an overlap
	if ((day + nights) > num_days_arrival_month) {
		var days_before = (num_days_arrival_month - day) + 1;
		var days_after = nights - days_before;
		var current_month = month;
		//alert("current: " + current_month);
		var next_month = month == 11 ? 0 : month + 1;
		//alert("next: " + next_month);
		var season_before = getSeason(current_month);
		var season_after = getSeason(next_month);

		switch(room)
		{
			case 'selfcater' :
				var rooms_req = 0;
				var loner = guests % 2;			
				if (guests > 1) {
					guests = Math.floor(guests/2);
					rooms_req = (loner==0) ? guests : (guests + loner);
					if (loner==1) {
						rooms_req--;
						
						total_before = rooms_req * (rate_selfcater_sharing[season_before]*2) * days_before;
						total_after = rooms_req * (rate_selfcater_sharing[season_after]*2) * days_after;
						
						total_before = total_before + (loner * rate_selfcater_single[season_before] * days_before);
						total_after = total_after + (loner * rate_selfcater_single[season_after] * days_after);
						
					} else {
					
						total_before = rooms_req * (rate_selfcater_sharing[season_before]*2) * days_before;
						total_after = rooms_req * (rate_selfcater_sharing[season_after]*2) * days_after;
					}
				} else {
					rooms_req = 1;
					total_before = rooms_req * rate_selfcater_single[season_before] * days_before;
					total_after = rooms_req * rate_selfcater_single[season_after] * days_after;
				}
				
				total = total_before + total_after;
				//total = addDiscount(nights, total);
			break;
			case 'communal' :
				total_before = rate_budget_communal[season_before] * days_before * guests;
				total_after = rate_budget_communal[season_after] * days_after * guests;
				
				total = total_before + total_after;
				//total = addDiscount(nights, total);
			break;
			default:
				total = 0;
		}
		
	} else {
		switch(room)
		{
			case 'selfcater' :
				var rooms_req = 0;
				var loner = guests % 2;			
				if (guests > 1) {
					guests = Math.floor(guests/2);
					rooms_req = (loner==0) ? guests : (guests + loner);
					if (loner==1) {
						rooms_req--;
						total = rooms_req * (rate_selfcater_sharing[season]*2) * nights;
						total = total + (loner * rate_selfcater_single[season] * nights);
					} else {
						total = rooms_req * (rate_selfcater_sharing[season]*2) * nights;
					}
				} else {
					rooms_req = 1;
					total = rooms_req * rate_selfcater_single[season] * nights;
				}
				//total = addDiscount(nights, total);
			break;
			case 'communal' :
				total = rate_budget_communal[season] * nights * guests;
				//total = addDiscount(nights, total);
			break;
			default:
				total = 0;
		}
	}
	
	spanTotal.innerHTML = 'R '+total;
	if ( total > 0 ) curConverter.value = total;
}

function days_in_month(year, month) {
     return 32 - new Date(year, month, 32).getDate();
}

function addDiscount(numNights, theTotal) {
	var total_saving = '';
	var divSaving = document.getElementById('savingsblock');
	var spanSaving = document.getElementById('saving');
	var hex_10 = '#FFEA00';
	var hex_20 = '#FF9600';
	var newTotal = theTotal;
	var new_hex = '#1e1e1e';
	
	//For a stay of more than 7 days, we offer a 5% discount
	if ( ( numNights > 7 ) && ( numNights <= 14 ) ) {
		newTotal = theTotal*.95;
		total_saving = theTotal*.05;
		new_hex = hex_10;
	} else {
		//For a stay of more than 14 days, we offer a 10% discount
		if ( numNights > 14 ) {
			newTotal = theTotal*.90;
			total_saving = theTotal*.10;
			new_hex = hex_20;
		}
	}
	
	divSaving.style.color = new_hex;
	spanSaving.innerHTML = 'R '+total_saving;
	
	return newTotal;
}

// accepts date as object
function getSeason(month) {
	
	month++;
	
	if ( ( month == 12 ) || ( month == 1 ) ) {
		return high_season;
	} else {
		if ( ( ( month > 8 ) && (month < 12 ) ) || ( ( month > 1 ) && ( month < 5 ) ) ) {
			return mid_season;
		} else {
			if ( (month > 4 ) && ( month < 9 ) ) {
				return low_season;
			}
		}
	}
}

function showsub(id, total) {
	for(i=1;i<(total+1);i++) {
		document.getElementById("sub"+i).style.display = 'none';
	}
	document.getElementById("sub"+id).style.display = 'inline';
}

function getother(field, fieldname) {
	if (field[field.selectedIndex].value=='Other') {
		var subject = prompt('Enter '+fieldname+':','');
		if (subject != null) {
			field[field.selectedIndex].value = subject;
			field[field.selectedIndex].text = subject;
		}
	}
}

function validate(id) {
	
	var theForm = document.getElementById(id);
	
	switch(id)
	{
		case 'contform' :
			if (theForm.subject.value=='') {
				alert("Please select a subject");
				theForm.subject.focus();
				return false;
			}
			if (theForm.name.value=='') {
				alert("Please enter your full name");
				theForm.name.focus();
				return false;
			}
			if (!validEmail(theForm.email.value)) {
				alert("Please enter a valid email address");
				theForm.email.focus();
				return false;
			}
			if (theForm.country.value=='') {
				alert("Please select your country");
				theForm.country.focus();
				return false;
			}
			if (theForm.message.value=='') {
				alert("Please enter a message");
				theForm.message.focus();
				return false;
			}
		break;
		case 'resform' :
			if (theForm.name.value=='') {
				alert("Please enter your full name");
				theForm.name.focus();
				return false;
			}
			if (!validEmail(theForm.email.value)) {
				alert("Please enter a valid email address");
				theForm.email.focus();
				return false;
			}
			if (theForm.room.value=='') {
				alert("Please select a room");
				theForm.room.focus();
				return false;
			}
			if (!validNum(theForm.tel.value)) {
				alert("Please enter a valid number eg. +27 (21) 400-000");
				theForm.tel.focus();
				return false;
			}
			// added Dec 2006: brett@activeice.co.za
			a_day = strip_zero(theForm.arrDay.value);
			a_month = strip_zero(theForm.arrMonth.value);
			a_year = theForm.arrYear.value;
			
			d_day = strip_zero(theForm.depDay.value);
			d_month = strip_zero(theForm.depMonth.value);
			d_year = theForm.depYear.value;
			
			if (!check_dates_search(a_day,a_month,a_year,d_day,d_month,d_year)) {
				return false;
			}
		break;
		case 'miniresform' :
			if (theForm.miniroom.value=='') {
				alert("Please select a room");
				theForm.miniroom.focus();
				return false;
			} else {
				//slideOut('miniresbox');
				return true;
			}
		break;
		case 'joinform' :
			if (!validEmail(theForm.addme.value)) {
				alert("Please enter a valid email address");
				theForm.addme.focus();
				return false;
			}
		break;
		case 'faqform' :
			if (theForm.message.value=='') {
				alert("Please enter a question");
				theForm.message.focus();
				return false;
			}
		break;
		case 'gbform' :
			if (theForm.name.value=='') {
				alert("Please enter your name");
				theForm.name.focus();
				return false;
			}
			if (!validEmail(theForm.email.value)) {
				alert("Please enter a valid email address");
				theForm.email.focus();
				return false;
			}
			if (theForm.country.value=='') {
				alert("Please select your country");
				theForm.country.focus();
				return false;
			}
			if (theForm.room.value=='') {
				alert("Please select your room");
				theForm.room.focus();
				return false;
			}
			if (theForm.message.value=='') {
				alert("Please enter a message");
				theForm.message.focus();
				return false;
			}
		break;
		case 's2fform' :
			if (theForm.s2fname.value=='') {
				alert("Please enter your friends name");
				theForm.s2fname.focus();
				return false;
			}
			if (!validEmail(theForm.s2femail.value)) {
				alert("Please enter a valid email address");
				theForm.s2femail.focus();
				return false;
			}
			if (theForm.s2ffrom.value=='') {
				alert("Please enter your name");
				theForm.s2ffrom.focus();
				return false;
			}
		break;
		default : return true;
	}
	return true;
}

function validEmail(addr){
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=addr.match(emailPat)
	
	if (matchArray==null) {
		//alert("Please enter a valid email address (check @ and .'s)")
		return false
	}
	
	var user=matchArray[1]
	var domain=matchArray[2]
	
	if (user.match(userPat)==null) {
		//alert("The username doesn't seem to be valid.")
		return false
	}
	
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				//alert("Destination IP address is invalid!")
				return false
			}
		}
		return true
	}
	
	var domainArray=domain.match(domainPat)
	
	if (domainArray==null) {
		//alert("The domain name doesn't seem to be valid.")
		return false
	}
	
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || domArr[domArr.length-1].length>3) {
		//alert("The address must end in a three-letter domain, or two letter country.")
		return false
	}
	if (len<2) {
		//alert("This address is missing a hostname!")
		return false
	}
	return true;
}

function validNum(telno) {
	var pattern = "0123456789+-)( ";
	var i = 0;
	
	do {
		var pos = 0;
		for (var j=0;j<pattern.length;j++)
			if (telno.charAt(i)==pattern.charAt(j))
				pos = 1;
		i++;
	}
	while (pos==1 && i<telno.length)
	
	if (pos==0) return false;
	return true;
}

function popup(url,w,h){
	var popWin = window.open(url,'','width='+w+',height='+h+',location=no,menubar=no,scrollbars=no,toolbar=no,resizable=no');
	popWin.focus();
}


// added Dec 2006: brett@activeice.co.za
function strip_zero(str) {
	return ( str.indexOf('0') == 0 ) ? str.replace('0','') : str;
}

// added Dec 2006: brett@activeice.co.za
function y2k(year) { 
	return (year < 1000) ?year + 1900 : year; 
}
// added Dec 2006: brett@activeice.co.za
function check_dates_search(a_d,a_m,a_y,d_d,d_m,d_y) {
	a_m = a_m - 1;
	d_m = d_m - 1;
	
	var sys_date = new Date();
	var today = new Date(y2k(sys_date.getYear()),sys_date.getMonth(),sys_date.getDate());
	var arrival_date = new Date(a_y,a_m,a_d);
	var departure_date = new Date(d_y,d_m,d_d);
	
	if (arrival_date < today) {
		alert("Your arrival date is before today");
		return false;
	}
	if (departure_date < today) {
		alert("Your departure is before today");
		return false;
	}
	if (departure_date < arrival_date) {
		alert("Your departure date is before arrival date");
		return false;
	}
	if (arrival_date+'' == departure_date+'') {
		alert("Arrival and departure dates are the same");
		return false;
	}
	
	return true;
}
	
function isDate (day,month,year) {
    var today = new Date();
    year = ((!year) ? y2k(today.getYear()):year);
    month = ((!month) ? today.getMonth():month-1);
    if (!day) return false;
    var test = new Date(year,month,day);
    if ((y2k(test.getYear()) == year)&&(month == test.getMonth())&&(day == test.getDate())){
		return true;
	} else {
		return false;
	}
}

function checkDate(theForm) {
	var cal = new Array(); 
	cal[01] = "Jan"; 
	cal[02] = "Feb"; 
	cal[03] = "Mar"; 
	cal[04] = "Apr"; 
	cal[05] = "May"; 
	cal[06] = "Jun"; 
	cal[07] = "Jul"; 
	cal[08] = "Aug"; 
	cal[09] = "Sep"; 
	cal[10] = "Oct"; 
	cal[11] = "Nov"; 
	cal[12] = "Dec"; 
	
	var sysDate = new Date(); 
	
	if ( theForm.cmbMonth.value.charAt(0) == '0' ){
		var tempmonth = theForm.cmbMonth.value.charAt(1);
	} else {
		var tempmonth = theForm.cmbMonth.value;
	}
	var effDateMonth = cal[tempmonth]; 
	var effDateDay = theForm.cmbDay.value; 
	var effDateYear = theForm.cmbYear.value; 
	var effDateDate = new Date(effDateMonth+" "+ effDateDay  + ", "+effDateYear);
	var Yesterday= new Date(y2k(sysDate.getYear()),sysDate.getMonth(),sysDate.getDate());
	
	if (effDateDate < Yesterday){
		alert("You must choose a date that is in the future!");
		return false;
	}
	
	if (!isDate(effDateDay,tempmonth,effDateYear)){
		alert("You must choose a valid date!");
		return false;
	}
	
	if (theForm.cmbHotel.value=='') {
		alert("You must choose a hotel");
		return false;
	}
}