function openPopUpWindow(url, features)
{
	window.open(url,"mywindow", features);
}

var cellPrefix = '<table cellpadding="2" cellspacing="0" border="1">';
cellPrefix += '<tr><td align="center"><b>Opening time</b></td><td align="center"><b>Closing time</b></td>';
cellPrefix += '<td align="center"><b>Mon</b></td><td align="center"><b>Tue</b></td>';
cellPrefix += '<td align="center"><b>Wed</b></td><td align="center"><b>Thu</b></td>';
cellPrefix += '<td align="center"><b>Fri</b></td><td align="center"><b>Sat</b></td>';
cellPrefix += '<td align="center"><b>Sun</b></td><td align="center"><b>Public Holidays</b></td>';
cellPrefix += '<td align="center"><b>Delete</b></td></tr>';
var cellSuffix_order = '<tr><td colspan="13"><input type="button" name="addtimeslot" value="Add a new timeslot" onClick="rowRedraw(0, \'order\')"/></table>';
var cellSuffix_booking = '<tr><td colspan="13"><input type="button" name="addtimeslot" value="Add a new timeslot" onClick="rowRedraw(0, \'booking\')"/></table>';

function rowRedraw(deleterow, rowtype) {
	var rowcount = 1;
	var text = '';
	
	// Go through all of the rows (delete buttons) and rewrite all of the ones that aren't being deleted.
	for (var i = 1; i<= document.getElementById('id_timeslotrowcount_' + rowtype).value; i++) {
		if ((i - 0) != (deleterow - 0)) {
			text += drawRow(rowcount,rowtype,
				document.getElementById('id_' + rowtype + '_' + i + '_open_hour').options[document.getElementById('id_' + rowtype + '_' + i + '_open_hour').selectedIndex].value,
				document.getElementById('id_' + rowtype + '_' + i + '_open_min').options[document.getElementById('id_' + rowtype + '_' + i + '_open_min').selectedIndex].value,
				document.getElementById('id_' + rowtype + '_' + i + '_close_hour').options[document.getElementById('id_' + rowtype + '_' + i + '_close_hour').selectedIndex].value,
				document.getElementById('id_' + rowtype + '_' + i + '_close_min').options[document.getElementById('id_' + rowtype + '_' + i + '_close_min').selectedIndex].value,
				document.getElementById('id_' + rowtype + '_' + i + '_mon').checked,
				document.getElementById('id_' + rowtype + '_' + i + '_tue').checked,
				document.getElementById('id_' + rowtype + '_' + i + '_wed').checked,
				document.getElementById('id_' + rowtype + '_' + i + '_thu').checked,
				document.getElementById('id_' + rowtype + '_' + i + '_fri').checked,
				document.getElementById('id_' + rowtype + '_' + i + '_sat').checked,
				document.getElementById('id_' + rowtype + '_' + i + '_sun').checked,
				document.getElementById('id_' + rowtype + '_' + i + '_pub').checked);
			rowcount++;
		}
	}
	if (deleterow == 0) {
		text += drawRow(rowcount,rowtype,8,0,20,0,1,1,1,1,1,1,1,1);
		rowcount++;
	}

	// Write to page
	writeCell(text, rowtype);
	document.getElementById('id_timeslotrowcount_' + rowtype).value = (rowcount - 1);
	return true;
}

