var xmltemplate = 'pbttemplates.xml' ;
function FCKeditor_OnComplete( editorInstance )
{
	editorInstance.Events.AttachEvent( 'OnBlur'	, FCKeditor_OnBlur ) ;
	editorInstance.Events.AttachEvent( 'OnFocus', FCKeditor_OnFocus ) ;
}

function FCKeditor_OnBlur( editorInstance )
{
	editorInstance.ToolbarSet.Collapse() ;
}

function FCKeditor_OnFocus( editorInstance )
{
	editorInstance.ToolbarSet.Expand() ;
}

function show_html(editorInstance)
{
	//var h = editorInstance.GetXHTML('xhtml');
	//var h = editorInstance ;
	var h  = getEditorValue( editorInstance )
	//alert (h);
	return h;
}
function getEditorValue( instanceName )
{
  // Get the editor instance that we want to interact with.
  var oEditor = FCKeditorAPI.GetInstance( instanceName ) ;

  // Get the editor contents as XHTML.
  return oEditor.GetXHTML( true ) ;  // "true" means you want it formatted.
}

function setEditorValue( instanceName, text )
{
  // Get the editor instance that we want to interact with.
  var oEditor = FCKeditorAPI.GetInstance( instanceName ) ;

  // Set the editor contents.
  oEditor.SetHTML( text ) ;
}
function on_submit(var_do_next)
{
	//alert("do_next "+var_do_next);
	document.getElementById('do_next').value = var_do_next ;
	try
	{
		if ((document.getElementById('userid').value.length < 3 || !document.getElementById('userid').value.length)
				&& document.getElementById('userid').type != 'hidden')
		{
			alert("You must supply a user ID");
			document.getElementById('userid').focus();
			return;
		}
	} catch(e) {};
	try
	{
		if ((document.getElementById('userpw').value.length < 3 || !document.getElementById('userid').value.length)
				&& document.getElementById('userpw').type != 'hidden')
		{
			alert("You must supply a user password");
			document.getElementById('userpw').focus();
			return;
		}
	} catch(e) {};
	try
	{
		if ((document.getElementById('userpw').value.length < 3 || document.getElementById('userid').value.length < 3) ||
				(!document.getElementById('userpw').value.length || !document.getElementById('userid').value.length ))
		{
			alert("You must supply a user password");
			document.getElementById('userpw').focus();
			return;
		}
	} catch(e) {};
	try
	{
		if (document.getElementById('dptext').value -0 < 1 )
		{
			var dary = new Array();
			var d = document.getElementById('meetingdate').value;
			dary = d.split("/");//NaN
			if ((dary[0]*1 >12 || dary[0]*1 < 1) ||
			(dary[1]*1 >31 || dary[1]*1 < 1) ||
			(dary[2]*1 >2012 || dary[2]*1 < 2005) ||
			isNaN(dary[0]*1) || isNaN(dary[2]*1) || isNaN(dary[2]*1)
			)
			{
				alert ("Please enter the date in MM/DD/YYYY format");
				return false ;
			}
		}
	} catch(e) {};
	try {
		if (document.getElementById('meetinglabel')) {
			// loop through options = set selected
	//		alert(document.getElementById('meetinglabel').value);
			for (i=0; i <= document.adminform.meetinglabel.options.length - 1;i++)
			{
	//			alert(document.adminform.meetinglabel.options[i].text);
			}
		} else {
			//alert('no value');
			}
	} catch (e) {};
	try {
		document.getElementById('xsubmit').value = "Please Wait" ;
	} catch(e) {};
	try {
		document.getElementById('xlsubmit').value = "Please Wait" ;
	} catch(e) {};
	var base_name = 'FCKeditor' ;
	var i = 0 ;
	var temp_name = base_name+i ;
	var temp_name_xhtml = base_name+i+ 'xhtml';
	while (i < 12)
	{
		try {
			document.getElementById(temp_name_xhtml).value = specialchar_cnv( getEditorValue( temp_name ) );
		} catch(e) {};
		i++ ;
		temp_name = base_name+i ;
		temp_name_xhtml = base_name+i+ 'xhtml';

	}
	document.adminform.submit() ;
}

function testsubmit() {
	document.adminform.action="/cgi-bin/testoutputs.pl";
	document.adminform.submit() ;
}

