/**
 * Planning component functions
 */

var strAjaxDoPath = '/dios/ajaxDo'; // the full path to ajaxDo (this will be component-specific)
var userPlans1 = ''; // to store first part of html response (plans)
var userPlans2 = ''; // to store second part of html response (plans)
var userReports1 = ''; // to store first part of html response (reports)
var userReports2 = ''; // to store second part of html response (reports)

function copyPlan() {
	document.getElementById('createNewVersion').style.display='none';
	document.getElementById('loading-image').style.display='block';
//	alert('Now creating new version.');
	diosGo('home', 'startNewVersion', '1');
	this.href='javascript:void(0)';
}

function createPlan() {
	document.getElementById('createFirstPlan').style.display='none';
	document.getElementById('loading-image').style.display='block';
//	alert('Now creating plan.');
	diosGo('home', 'createNewPlan', '1');
	this.href='javascript:void(0)';
}

function menuCollapseAll() {
	/**
	 * Collapse all menu items in tree
	 */

	var arrMenuTreeIcons = '';

	arrMenuTreeIcons = getElementsByClassName(document, 'img', 'menu-tree-icon');

	for (var i = 0; i < arrMenuTreeIcons.length; i++) { // iterate through array of menu tree icons
		strId = arrMenuTreeIcons[i].id;
		if (strId.substring((strId.length - 6), strId.length) == 'Button') { // found a button
			// make object button is assigned to invisible
			strObjId = strId.substring(0, (strId.length - 6)); // store the object name part of the string
			if (document.getElementById(strObjId)) { // make sure object exists in document
				obj = document.getElementById(strObjId);
				if (obj.style.display != 'none') { // make invisible
					$(obj).slideUp('fast');
					//obj.style.display = 'none';
					objButton = document.getElementById(strId); // load button image element
					objButton.src = '/dios/images/expandDown.gif';
				}
			}
		}
	}
}

function menuExpandAll() {
	/**
	 * Expand all menu items in tree
	 */
	var arrMenuTreeIcons = '';
	arrMenuTreeIcons = getElementsByClassName(document, 'img', 'menu-tree-icon');
	for (var i = 0; i < arrMenuTreeIcons.length; i++) { // iterate through array of menu tree icons
		strId = arrMenuTreeIcons[i].id;
		if (strId.substring((strId.length - 6), strId.length) == 'Button') { // found a button
			// make object button is assigned to visible
			strObjId = strId.substring(0, (strId.length - 6)); // store the object name part of the string
			if (document.getElementById(strObjId)) { // make sure object exists in document
				obj = document.getElementById(strObjId);
				if (obj.style.display != 'block') { // make visible
					$(obj).slideDown('fast');
					//obj.style.display = 'block';
					objButton = document.getElementById(strId); // load button image element
					objButton.src = '/dios/images/expandUp.gif';
				}
			}
		}
	}
}

function loadReportPage(tabId, curPage, curVal) {
	/**
	 * Load a report page with an ajax call
	 */

	// show loading indicator (menu)
	getObj('msg-loading-menu');
	style.display = 'block';

	// show loading indicator (report)
	getObj('msg-loading-report');
	style.display = 'block';

	sendRequest(strAjaxDoPath + '.php?tabId=' + tabId + '&action=loadReportPage&curPage=' + curPage + '&curVal=' + curVal, loadReportPage_Callback);

	menuRefresh(tabId);
}

function loadReportPage_Callback(req) {
	/**
	 * Handle callback data from the AJAX call
	 */

	s = req.responseText;
	/*s = s.replace(/\/dios\/ajaxDo/g, "/dios/report0607");*/
	s = s.replace(/strAjaxDoPath/g, '/dios/report0607');

	getObj('report'); // load report element

	obj.innerHTML = s; // replace contents of report element
}