function rowRedrawClosed(deleterow, rowtype) {
	var rowcount = 1;
	var text = '';
	
	// Go through all of the rows (delete buttons) and rewrite all of the ones that aren't being deleted.
	for (var i = 1; i<= document.getElementById('id_timeslotrowcount_' + rowtype).value; i++) {
		if ((i - 0) != (deleterow - 0)) {
			text += drawRow(rowcount,rowtype,
				document.getElementById('id_' + rowtype + '_' + i + '_open_hour').options[document.getElementById('id_' + rowtype + '_' + i + '_open_hour').selectedIndex].value,
				document.getElementById('id_' + rowtype + '_' + i + '_open_min').options[document.getElementById('id_' + rowtype + '_' + i + '_open_min').selectedIndex].value,
				document.getElementById('id_' + rowtype + '_' + i + '_close_hour').options[document.getElementById('id_' + rowtype + '_' + i + '_close_hour').selectedIndex].value,
				document.getElementById('id_' + rowtype + '_' + i + '_close_min').options[document.getElementById('id_' + rowtype + '_' + i + '_close_min').selectedIndex].value,
				document.getElementById('id_' + rowtype + '_' + i + '_mon').checked,
				document.getElementById('id_' + rowtype + '_' + i + '_tue').checked,
				document.getElementById('id_' + rowtype + '_' + i + '_wed').checked,
				document.getElementById('id_' + rowtype + '_' + i + '_thu').checked,
				document.getElementById('id_' + rowtype + '_' + i + '_fri').checked,
				document.getElementById('id_' + rowtype + '_' + i + '_sat').checked,
				document.getElementById('id_' + rowtype + '_' + i + '_sun').checked,
				document.getElementById('id_' + rowtype + '_' + i + '_pub').checked);
			rowcount++;
		}
	}
	if (deleterow == 0) {
		text += drawRow(rowcount,rowtype,8,0,20,0,1,1,1,1,1,1,1,1);
		rowcount++;
	}

	// Write to page
	writeCell(text, rowtype);
	document.getElementById('id_timeslotrowcount_' + rowtype).value = (rowcount - 1);
	return true;
}

function writeCell(text, rowtype) {
	if (rowtype == 'order') {
		var newtext = cellPrefix + text + cellSuffix_order;
	} else {
		var newtext = cellPrefix + text + cellSuffix_booking;
	}
	if (document.layers) {
		document.layers['replace_ILayer_' + rowtype].document.layers['replace_Layer_' + rowtype].document.open();
		document.layers['replace_ILayer_' + rowtype].document.layers['replace_Layer_' + rowtype].document.write(newtext);
		document.layers['replace_ILayer_' + rowtype].document.layers['replace_Layer_' + rowtype].document.close();
	} else if (document.getElementById) {
		document.getElementById('replace_Cell_' + rowtype).innerHTML = newtext;
	} else if (document.all) {
		if (rowtype == 'order') {
			document.all.replace_Cell_order.innerHTML = newtext;
		} else {
			document.all.replace_Cell_booking.innerHTML = newtext;
		}
	}
	return true;
}