function go_client(id)
{
	document.getElementById('active_client').value = id;
	document.getElementById('do_next').value = 'show_client' ;
	on_submit('show_client');
}

function go_manage_video(sort)
{
	//alert ('sort '+sort);
	try {
		document.getElementById('video_sort_by').value = sort;
		if (document.getElementById('video_sort_by_last').value == sort)
		{
			if (document.getElementById('asc_desc').value == 'a')
			{
				document.getElementById('asc_desc').value = 'd' ;
			} else {
				document.getElementById('asc_desc').value = 'a'	;
			}
		}
		document.getElementById('video_sort_by_last').value = sort ;
	} catch (e) {}
	document.getElementById('active_client').value = '000';
	document.getElementById('do_next').value = 'manage_video' ;
	on_submit('manage_video');
}

function go_proof()
{
	document.getElementById('do_next').value = 'proof' ;
	on_submit('proof');
}

function go_publish()
{
	document.getElementById('do_next').value = 'publish' ;
	on_submit('publish');
}

function specialchar_cnv(ch)
{
	var reg1 = new RegExp ('&', 'g') ;
	var reg2 = new RegExp ('"', 'g') ;
	var reg3 = new RegExp ("'", 'g') ;
	var reg4 = new RegExp ('<', 'g') ;
	var reg5 = new RegExp ('>', 'g') ;
	ch = ch.replace(reg1,'&amp;');
	ch = ch.replace(reg2,'&quot;');
	ch = ch.replace(reg3,'&#39;');
	ch = ch.replace(reg4,"&lt;");
	ch = ch.replace(reg5,"&gt;");
	return ch;
}

function settime(tid)
{
	var thedate = new Date();
	var thissec = thedate.getSeconds() ;
	var totaltime = thissec ;
	var thismin = thedate.getMinutes() ;
	totaltime += thismin*60 ;
	var thishour = thedate.getHours() ;
	totaltime += thishour*60*60 ;
	var txtid = 'timestamp'+tid;
	document.getElementById(txtid).value = calc_hms(totaltime) ;
	//add_sub(0,0);
	display_et(totaltime,tid) ;
}

function add_1(tid)
{
	add_sub(tid,1)
}

function sub_1(tid)
{
	add_sub(tid,-1) ;
}

function et_onchange(tid)
{
	var txtid_et = 'timestamp_et_'+tid;
	var tt_et = calc_totaltime(txtid_et) ;
	var basetime = calc_totaltime('timestamp0') ;
	var new_clock_time = basetime + tt_et ;
	var txtid = 'timestamp'+tid;
	try {
		document.getElementById(txtid).value = calc_hms(new_clock_time);
	} catch (e) {}

}

function format_hms(thms)
{
	if (thms<10){ thms = '0'+thms;}
	return thms;
}

function calc_hms(t)
{
	var thishr = parseInt(t/(60*60)) ;
	t -= thishr*60*60 ;
	var thismin = parseInt(t/60) ;
	t -= thismin*60 ;
	var thissec = parseInt(t) ;
	thishr = format_hms(thishr);
	thismin = format_hms(thismin);
	thissec = format_hms(thissec);
	var ts = 	thishr+':'+thismin+':'+thissec ;
	if (thishr == 'NaN' || thismin == 'NaN' || thissec == 'NaN')
	{
		ts = '' ;
	}
	return (ts);
}

function calc_totaltime(ebid)
{
	try {
		var e = document.getElementById(ebid).value
		var b_ary = e.split(":") ;
		var b = parseInt(b_ary[0]*60*60) + parseInt(b_ary[1]*60) + parseInt(b_ary[2]) ;
		b = parseInt(b);
	} catch (e) {
		b = '';
	}
	return b ;
}

