
var fc = false;

function form_changed() {
	fc = true;
}

/* utility functions to support AJAX in the hutton application */

function load_project_form(mode,pars,msg) {
	if (msg != undefined) {
		var r = confirm(msg);
		if (r == false) {
			return false;
		}
	}

	var t = document.getElementById('mytabber').tabber;
	var i = t.activeTab; /* which tab was clicked */
	var div = t.tabs[i].div;

	/* Fetch some html depending on which tab was clicked */
	var url = 'index.php';
	
	var gpars = {
		'mode' : mode ,
		'projectid' : t.projectid ,
		'logid' : t.logid,
		'id' : t.projectid ,
		'id2' : t.logid ,
		'prototype' : Prototype.Version
	};

	if (pars != undefined)
		var spars = Object.extend(pars, gpars);
	else
		var spars = gpars;
			
		
        /* Display a loading message */
        div.innerHTML = "<p>Loading...<img border=0 src='../images/progress_active.gif'><\/p>";

        var myAjax = new Ajax.Updater(div, url, {
			method:'post',
			parameters:spars,
			evalScripts:true
		}
	);

        return false;
}

function perform_action(action,pars,msg) {
	if (msg != undefined) {
		var r = confirm(msg);
		if (r == false) {
			return false;
		}
	}

	var t = document.getElementById('mytabber').tabber;
	var i = t.activeTab; /* which tab was clicked */
	var div = t.tabs[i].div;

	/* Fetch some html depending on which tab was clicked */
	var url = 'index.php';
	
	var gpars = {
		'action' : action ,
		'projectid' : t.projectid ,
		'logid' : t.logid,
		'id' : t.projectid ,
		'id2' : t.logid ,
		'prototype' : Prototype.Version
	};

	var spars = Object.extend(gpars, pars);

        /* Display a loading message */
        div.innerHTML = "<p>Loading...<img border=0 src='../images/progress_active.gif'><\/p>";

        var myAjax = new Ajax.Updater(div, url, {
			method:'post',
			parameters:spars,
			evalScripts:true
		}
	);

        return false;
}

function insert_project_form(fid) {
	var t = document.getElementById('mytabber').tabber;
	var i = t.activeTab; /* which tab was clicked */
	var div = t.tabs[i].div;

	/* Fetch some html depending on which tab was clicked */
	var url = 'index.php';
	var pars = $(fid).serialize(true);
	
	var gpars = {
		'projectid' : t.projectid ,
		'logid' : t.logid,
		'id' : t.projectid ,
		'id2' : t.logid ,
		'prototype' : Prototype.Version
	};

	var spars = Object.extend(pars, gpars);

	/* Display a loading message */
	div.innerHTML = "<p>Loading...<img border=0 src='../images/progress_active.gif'><\/p>";

	var myAjax = new Ajax.Updater(div, url, {method:'post',parameters:spars,evalScripts:true});

	return false;
}

function save_form_contents(fid, divid) {
	var t = document.getElementById('mytabber').tabber;
	var div = $(divid);

	/* Fetch some html depending on which tab was clicked */
	var url = 'index.php';
	var pars = $(fid).serialize(true);
	
	var gpars = {
		'projectid' : t.projectid ,
		'logid' : t.logid,
		'id' : t.projectid ,
		'id2' : t.logid ,
		'prototype' : Prototype.Version
	};

	var spars = Object.extend(pars, gpars);

	/* Display a loading message */
	div.innerHTML = "<p>Loading...<img border=0 src='../images/progress_active.gif'><\/p>";

	var myAjax = new Ajax.Updater(div, url, {method:'post',parameters:spars,evalScripts:true});

	return false;
}

/* not used at the moment */
function reload_tabber(mode,projectid,logid) {
	var pars = {
		'mode' : mode ,
		'projectid' : projectid ,
		'logid' : logid
	};
	var url = 'index.php';
	
        var myAjax = new Ajax.Request(url, {
        	method: 'post' ,
        	parameters: pars ,
        	evalScripts: true ,
        	onComplete: function(transport) {
        		$('mytabber').innerHTML = transport.responseText;
			tabberAutomatic(tabberOptions);
        	}
        });
}