function drawRow(drawrownum, rowtype, open_hour, open_min, close_hour, close_min, day_mon, day_tue, day_wed, day_thu, day_fri, day_sat, day_sun, day_pub) {
	var rowtext = '<tr><td align="center"><select name="timeslot[' + rowtype + '][' + drawrownum + '][open_hour]" id="id_' + rowtype + '_' + drawrownum + '_open_hour">';
	for (var ii = 0; ii <= 23; ii++) {
		rowtext += '<option value="' + ii + '"';
		if (open_hour == ii) rowtext += ' selected';
		if (ii < 10) {
			rowtext += '>0' + ii + '</option>';
		} else {
			rowtext += '>' + ii + '</option>';
		}
	}
	rowtext += '</select>:<select name="timeslot[' + rowtype + '][' + drawrownum + '][open_min]" id="id_' + rowtype + '_' + drawrownum + '_open_min">';
	for (var ii = 0; ii <= 45; ii+=15) {
		rowtext += '<option value="' + ii + '"';
		if (open_min == ii) rowtext += ' selected';
		if (ii < 10) {
			rowtext += '>0' + ii + '</option>';
		} else {
			rowtext += '>' + ii + '</option>';
		}
	}
	rowtext += '</select></td><td align="center"><select name="timeslot[' + rowtype + '][' + drawrownum + '][close_hour]" id="id_' + rowtype + '_' + drawrownum + '_close_hour">';
	for (var ii = 0; ii <= 23; ii++) {
		rowtext += '<option value="' + ii + '"';
		if (close_hour == ii) rowtext += ' selected';
		if (ii < 10) {
			rowtext += '>0' + ii + '</option>';
		} else {
			rowtext += '>' + ii + '</option>';
		}
	}
	rowtext += '</select>:<select name="timeslot[' + rowtype + '][' + drawrownum + '][close_min]" id="id_' + rowtype + '_' + drawrownum + '_close_min">';
	for (var ii = 0; ii <= 45; ii+=15) {
		rowtext += '<option value="' + ii + '"';
		if (close_min == ii) rowtext += ' selected';
		if (ii < 10) {
			rowtext += '>0' + ii + '</option>';
		} else {
			rowtext += '>' + ii + '</option>';
		}
	}
	rowtext += '</select></td><td align="center"><input type="checkbox" name="timeslot[' + rowtype + '][' + drawrownum + '][mon]" id="id_' + rowtype + '_' + drawrownum + '_mon" value="1"';
	if (day_mon) rowtext += ' checked';
	rowtext += '/></td><td align="center"><input type="checkbox" name="timeslot[' + rowtype + '][' + drawrownum + '][tue]" id="id_' + rowtype + '_' + drawrownum + '_tue" value="1"';
	if (day_tue) rowtext += ' checked';
	rowtext += '/></td><td align="center"><input type="checkbox" name="timeslot[' + rowtype + '][' + drawrownum + '][wed]" id="id_' + rowtype + '_' + drawrownum + '_wed" value="1"';
	if (day_wed) rowtext += ' checked';
	rowtext += '/></td><td align="center"><input type="checkbox" name="timeslot[' + rowtype + '][' + drawrownum + '][thu]" id="id_' + rowtype + '_' + drawrownum + '_thu" value="1"';
	if (day_thu) rowtext += ' checked';
	rowtext += '/></td><td align="center"><input type="checkbox" name="timeslot[' + rowtype + '][' + drawrownum + '][fri]" id="id_' + rowtype + '_' + drawrownum + '_fri" value="1"';
	if (day_fri) rowtext += ' checked';
	rowtext += '/></td><td align="center"><input type="checkbox" name="timeslot[' + rowtype + '][' + drawrownum + '][sat]" id="id_' + rowtype + '_' + drawrownum + '_sat" value="1"';
	if (day_sat) rowtext += ' checked';
	rowtext += '/></td><td align="center"><input type="checkbox" name="timeslot[' + rowtype + '][' + drawrownum + '][sun]" id="id_' + rowtype + '_' + drawrownum + '_sun" value="1"';
	if (day_sun) rowtext += ' checked';
	rowtext += '/></td><td align="center"><input type="checkbox" name="timeslot[' + rowtype + '][' + drawrownum + '][pub]" id="id_' + rowtype + '_' + drawrownum + '_pub" value="1"';
	if (day_pub) rowtext += ' checked';
	rowtext += '/></td><td align="center"><input type="button" name="timeslot[' + rowtype + '][' + drawrownum + '][delete]" value="Delete" onClick="rowRedraw(' + drawrownum + ', \'' + rowtype + '\')"/></td></tr>';
	
	return rowtext;
}

var closedtimes = new Array();
var last_row_array = new Array();
last_row_array[0] = "closedtablecontainer";

function init_closed_times_table(parent_id) {
	//Requires block element

	var closedtableHTML = '<table cellpadding="2" cellspacing="0" border="1" width="500">';
	closedtableHTML += '<tr id="closedtablecontainer"><td align="center"><b>First Date Closed</b></td><td align="center"><b>Last Date Closed</b></td>';
	closedtableHTML += '<td align="center"><b>Delete</b></td></tr></table>';
	closedtableHTML += '<table border="1" width="500"><tr><td colspan="3"><input type="button" name="addtimeslotClosed" value="Add a new closed date range" onClick="write_closed_times_row(\'\',\'\');"/></table>';

	$(parent_id).innerHTML = closedtableHTML;
	
	for (i=0;i<closedtimes.length;i++) {
		write_closed_times_row(closedtimes[i]['from'],closedtimes[i]['to']);
	}
	
}

// This function based on the onClosed() in calendar-setup.js
// Re-written to short circuit the default write to displayArea.innerHTML
// since that call triggers an *anomaly* in Safari on MacOSX
// so we simply write to the two fields we know about in this case.
// By: DavidV 2008.02.04
function closed_times_row_select(cal) {
    var p = cal.params;
    var update = (cal.dateClicked || p.electric);
    if (update && p.inputField) {
        p.inputField.value = cal.date.print(p.ifFormat);
        if (typeof p.inputField.onchange == "function")
            p.inputField.onchange();
    }
    // this is the change from original { .innerHTML to .value }
    if (update && p.displayArea)
        p.displayArea.value = cal.date.print(p.daFormat);
    if (update && typeof p.onUpdate == "function")
        p.onUpdate(cal);
    if (update && p.singleClick && cal.dateClicked)
        cal.callCloseHandler();
}
    