function add_sub(tid,as)
{
	var txtid = 'timestamp'+tid;
	var tt = calc_totaltime(txtid);
	tt += parseInt(as);
	//var totaltime = tt ;
	var ts_tt = calc_hms(tt);
	document.getElementById(txtid).value = ts_tt ;
	display_et(tt,tid) ;//was totaltime
	if (tid == 0)
	{
		var tt0 = tt ;
		var k = 1 ;
		var txtid = 'timestamp'+k;
		var txtidet = 'timestamp_et_'+k;
		var txttsid = 'ts'+k;
		var tmpname = document.getElementById(txttsid).name ;
		while (tmpname.length)
		//while (k<3)
		{
			var tt = calc_totaltime(txtid);
			var ttet = calc_totaltime(txtidet);
			if (tt > 0 )
			{
				var ts_tt = calc_hms(tt);
				document.getElementById(txtid).value = ts_tt ;
				display_et(tt,k) ;
			}
			// new 03/11
			else if (ttet > 0)
			{
				var t0et = tt0 + ttet ;
				var ts_tt = calc_hms(t0et);
				document.getElementById(txtid).value = ts_tt ;
			}
			// end new
			k++ ;
			txtid = 'timestamp'+k;
			txttsid = 'ts'+k;
			try
			{
				tmpname = document.getElementById(txttsid).name ;
			}
			catch (e)
			{
				return;
			}
		}
	}
}

function display_et(ttime,tid)
{
	var basetime = calc_totaltime('timestamp0');
	var thiset = ttime - basetime ;
	var ts_et = calc_hms(thiset);
	var txt_et_id = 'timestamp_et_'+tid;
	document.getElementById(txt_et_id).value = ts_et ;
	return ;
}


function go_client(id)
{
	document.adminform.active_client.value = id ;
	document.adminform.do_next.value = 'show_client' ;
	//alert("id "+id);
	document.adminform.action = "/cgi-bin/pbtadmin.pl";
	//document.adminform.action = "http://dev.tclinuxsite.com/cgi-bin/testoutputs.pl";
	document.adminform.submit();
}

function save_client(id)
{
	document.adminform.active_client.value = id ;
	document.adminform.do_next.value = 'save_client' ;
	document.adminform.action = "/cgi-bin/pbtadmin.pl";
	document.adminform.submit();
}

function save_users(thisform,userdctl)
{
	var i = 0 ;
	var tmpform = new Object();
	for(i=0;i<document.forms.length;i++)
	{
		if (document.forms[i].id == thisform)
		{
			tmpform = document.forms[i] ;
		}
	}
	tmpform.do_next.value = 'save_user' ;
	if ( tmpform.usr_login.value == 'default' || tmpform.usr_password.value == 'changeme' )
	{
		alert("Please change the User ID and Password to new values");
		return false ;
	}
	tmpform.action = "/cgi-bin/pbtadmin.pl";
	//tmpform.action = "http://dev.tclinuxsite.com/cgi-bin/testoutputs.pl";
	tmpform.submit();
}

function go_manage_users(id)
{
	document.adminform.active_client.value = id ;
	document.adminform.do_next.value = 'manage_users' ;
	document.adminform.action = "/cgi-bin/pbtadmin.pl";
	document.adminform.submit();
}

function add_user(id)
{
	document.adminform.active_client.value = id ;
	document.adminform.do_next.value = 'add_user' ;
	document.adminform.action = "/cgi-bin/pbtadmin.pl";
	document.adminform.submit();
}

function add_client()
{
	document.adminform.do_next.value = 'add_client' ;
	document.adminform.action = "/cgi-bin/pbtadmin.pl";
	document.adminform.submit();
}


function go_create_agenda(id)
{
	//alert("new agenda for "+id);
	document.adminform.active_client.value = id ;
	document.adminform.do_next.value = 'make_agenda' ;
	document.adminform.action = "/cgi-bin/pbtadmin.pl";
	//document.adminform.action = "http://dev.tclinuxsite.com/cgi-bin/testoutputs.pl";
	document.adminform.submit();
}

function go_edit_agenda(id)
{
	//alert("edit agendas for "+id);
	document.adminform.active_client.value = id ;
	document.adminform.do_next.value = 'agendamgr' ;
	document.adminform.action = "/cgi-bin/pbtadmin.pl";
	//document.adminform.action = "http://dev.tclinuxsite.com/cgi-bin/testoutputs.pl";
	document.adminform.submit();
}

function go_edit_timestamps(id)
{
	//alert("edit agendas for "+id);
	document.adminform.active_client.value = id ;
	document.adminform.do_next.value = 'edit_timestamps' ;
	document.adminform.action = "/cgi-bin/pbtadmin.pl";
	//document.adminform.action = "http://dev.tclinuxsite.com/cgi-bin/testoutputs.pl";
	document.adminform.submit();
}