function menuRefresh(tabId, anon) {
	/**
	 * Refresh the menu with an ajax call
	 */

	getObj('msg-loading-menu'); // menu loading message
	obj.style.display = 'block'; // show loading message
	if (anon) { // tell ajax script to start DIOS component in anonymous mode
		sendRequest(strAjaxDoPath + '.php?tabId=' + tabId + '&anonMode=1&action=menuRefreshAll', menuRefresh_Callback);
	} else { // don't start DIOS component in anonymous mode
		sendRequest(strAjaxDoPath + '.php?tabId=' + tabId + '&action=menuRefreshAll', menuRefresh_Callback);
	}
}

function menuRefresh_Callback(req) {
	/**
	 * Handle callback data from the AJAX call
	 */

	getObj('menu'); // load menu element
	obj.innerHTML = req.responseText; // replace contents of menu element
}

function menuLoad(tabId, anon) {
	/**
	 * Load the menu with an ajax call
	 */

	getObj('msg-loading-menu'); // menu loading message
	obj.style.display = 'block'; // show loading message
	if (anon) { // anonymous user
		sendRequest(strAjaxDoPath + '.php?anonMode=1&tabId=' + tabId + '&action=menuRefresh', menuLoad_Callback);
	} else { // require auth
		sendRequest(strAjaxDoPath + '.php?tabId=' + tabId + '&action=menuRefresh', menuLoad_Callback);
	}
}

function menuLoad_Callback(req) {
	/**
	 * Handle callback data from the AJAX call
	 */

	getObj('menu'); // load menu element
	obj.innerHTML = req.responseText; // replace contents of menu element

	try	{ // having trouble detecting this function's existence - kludge follows
		showStatusInline(); // load status bar
	}
		catch(e) {
	}
}

function loadUserPR(userId) {
	/**
	 * Load user plan/report inclusion, etc. with an ajax call
	 * (returns formatted html)
	 */
	sendRequest('/dios/ajaxGetUserPlans.php?userId=' + userId, loadUserPlans_Callback); // get v1 plans html
	sendRequest('/dios/ajaxGetUserPlans2.php?userId=' + userId, loadUserPlans2_Callback); // get v2 plans html
	sendRequest('/dios/ajaxGetUserReports.php?userId=' + userId, loadUserReports_Callback); // get v1 report html
	sendRequest('/dios/ajaxGetUserReports2.php?userId=' + userId, loadUserReports2_Callback); // get v2 reports html
}

function loadUserPlans_Callback(req) {
	/**
	 * Handle callback data from the AJAX call
	 */

	//getObj('userPR'); // load element
	userPlans1 = req.responseText; // store result in variable
	checkUserPRData(); // check whether all data has been returned, update innerHTML if it has
//	if (userPlans2 != '') { // received both parts of the result
//		getObj('userPlans'); // load element
//		obj.innerHTML = (userPlans1 + userPlans2); // assign both parts of the result to element
//	}
}

function loadUserPlans2_Callback(req) {
	/**
	 * Handle callback data from the AJAX call
	 */

	//getObj('userPR'); // load element
	userPlans2 = req.responseText; // store result in variable
	checkUserPRData(); // check whether all data has been returned, update innerHTML if it has
//	if (userPlans1 != '') { // received both parts of the result
//		getObj('userPlans'); // load element
//		obj.innerHTML = (userPlans1 + userPlans2); // assign both parts of the result to element
//	}
}

function loadUserReports_Callback(req) {
	/**
	 * Handle callback data from the AJAX call
	 */

	//getObj('userPR'); // load element
	userReports1 = req.responseText; // store result in variable
	checkUserPRData(); // check whether all data has been returned, update innerHTML if it has
//	if (userReports2 != '') { // received both parts of the result
//		getObj('userReports'); // load element
//		obj.innerHTML = (userReports1 + userReports2); // assign both parts of the result to element
//	}
}