function write_closed_times_row(from,to) {
	
	var unique_id = "R" + Math.floor(Math.random()*10001);
	
	var closedtablerowHTML = '<tr id="' + unique_id + '"><td>';
    closedtablerowHTML += '<input type="text" id="f_displaydate_a_' + unique_id + '" size="15" class="date_closed" readonly="readonly" value="'+ from + '" />';
	closedtablerowHTML += '<img src="/images/calendar_img.gif" border="0" id="f_trigger_a_' + unique_id + '" style="cursor: pointer;" />';
    closedtablerowHTML += '<input type="hidden" name="date_closed[' + unique_id +'][from]" id="f_calcdate_a_' + unique_id + '" value="'+ from + '"  />';
	closedtablerowHTML += '</td>';
	closedtablerowHTML += '<td>';
    closedtablerowHTML += '<input type="text" id="f_displaydate_b_' + unique_id + '" size="15" class="date_closed" readonly="readonly" value="'+ to + '" />';
	closedtablerowHTML += '<img src="/images/calendar_img.gif" border="0" id="f_trigger_b_' + unique_id + '" style="cursor: pointer;" />';	
    closedtablerowHTML += '<input type="hidden" name="date_closed[' + unique_id +'][to]"" id="f_calcdate_b_' + unique_id + '" value="'+ to + '" />';
	closedtablerowHTML += '</td>';
	closedtablerowHTML += '<td><input type="button" name="deleteClosedTime" value="Delete" onclick="delete_row(\''+ unique_id + '\');" /></td>';
	closedtablerowHTML += '</tr>';

	new Insertion.After(last_row_array[last_row_array.length-1], closedtablerowHTML);
	
	last_row_id = unique_id;
	
	last_row_array[last_row_array.length] = last_row_id;
	
	Calendar.setup({
		inputField     :    'f_calcdate_a_' + unique_id,
		ifFormat       :    '%d/%m/%Y',
		displayArea    :    'f_displaydate_a_' + unique_id,
		daFormat       :    '%a, %b %e, %Y',
		button         :    'f_trigger_a_' + unique_id,
        onSelect       :    closed_times_row_select,
		firstDay       :    0,
		weekNumbers    :    false,
		singleClick    :    true
	});	

	Calendar.setup({
		inputField     :    'f_calcdate_b_' + unique_id,
		ifFormat       :    '%d/%m/%Y',
        displayArea    :    'f_displaydate_b_' + unique_id,
        daFormat       :    '%a, %b %e, %Y',
		button         :    'f_trigger_b_' + unique_id,
		onSelect       :    closed_times_row_select,
        firstDay       :    0,
        weekNumbers    :    false,
        singleClick    :    true
	});
}

function delete_row(delete_id) {
	var row = $(delete_id);
	Element.remove(row);
	
	for (i=0;i<last_row_array.length;i++) {
		if (last_row_array[i] == delete_id) {
			last_row_array.splice(i,1);
		}
	}
}

function AJAX_fetch_populate(div,file,method,params_string) {
    var objupdate = new Ajax.Updater(
        {success: div},
        file,
        {
            method: method,
            parameters: params_string,
            evalScripts: true,
            onSuccess: AJAX_success,
            onFailure: AJAX_error
        }
    );
}

function REMOTE_website_bookings(div,file,method,params_string) {
	$('loading').style.display = "block";
	AJAX_fetch_populate(div,file,method,params_string);
}

function REMOTE_website_commissions(div,file,method,params_string) {
	$('loading_com').style.display = "block";
	AJAX_fetch_populate(div,file,method,params_string);
}

function AJAX_error() {
	alert("I'm sorry there has been an error in this request. Please try again.");
}

function AJAX_success() {
	try {
		$('loading').style.display = "none";
		$('loading_com').style.display = "none";
	} catch (e) { } //Lazy...
}

function return_details(phone_number) {
	AJAX_fetch_populate("details","signup_fetch_details.php","post","suid="+phone_number);
}

function return_details_phone(country_code, area_code, phone_number) {
	AJAX_fetch_populate("details","signup_fetch_details.php","post","country="+country_code+"&area="+area_code+"&phone="+phone_number);
}