/* graph functions */
function display_graph(fid) {
	try {
		var div = $('ganttimg');
	
		var url = 'index.php';
		var pars = $(fid).serialize(true);
		
        /* Display a loading message */
        div.innerHTML = "<p>Loading...<img border=0 src='../images/progress_active.gif'><\/p>";

        var myAjax = new Ajax.Updater(div, url, {
				method:'post',
				parameters:pars,
				evalScripts:true
			}
		);	
	} catch(err) {
		return false;
	}
	
	return false;
}

function print_gantt() {
	var imageurl = $('ganttimg').src;
	var url = imageurl.replace('projects_graph', 'projects_graph_print');
	window.open(url);
}

function fillstep3() {
	var url = 'index.php'; 
	var pars = {
			mode : 'projects_graph_step3',
			sort : $F('sorttype')
	};

	var myAjax = new Ajax.Request(
			url,
			{
					method: 'get',
					parameters: pars,
					onComplete: showResponse
			});
}

function showResponse(originalRequest,json) {
        // empty the select box
        $('empname').options.length = 0;
        // load the select box
		var opt = new Option('All', null);
		try {
			$('empname').add(opt, null);
		} catch(ex) {
			$('empname').add(opt);
		}
        $H(json).each( function(pair) {
            /* */
			var opt = new Option(pair.value, pair.key);
			try {
				$('empname').add(opt, null);
			} catch(ex) {
				$('empname').add(opt);
			}
        });
        if ($('empname').options.length == 0) {
                $('dempname').style.display = 'none';
        } else {
                $('dempname').style.display = 'inline';
        } 
}         

function slideshow($url) {
	window.open($url,'slideshow','width=670,height=680,resizable=yes,scrollbars=yes,status=yes');
}

function show_backtrace(id) {
	var bt = document.getElementById(id);
	if (bt.style.display == 'none') {
		bt.style.display = 'block';
	} else {
		bt.style.display = 'none';
	}
}

function reload_iframe_parent(i) {
	var t = i.parent.document.getElementById('mytabber').tabber;
	var i = t.activeTab; /* which tab was clicked */
	var div = t.tabs[i].div;

	/* fetch the id of the tab that was clicked on */			
	var mode = div.id;
	
	/* fetch the content to display */
	var url = 'index.php';
	var pars = {
		'mode' : mode ,
		'projectid' : t.projectid ,
		'logid' : t.logid,
		'id' : t.projectid ,
		'id2' : t.logid,
		'prototype' : Prototype.Version
	}; 

	var myAjax = new Ajax.Updater(div, url, 
		{
			method:'get',
			parameters:pars,
			evalScripts:true
		});
}

function milestone_pick() {
	var t = document.getElementById('mytabber').tabber;
	var i = t.activeTab; /* which tab was clicked */
	var div = $('msvalue');

	/* Fetch some html depending on which tab was clicked */
	var url = 'index.php';
	
	var pars = {
		'mode' : 'projects_dailylog_milestones_getvalue',
		'projectid' : t.projectid ,
		'logid' : t.logid,
		'id' : t.projectid ,
		'id2' : t.logid ,
		'msid' : $F('msid') ,
		'prototype' : Prototype.Version
	};

	var myAjax = new Ajax.Request(
			url,
			{
					method: 'post',
					parameters: pars,
					onComplete: showDates
			});	
}

function showDates(originalRequest,json) {
	if (json.msedate == undefined) {
		$('msedate').value = '';
	} else {
		$('msedate').value = json.msedate;
	}
	if (json.msadate == undefined) {
		$('msadate').value = '';
	} else {
		$('msadate').value = json.msadate;
	}
	$('msedate').disabled = json.disabled;
	$('msadate').disabled = json.disabled;
	$('msbutton').disabled = json.disabled;
	$('bmsedate').disabled = json.disabled;
	$('bmsadate').disabled = json.disabled;
	if (json.disabled) {
		$('msbutton').style.display = "none";
		$('bmsedate').style.display = "none";
		$('bmsadate').style.display = "none";
	} else {
		$('msbutton').style.display = "inline";
		$('bmsedate').style.display = "inline";
		$('bmsadate').style.display = "inline";
	}
	$('msg').innerHTML = "";
}