function loadUserReports2_Callback(req) {
	/**
	 * Handle callback data from the AJAX call
	 */

	//getObj('userPR'); // load element
	userReports2 = req.responseText; // store result in variable
	checkUserPRData(); // check whether all data has been returned, update innerHTML if it has
//	if (userReports1 != '') { // received both parts of the result
//		getObj('userReports'); // load element
//		obj.innerHTML = (userReports1 + userReports2); // assign both parts of the result to element
//	}
}

function checkUserPRData() {
	// function simply checks whether all user PR data
	// has been returned, and updates html element innerHTML
	// with the data when it is
//alert('userPlans1: ' + userPlans1 + '\nuserPlans2: ' + userPlans2 + '\nuserReports1: ' + userReports1 + '\nuserReports2: ' + userReports2); // debug
	if (userPlans1 && userPlans2 && userReports1 && userReports2) { // all data returned, update innerHTML
		getObj('userPR'); // load element
		obj.innerHTML = (userPlans1 + userPlans2 + userReports1 + userReports2); // assign all data to element
	}
}

function planShowDialog(planId) {
	/**
	 * Display plan dialog
	 */

	if (planId == undefined) { // no plan id specified
		/*getObj('progress-box');
		obj.style.display = 'block';*/
		//$('#progress-box').show('fast');
		$('#progress-box').show('fast');
	} else { // plan id specified
		/*getObj('progress-box' . $planId);
		obj.style.display = 'block';*/
		$('#progress-box').show('fast');
	}

	// now that it has been clicked, load the contents of the progress box
	showProgressInline();

	return;
}

function planShowStatus(tabId, noCache, planId) {
	/**
	 * Retrieve plan status via ajax call
	 */

	strRequest = strAjaxDoPath + '.php?tabId=' + tabId + '&action=getPlanStatus';

	if (noCache == 1) strRequest += ('&nocache=1'); // refresh cache
	if (planId != undefined) strRequest += ('&planid=' + planId); // status bar Id supplied

	if (planId == undefined) { // no plan Id supplied
		sendRequest(strRequest, planShowStatus_Callback, null, planId);
	} else { // status bar Id supplied
		sendRequestM(strRequest, planShowStatusM_Callback, null, planId); // use different callback function when plan id given
	}

	return;
}

function planShowStatus_Callback(req) {
	/**
	 * Handle callback data from the AJAX call
	 */

	s = req.responseText;
	getObj('plan-status'); // load plan element

	obj.innerHTML = s; // replace contents of plan element
}

function planShowStatusM_Callback(req, planId) {
	/**
	 * Handle callback data from the AJAX call
	 */
	s = req.responseText;

	getObj('plan-status' + planId); // load plan element

	obj.innerHTML = s; // replace contents of plan element
}

function planShowProgress(tabId) {
	/**
	 * Retrieve plan progress via ajax call
	 */
	sendRequest(strAjaxDoPath + '.php?tabId=' + tabId + '&action=getPlanProgress', planShowProgress_Callback);

	return;
}

function planShowProgress_Callback(req) {
	/**
	 * Handle callback data from the AJAX call
	 */

	s = req.responseText;

	getObj('progress-box');

	obj.innerHTML = s; // replace contents of plan element
}

function sendRequestM(url,callback,postData,planId) {
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	req.onreadystatechange = function () {
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) {
//			alert('HTTP error ' + req.status);
			return;
		}
		callback(req, planId);
	}
	if (req.readyState == 4) return;
	req.send(postData);
}