function show_hide_dtl(t)
{
	//alert(t);
	//return;
	var showplus = 0;
	if (document.getElementById(t).style.display=="none" ) {
		document.getElementById(t).style.display="block" ;
		showplus = 1;
	} else {
		document.getElementById(t).style.display="none" ;
	}
	try {
		//1063_videofiles_edit
		var reg1 = new RegExp ('videofiles_edit', 'g') ;
		var tinner = t.replace(reg1,'plusminus');
		if (showplus == 1) {
			document.getElementById(tinner).innerHTML = '[-]';
		} else {
			document.getElementById(tinner).innerHTML = '[+]';
		}

	} catch (e) {}
}


function show_dtl(t)
{
	if (document.getElementById(t).style.display=="none" ) {
		document.getElementById(t).style.display="block" ;
	} else {
		document.getElementById(t).style.display="none" ;
	}
}

function set_active_agenda()
{
	//alert("need to save this");
	document.adminform.do_next.value = 'save_file_active' ;
	document.adminform.action = "/cgi-bin/pbtadmin.pl";
	//document.adminform.action = "http://dev.tclinuxsite.com/cgi-bin/testoutputs.pl";
	document.adminform.submit();
}

function check_date(mydate)
{
	var a = mydate.value ;
	if (a=='' || a==null){return;}
	var newdate = a ;
	var reg1 = new RegExp ("[^0-9]", 'g') ;
	var has_delims = reg1.exec(a) ;
	if (has_delims)
	{
		var newdate = a.replace(reg1,'/');
		ary_newdate = new Array();
		ary_newdate = newdate.split("/");
		var mym = ary_newdate[0] - 0 ;
		var myd = ary_newdate[1] - 0 ;
		var myy = ary_newdate[2] - 0 ;
		if (mym >1900)
		{
			myy = ary_newdate[0] - 0 ;
			mym = ary_newdate[1] - 0 ;
			myd = ary_newdate[2] - 0 ;
		}
	}
	else
	{
		if (newdate.length == 6 || newdate.length == 8)
		{
			var myy = newdate.substring(4,8) - 0;
			var mym = newdate.substring(0,2) - 0 ;
			var myd =newdate.substring(2,4) - 0 ;
		}
	}
	if (myy < 10 && myy < 1000) {myy += 2000 ;}
	if (myy > 10 && myy < 1000) {myy += 1900 ;}
	if (mym < 10) {mym = '0'+mym;}
	if (myd < 10) {myd = '0'+myd;}
	var f_newdate = mym+'/'+myd+'/'+myy ;
	if( f_newdate && isdate(f_newdate) )
	{
		mydate.value = f_newdate ;
	}
	else
	{
		mydate.focus();
		alert("Invalid date. Enter date using format MM/DD/YYYY");
		//mydate.value = "" ;
	}
	return;
}

function isdate(str)
{
  // dd-mm-yyyy format; leading zeros required
  if(str.length != 10) { return false;}
  strmonth = str.substring(0, 2);
  strday =  str.substring(3, 5);
  stryear = str.substring(6, 10);
	//alert('309 '+strmonth+'/'+strday+'/'+stryear);
	var reg31 = new RegExp ("01|03|05|07|08|10|12") ;
	var reg30 = new RegExp ("04|06|09|11") ;
  if( isNaN(strday) || (strday < 0) || isNaN(stryear) || (stryear < 0)) {return false;}
  if( reg31.exec(strmonth) ) { monthdays = 31 ;}
  else if( reg30.exec(strmonth) ) { monthdays = 30 ;}
  else if(strmonth == "02") { monthdays = ((stryear % 4) == 0) ? 29 : 28;}
  else { return false; }
  if(strday > monthdays) { return false; }
	// make sure date is <= today - do not want to do that in pbt - meetings can be in the future
	/*var thedate = new Date();
	var nyear=thedate.getFullYear();
	var nmonth=thedate.getMonth()+1;
	var nday=thedate.getDate();
	if ( ( strday-0 > nday && strmonth-0 >= nmonth && stryear == nyear) ||
				(strmonth-0 >= nmonth && stryear == nyear) ||
				(stryear > nyear) ) {return false;} */
  return true;
}

