var G_inlinelogin='loginblock';	//the id of the div to toggle when logged out (in SC, this is the login box). This can be overridden elsewhere.

var G_ListActive=true;
var G_AddMode=false;
var G_CurrRec='';
var G_MaxRec='';
var G_Table=new Array();
var G_Routeplans=new Array();
var G_Mailboxes=new Array();
var G_RPselect="";
var G_MBselect="";

var bg_def='';
var bg_over='#BBBBFF';
var bg_sel='#FFC000';
var bg_selover='#E3D599';

var selbox_fieldlist=new Array('name','inuse_yn');

function LoginCallback(userstring,errmessage) {
	if(errmessage) { userstring='';}
	HdrLoginFlip(userstring.length>0,userstring);
	DPVisible(false);
	if(userstring) {
		document.getElementById("loggedininfo").innerHTML="<p>You are now logged in to the Skycom management area.</p>";
		PreloadSequence(0);
// 		BuildRoutePlanArray(G_Routeplans,"","RouteplansLoaded");
		document.getElementById(G_inlinelogin).className="hideblock";
		document.getElementById("mainblock").className="showblock";
	} else {
		document.getElementById(G_inlinelogin).className="showblock";
		document.getElementById("mainblock").className="hideblock";
		G_CurrRec=0;
		G_Table=new Array();
		G_Routeplans=new Array();
		G_RPselect="";
		G_ListActive=true;
		G_AddMode=false;
		EmptyList();
	}
}
function LoggedIn(username) {DPVisible(false); G_LoginDisplayed=0; LoginLogout(username?0:1);}	//may be called this way too if user clicked on login/logout button at top of page.

function PreloadSequence(pldseq) {
	pldseq++;
	switch(pldseq) {
		case 1: //routeplans
			BuildRoutePlanArray(G_Routeplans,"",'PreloadSequence('+pldseq+')','PreloadSequence('+pldseq+')');
			break;
		case 2: //mailboxes
			BuildMailboxArray(G_Mailboxes,'PreloadSequence('+pldseq+')','PreloadSequence('+pldseq+')');
			break;
		case 3: //dayparting (this must be the last one!)
			BuildDayPartingArray(G_Table,"items",'PreloadSequence('+pldseq+')','PreloadSequence('+pldseq+')');
			break;
		default://insert new features in sequence above here.
			BuildSelectBoxes();	//generate some select box code for the routeplans, etc.
			BuildTable();		//all done with the setup, so display the loaded day partings in the selection list on the page.
			break;
	}
}

function BuildSelectBoxes() {
	//Load the routeplans select list....
	if(document.getElementById('temp_info_msg')) {document.getElementById('temp_info_msg').innerHTML+="...";}
	G_RPselect="";
	if(G_Routeplans.length==0) {
		G_RPselect+="<option value='0'>-- no route plans --</option>";
	} else {
		G_RPselect+="<option value='0'>-- please select --</option>";
	}
	for(var rnum in G_Routeplans) {
		G_RPselect+="<option value='"+G_Routeplans[rnum].id+"'>"+G_Routeplans[rnum].name+"</option>";
	}

	//Load the mailbox select list....
	if(document.getElementById('temp_info_msg')) {document.getElementById('temp_info_msg').innerHTML+="...";}
	G_MBselect="";
	if(G_Mailboxes.length==0) {
		G_MBselect+="<option value='0'>-- no mailboxes --</option>";
	} else {
		G_MBselect+="<option value='0'>-- please select --</option>";
	}
	for(var mnum in G_Mailboxes) {
		G_MBselect+="<option value='"+G_Mailboxes[mnum].id+"'>"+G_Mailboxes[mnum].name+"</option>";
	}
	if(document.getElementById('temp_info_msg')) {document.getElementById('temp_info_msg').innerHTML+="...";}
}

function PressAddButton() {
	//Clear the form, ready for the user to start typing a new route plan.
	SelectRow(0);
	G_AddMode=true;
	ResetToDefault();
	document.getElementById("dp_name").value="New Day Parting";
	DPVisible(true);
	document.getElementById("dp_name").select();
}

function PressDelButton() {
	//User has asked to delete the current record, so confirm it's okay, and delete it.
	if(!G_CurrRec) {if(G_AddMode){G_AddMode=false; DPVisible(false);} return;}
	if(G_Table[G_CurrRec]['inuse']) {
		alert(MSIE_MsgBox("Day Parting \""+G_Table[G_CurrRec]['name']+"\" is in use by one or more of your numbers, and cannot be deleted"));
		return;
	}

	var okay=confirm(MSIE_MsgBox("Are you sure you want to delete day parting \""+G_Table[G_CurrRec]['name']+"\"?"));
	if(okay) {
		var saveobj=new Object;
		saveobj["dp_id"]=G_dpid;
		saveobj["func"]=("delete");
		apilite_post('dp_includes/dp_save.php',saveobj, 'DPDelRec_okay', saveobj , 'DPDelRec_fail', saveobj, true);
	}
	G_AddMode=false;
}

function DPDelRec_okay(responsestring,args) {
	var rownum=document.getElementById('dtr_'+args.dp_id).rowIndex;
	document.getElementById('datatable').tBodies[0].deleteRow(rownum);	//delete the table row.
	delete G_Table[args.dp_id];						//delete the array element.
	G_CurrRec='';								//clear the current record pointer.
	DPVisible(false);
	alert("Thank you. Your day parting has been deleted.");
}

function DPDelRec_fail(responsestring,args) {
	alert("Error deleting record.\n"+responsestring);
}

function ClearForm() {
	//nothing to do here for day parting.
}

function PopulateForm() {
	//Display the record currently selected in the list onto the form fields.
	if(!G_CurrRec) { return; }
	LoadDPData(G_CurrRec);
}