function planIconClick(n) {
	/**
	 * Select plan
	 */

	var objPlan = document.getElementById('planIcon' + n);
	var objText = document.getElementById('txt' + n);
	var objTextSmall = document.getElementById('small' + n);
	var objTextSelected = document.getElementById('selected' + n);
	var objTextPlan = document.getElementById('text-plan' + n);
	var objImage = document.getElementById('planImg' + n);
	var objPlanDetails = document.getElementById('planDetails' + n);
	var objPlanOptions = document.getElementById('planOptions' + n);

	// hilight row
	if (objPlan) { // in case objPlan does not exist
		objPlan.style.backgroundColor = '#b7d9f5';
	}

	// show extra plan text
	objText.style.display = 'block';

	// hide 'click for details' text
	objTextSmall.style.display = 'none';

	// show 'selected' text
	objTextSelected.style.display = 'block';

	// adjust 'plan' text
	objTextPlan.style.fontSize = '1.2em';
	objTextPlan.style.fontWeight = 'bold';

	// enlarge image
	objImage.style.backgroundImage = 'url(/dios/images/plan_bg.gif)';
	objImage.style.height = '144px';
	objImage.style.paddingLeft = '14px';

	// show extra plan details (comments)
	//objPlanDetails.style.display = 'block';
	$('planDetails' + n).show();

	// show plan options
	if (objPlanOptions) { // in case plan options are not rendered
		objPlanOptions.style.display = 'block';
	}

	/*
	 * Hide all other plan icons
	 */

	var arrPlan = 			getElementsByClassName(document, 'div', 'plan-icon-container');
	var arrText = 			getElementsByClassName(document, 'p', 'planIconLargeText');
	var arrTextSmall = 		getElementsByClassName(document, 'small', 'text-small');
	var arrTextSelected = 	getElementsByClassName(document, 'h4', 'text-selected');
	var arrTextPlan = 		getElementsByClassName(document, 'h3', 'text-plan');
	var arrImageCur = 		getElementsByClassName(document, 'div', 'plan-img-current');
	var arrImageArc = 		getElementsByClassName(document, 'div', 'plan-img-archived');
	var arrImageSub = 		getElementsByClassName(document, 'div', 'plan-img-submitted');
	var arrImageRej = 		getElementsByClassName(document, 'div', 'plan-img-rejected');
	var arrImageApp = 		getElementsByClassName(document, 'div', 'plan-img-approved');
	var arrPlanDetails = 	getElementsByClassName(document, 'table', 'plan-details-collapsed-footer');
	var arrPlanOptions = 	getElementsByClassName(document, 'table', 'plan-options-collapsed-footer');

	// un-hilight row
	for (var i = 0; i < arrPlan.length; i++) { // iterate through array of plan icon container objects and hide them
		if (arrPlan[i].id != objPlan.id) { // only if not selected
			arrPlan[i].style.backgroundColor = '#dddddd';
		}
	}

	// hide extra plan text
	for (var i = 0; i < arrText.length; i++) { // iterate through array of plan text objects and hide them
		if (arrText[i].id != objText.id) { // only if not selected
			arrText[i].style.display = 'none';
		}
	}

	// show 'click for details' text
	for (var i = 0; i < arrTextSmall.length; i++) {
		if (arrTextSmall[i].id != objTextSmall.id) { // only if not selected
			arrTextSmall[i].style.display = 'inline';
		}
	}

	// hide 'selected' text
	for (var i = 0; i < arrTextSelected.length; i++) {
		if (arrTextSelected[i].id != objTextSelected.id) { // only if not selected
			arrTextSelected[i].style.display = 'none';
		}
	}

	// adjust 'plan' text
	for (var i = 0; i < arrTextPlan.length; i++) {
		if (arrTextPlan[i].id != objTextPlan.id) { // only if not selected
			arrTextPlan[i].style.fontSize = '.8em';
			arrTextPlan[i].style.fontWeight = 'normal';
		}
	}

	// shrink images
	for (var i = 0; i < arrImageCur.length; i++) {
		if (arrImageCur[i].id != objImage.id) { // only if not selected
			arrImageCur[i].style.backgroundImage = 'url(/dios/images/plan_bg_small.gif)';
			arrImageCur[i].style.height = '72px';
			arrImageCur[i].style.paddingLeft = '7px';
		}
	}

	for (var i = 0; i < arrImageArc.length; i++) {
		if (arrImageArc[i].id != objImage.id) { // only if not selected
			arrImageArc[i].style.backgroundImage = 'url(/dios/images/plan_bg_small.gif)';
			arrImageArc[i].style.height = '72px';
			arrImageArc[i].style.paddingLeft = '7px';
		}
	}

	for (var i = 0; i < arrImageSub.length; i++) {
		if (arrImageSub[i].id != objImage.id) { // only if not selected
			arrImageSub[i].style.backgroundImage = 'url(/dios/images/plan_bg_small.gif)';
			arrImageSub[i].style.height = '72px';
			arrImageSub[i].style.paddingLeft = '7px';
		}
	}

	for (var i = 0; i < arrImageRej.length; i++) {
		if (arrImageRej[i].id != objImage.id) { // only if not selected
			arrImageRej[i].style.backgroundImage = 'url(/dios/images/plan_bg_small.gif)';
			arrImageRej[i].style.height = '72px';
			arrImageRej[i].style.paddingLeft = '7px';
		}
	}

	for (var i = 0; i < arrImageApp.length; i++) {
		if (arrImageApp[i].id != objImage.id) { // only if not selected
			arrImageApp[i].style.backgroundImage = 'url(/dios/images/plan_bg_small.gif)';
			arrImageApp[i].style.height = '72px';
			arrImageApp[i].style.paddingLeft = '7px';
		}
	}

	// hide extra plan details (comments)
	for (var i = 0; i < arrPlanDetails.length; i++) {
		if (arrPlanDetails[i].id != objPlanDetails.id) { // only if not selected
			arrPlanDetails[i].style.display = 'none';
		}
	}

	// hide plan options
	for (var i = 0; i < arrPlanOptions.length; i++) {
		if (arrPlanOptions[i].id != objPlanOptions.id) { // only if not selected
			arrPlanOptions[i].style.display = 'none';
		}
	}
}