function save_comments(dtl_comments_id,cpname)
{
	//alert(dtl_comments_id);
	//<input type="hidden" name="save_comments_dctl" id="save_comments_dctl" value="" />
	//<input type="hidden" name="save_agenda_comments" id="" value="N" />
	var new_comment = document.getElementById(dtl_comments_id).value ;
	if (new_comment != '') {
		document.getElementById('save_agenda_comments').value = 'Y';
		document.getElementById('save_comments_dctl').value = dtl_comments_id;
		document.adminform.do_next.value = 'save_agenda_comments' ;
		document.getElementById('hold_cp_open').value = cpname;
		document.adminform.action = "/cgi-bin/pbtadmin.pl";
		//document.adminform.action = "http://dev.tclinuxsite.com/cgi-bin/testoutputs.pl";
		document.adminform.submit();
	}

}

function save_meeting_label(dtl_meeting_label_id,cpname)
{
	var new_meeting_label = document.getElementById(dtl_meeting_label_id).value ;
	if (new_meeting_label != '') {
		document.getElementById('save_pbtvideo_meeting_label').value = new_meeting_label;
		var sstr = dtl_meeting_label_id.split("-");
		document.getElementById('save_pbtvideo_dctl').value = sstr[1];
		document.adminform.do_next.value = 'save_meeting_label' ;
		document.getElementById('hold_cp_open').value = cpname;
		document.adminform.action = "/cgi-bin/pbtadmin.pl";
		document.adminform.submit();
	}

}

function save_videostatus(dtl_videostatus_id,cpname,dtl_agendastatus_id)
{
	var new_videostatus = document.getElementById(dtl_videostatus_id).value ;
	var new_agendastatus = document.getElementById(dtl_agendastatus_id).value ;
	document.getElementById('save_pbtvideo_videostatus').value = new_videostatus;
	var sstr = dtl_videostatus_id.split("-");
	document.getElementById('save_pbtvideo_dctl').value = sstr[1];
	var asstr = dtl_agendastatus_id.split("-");
	document.getElementById('save_agenda_mgr1_dctl').value = asstr[1];
	document.getElementById('save_agenda_mgr1_agenda_status').value = new_agendastatus;
	document.getElementById('hold_cp_open').value = cpname;
	document.adminform.do_next.value = 'save_videostatus' ;
	document.adminform.action = "/cgi-bin/pbtadmin.pl";
	document.adminform.submit();
}

function editagenda(ag_dcontrolno_key,cpname) {
	document.getElementById('hold_cp_open').value = cpname;
	document.getElementById('current_agedit_file_dctl').value = ag_dcontrolno_key;
	document.adminform.do_next.value = 'make_agenda' ;
	document.adminform.submit();
}

function viewagenda(ag_dcontrolno_key,cpname) {
	document.getElementById('hold_cp_open').value = cpname;
	document.getElementById('current_agedit_file_dctl').value = ag_dcontrolno_key;
	document.adminform.do_next.value = 'proof' ;
	document.adminform.submit();
}

function publishagenda(ag_dcontrolno_key,cpname) {
	document.getElementById('hold_cp_open').value = cpname;
	document.getElementById('current_agedit_file_dctl').value = ag_dcontrolno_key;
	document.getElementById('do_next').value = 'publishpage' ;
	document.adminform.submit();
}

function selectfile(ctl,cpname) {
	document.getElementById('hold_cp_open').value = cpname;
	 document.getElementById('controlno').value = ctl;
	 document.getElementById('do_next').value = 'show_timestamps' ;
	 //document.adminform.action = "/cgi-bin/testoutputs.pl";
	 document.adminform.action = "/cgi-bin/pbtadmin.pl";
	 document.adminform.submit();
}

function go_edit_ir(id,cpname) {
	//document.getElementById('hold_cp_open').value = cpname;
	document.adminform.active_client.value = id ;
	document.adminform.from_inforeq.value = 'Y';
	document.adminform.do_next.value = 'showirmanager' ;
	document.adminform.action = "/cgi-bin/pbtadmin.pl";
	document.adminform.submit();
}