function expand_textarea(clicked, textarea) {
	var control = document.getElementById(textarea);
	if (control.rows == 3) {
		control.rows = 10;
		clicked.src = "../images/minus.gif";
	} else {
		control.rows = 3;
		clicked.src = "../images/plus.gif";
	}
}

var htr;
var ori;
function set_grid_background(tr) {
	/* remove the color from the old row */
	var grid1 = document.getElementById('grid1');
	var grid2 = document.getElementById('grid2');
	var row1 = grid1.rows[ori];
	var row2 = grid2.rows[ori];

	try {
		$(row1).removeClassName('shaded');
	} catch (err) {
		/* must not have been set continue */
	}
	try {
		$(row2).removeClassName('shaded');
	} catch (err) {
		/* must not have been set continue */
	}
	
	/* store the row we are changing color on */
	ri = tr.rowIndex;
	ori = ri;
	
	var row1 = grid1.rows[ri];
	var row2 = grid2.rows[ri];
	
	htr = tr;
	
	/* now change it's color */
	$(row1).addClassName('shaded');
	$(row2).addClassName('shaded');
	
}

function resize_grid() {
	var t = document.getElementById('mytabber').tabber;
	var i = t.activeTab; /* which tab was clicked */
	var div = t.tabs[i].div;
	
	var width = $(div).getWidth();
	$('turnover2').style.width = Math.round(($(div).getWidth() - $('turnover1').getWidth()) * 0.95);
	var t = width;
}

// this function gets the cookie, if it exists
function Get_Cookie( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
	{
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	// if the expires variable is set, make the correct expires time, the
	// current script below will set it for x number of days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
	var expires_date = new Date( today.getTime() + (expires) );
	//alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function checkall() {
	var cb = $$('input.cbset');
	for(var i=0; i<cb.length; i++) {
		if ($('cbcheckall').checked == true) {
			cb[i].checked = true;
		} else {
			cb[i].checked = false;
		}
	}		
}

function print_singletab() {
}

function format_date(control) {
	var txt = control.value;
	if (txt.length == 0) {
		return false;
	}
	
	var time = valid_date(control.value);
	
	var d = new Date();
	if (isNaN(time)) {
		alert("Invalid date format. Please provide date as mm/dd/yyyy or mm/dd/yy.");
		control.select();
		control.focus();
		return false;
	} else {
		d.setTime(time);
	}
	
	var today = new Date();
	var controlname = control.name;
	if (controlname.match("msadate") && d > today) {
		alert("Invalid date format. Actual dates cannot be future dates.");
		control.select();
		control.focus();
		return false;
	}
	
	control.value = (d.getMonth() + 1) + '/' + d.getDate() + '/' + (d.getFullYear() < 2000 ? d.getFullYear() + 100 : d.getFullYear());
	form_changed();
}

function valid_date(dvalue) {
	var str = dvalue;
	str = str.replace(/\-/g, "/");
	return Date.parse(str);
}

function checkall_dates(fid) {
	var frm = fid;
	for (var i=0;i<frm.length;i++) {
		var str = frm.elements[i].name;
		if (str.match("msedate") || str.match("msadate")) {
			if (frm.elements[i].value) {
				var time = valid_date(frm.elements[i].value);
				if (!time) {
					alert("You are trying to submit an invalid date. Please provide date as mm/dd/yyyy or mm/dd/yy.");
					frm.elements[i].select();
					frm.elements[i].focus();
					return false;
				}
				if (str.match("msadate")) {
					var d = new Date;
					d.setTime(time);
					var today = new Date();
					if (d > today) {
						alert("You are trying to submit an invalid date. Actual dates cannot be future dates.");
						frm.elements[i].select();
						frm.elements[i].focus();
						return false;
					}
				}
			}
		}
	}
	
	insert_project_form(frm);
	return false;
}

function customcode_selected(code, custom_value) {
	var code_value = code.value;
	if (code_value == custom_value) {
		// show the hidden custom value box
		$('customcode').enable();
		$('customcode').activate();
	} else {
		// show the hidden custom value box
		$('customcode').disable();
		$('customcode').value = "";
	}
}