function planIconOver(n, intVersion) {
	/*
	 * Hilight the plan row
	 */
	var objPlan = document.getElementById('planIcon' + n);

	// hilight row - only if not already the selected plan icon
	if (objPlan.style.backgroundColor != '#b7d9f5' && objPlan.style.backgroundColor != 'rgb(183, 217, 245)') { // not the selected plan icon - hilight (w/ffox test)
		objPlan.style.backgroundColor = '#ffc600';
	}
}

function planIconOut(n) {
	/*
	 * Un-hilight the plan row
	 */

	var objPlan = document.getElementById('planIcon' + n);

	// un-hilight row - only if not the selected plan icon
	if (objPlan.style.backgroundColor != '#b7d9f5' && objPlan.style.backgroundColor != 'rgb(183, 217, 245)') { // not the selected plan icon - un-hilight (w/ffox test)
		objPlan.style.backgroundColor = '#dddddd';
	}
}

function planIconDown(n) {
	/*
	 * Begin 'click' effect
	 */

	var objPlan = document.getElementById('planIcon' + n);

	// hilight row - only if not already the selected plan icon
	if (objPlan.style.backgroundColor != '#b7d9f5' && objPlan.style.backgroundColor != 'rgb(183, 217, 245)') { // not the selected plan icon - hilight (w/ffox test)
		objPlan.style.backgroundColor = '#fff600';
	}
}

/**
 * Some javascript functions for the compare plans feature
 */

function enableDisableSubmitButton() {
	// Enable or disable the compare plan form submit button
	// according to the selected value under the plan drop-down
	// menu (ie. a plan must be selected

	getObj('planSelect'); // set obj to drop-down list

	if (obj.value > 0) { // plan selected - enable submit button
		getObj('btnCompare'); // set obj to submit button
		obj.disabled = false;
	} else { // plan not selected - disable the submit button
		getObj('btnCompare'); // set obj to submit button
		obj.disabled = true;
	}
}

/**
 * To display word limit warning
 */
function exceededWordLimit() {
	alert('One or more fields have exceeded word limit!\n\nPlease refer to highlighted fields.');
}

