function OpenRequestedPopup(strUrl, strTarget) {
	var WindowObjectReferenceOfRequestedPopup;
	var windowWidth, windowHeight, windowLeft, windowTop;

	if (typeof window.screenX == "number"
			&& typeof window.innerWidth == "number") {
		windowWidth = window.innerWidth * .68;
		windowHeight = window.innerHeight * .68;
		windowLeft = window.screenX + window.innerWidth * .16;
		windowTop = window.screenY + window.innerHeight * .16;
	} else if (typeof window.screenTop == "number"
			&& typeof document.documentElement.offsetHeight == "number") {
		windowWidth = document.documentElement.offsetWidth * .68;
		windowHeight = document.documentElement.offsetHeight * .68;
		windowLeft = window.screenLeft + document.documentElement.offsetWidth
				* .16;
		windowTop = window.screenTop - 50;
	} else {
		windowWidth = 500;
		windowHeight = 250;
		windowLeft = 60;
		windowTop = 40;
	}
	;

	/*
	 * The above code is just to define reasonable sizes and initial positions
	 * to the popup to be.
	 */

	if (WindowObjectReferenceOfRequestedPopup == null
			|| WindowObjectReferenceOfRequestedPopup.closed) {
		WindowObjectReferenceOfRequestedPopup = window
				.open(
						strUrl,
						strTarget,
						"top="
								+ windowTop
								+ ",left="
								+ windowLeft
								+ ",width="
								+ windowWidth
								+ ",height="
								+ windowHeight
								+ ",menubar,toolbar,location,resizable,scrollbars,status");
	} else {
		WindowObjectReferenceOfRequestedPopup.focus();
	}
	;

	/*
	 * The above 9 lines of code creates the popup; if the popup is already
	 * opened, then it is only brought on top. This feature is possible only if
	 * the user allows it via the setting
	 * Edit/Preferences.../category:Advanced/Scripts & Plugins/Allow webpages
	 * to:/Raise or lower windows
	 */
}

function validateTextField(field, msg) {
	if (field.value.strip() == '') {
		alert(msg);
		field.focus();
		return false;
	}
	return true;
}

function DeleteRecord(tid) {
	if (confirm('Are you sure you want to delete ?')) {
		document.delform.DelID.value = tid;
		timer = setTimeout('document.delform.submit();', 500);
	}
}

function DeleteProcess(tid) {
	if (confirm('Are you sure you want to delete this policy ?')) {
		document.delprocess.DelProcID.value = tid;
		timer = setTimeout('document.delprocess.submit();', 500);
	}
}

function stripe(id) {
	var even = false;
	var table = document.getElementById(id);
	var off = document.getElementById('noStripe');
	if (!table || off) {
		return;
	}
	var tbodies = table.getElementsByTagName("tbody");

	for ( var h = 0; h < tbodies.length; h++) {
		var trs = tbodies[h].getElementsByTagName("tr");

		for ( var i = 0; i < trs.length; i++) {
			if (!trs[i].style.backgroundColor) {
				$(trs[i]).removeClassName(even ? 'odd' : 'even');
				$(trs[i]).addClassName(even ? 'even' : 'odd');
			}
			even = !even;
		}
	}
}

function toggleView(ServiceID) {
	var ie = (document.defaultCharset && document.getElementById && !window.home);
	var area = document.getElementById(ServiceID);
	var img = document.getElementById(ServiceID + "_Img");
	var state = document.getElementById(ServiceID + "_State");
	var display = ie ? 'block' : 'table-row';

	if (state.value == 0) {
		area.style.display = 'none';
		img.src = "/icp/img/expand.gif";
		state.value = 1;
	} else {
		area.style.display = display;
		var newimg = new Image(30, 30);
		newimg.src = "/icp/img/collapse.gif";
		img.src = newimg.src;
		state.value = 0;
	}
	repositionFooter();
}

function toggleRView(ServiceID) {
	var ie = (document.defaultCharset && document.getElementById && !window.home);
	var area = document.getElementById(ServiceID);
	var txt = document.getElementById(ServiceID + "_TXT");
	var state = document.getElementById(ServiceID + "_State");
	var display = ie ? 'block' : 'table-row';

	if (state.value == 0) {
		area.style.display = 'none';
		txt.innerHTML = "(+&nbsp;Expand)";
		state.value = 1;
	} else {
		area.style.display = display;
		txt.innerHTML = "(-&nbsp;Collapse)";
		state.value = 0;
	}

	repositionFooter();
}

function toggleYelView(ServiceID) {
	var ie = (document.defaultCharset && document.getElementById && !window.home);
	var area = document.getElementById(ServiceID);
	var img = document.getElementById(ServiceID + "_Img");
	var state = document.getElementById(ServiceID + "_State");
	var display = ie ? 'block' : 'table-row';
	var detail = document.getElementById('Details');
	if (state.value == 0) {
		area.style.display = 'none';
		img.src = "/icp/img/yel_expand.gif";
		state.value = 1;
		detail.innerHTML = 'View Details';
	} else {
		area.style.display = display;
		var newimg = new Image(30, 30);
		newimg.src = "/icp/img/yel_collapse.gif";
		img.src = newimg.src;
		state.value = 0;
		detail.innerHTML = 'Hide Details';
	}
	repositionFooter();
}

function randomPassword() {
	var length = 8;
	chars = "abcdefghijklmnopqrstuvwxyz1234567890";
	pass = "";
	while (pass.length < 8) {
		i = Math.floor(Math.random() * 62);
		pass += chars.charAt(i);
	}
	return pass

}

function generatePassword() {
	var pwd = randomPassword('8');
	document.newuser.Password.value = pwd;
	if(document.getElementById('showpwd'))
	{
		document.getElementById('showpwd').innerHTML = pwd;
	}
}

function syncAjaxRequest(url, params) {
	var request = new Ajax.Request(url, {
		method: 'post',
		postBody: params,
		asynchronous: false
	});
	if (request.transport.responseText && request.transport.responseText.indexOf("Error:") == 0) {
		alert(request.transport.responseText);
		return null;
	}
	return request.transport.responseText;
}


function AjaxRequest(location, url, post, stripe_table) {
	if (!stripe_table)
		stripe_table = 'stripe';

	new Ajax.Updater(location, url, {
		onComplete : function() {
			stripe(stripe_table);
		},
		method :'post',
		postBody :post,
		asynchronous :true,
		evalScripts :true
	});
}

function GetFilter(num, ext) {
	var rtn = "";
	if (!ext) {
		ext = "";
	}
	for ( var idx = 1; idx <= num; idx++) {
		if (document.getElementById(ext + 'filter' + idx)
				&& document.getElementById(ext + 'filter' + idx).value) {
			rtn += document.getElementById(ext + 'fsel' + idx).options[document
					.getElementById(ext + 'fsel' + idx).selectedIndex].value;
			rtn += "=" + document.getElementById(ext + 'filter' + idx).value
					+ "&";
		}
	}
	return rtn;
}

function GetSetFilter(num) {
	var rtn = "";
	for ( var idx = 1; idx <= num; idx++) {
		if (document.getElementById('filter' + idx).value) {
			rtn += document.getElementById('fsel' + idx).value;
			rtn += "=" + document.getElementById('filter' + idx).value + "&";
		}
	}
	return rtn;
}

function GetRiskVal() {
	for ( var idx = 1; idx < 4; idx++) {
		var rlevel = document.getElementById('risklevel' + idx);
		if (rlevel.checked) {
			return rlevel.value;
		}
	}
	return null;
}

function GetIncidentVal() {
	for ( var idx = 1; idx < 4; idx++) {
		var rlevel = document.getElementById('incidentlevel' + idx);
		if (rlevel.checked) {
			return rlevel.value;
		}
	}
	return null;
}

function AddResponse() {
	var count = document.getElementById('NewResponseCount');
	var span = document.getElementById('newresponse');
	span.innerHTML += "<div style='border-bottom:5px solid #FFFFFF;width:100%'><table><tr><Td valign=top >"
			+ "<textarea name='NEWR_TXT"
			+ count.value
			+ "' style='width:475px;margin-left:25px' rows=3 ></textarea></td>"
			+ "<td valign='top'>&nbsp;<select name='NEWR_VS"
			+ count.value
			+ "'  "
			+ "id='NEWR_VS"
			+ count.value
			+ "'   onChange=\"ShowComments2('','NEWR_VS"
			+ count.value
			+ "')\" > "
			+ " <option value='1'>Private</option> "
			+ " <option value='2'>Public</option></select> </td><td>&nbsp;</td></tr> "
			+ "<tr id='NEWR_VS"
			+ count.value
			+ "ShowComm' style='display:none;'><td>"
			+ "<div style='width:475px;margin-left:25px'>Comments:<br>"
			+ " <textarea name='NEWCOM"
			+ count.value
			+ "' style='width:475px' rows=2 ></textarea></div></td></tr> </table>";
	count.value = parseInt(count.value) + 1;
}

function RemoveResponse() {
	document.getElementById('newresponse').innerHTML = "<input type='hidden' name='deleteres' value='1'>";
	timer = setTimeout('document.rform.submit();', 200);
}
function GetSelectedIndustries(ind) {
	var ind = document.getElementsByName('Industry[]');
	var rtn = "";
	for ( var idx = 0; idx < ind.length; idx++) {
		rtn += "id" + ind[idx].value + "=" + ind[idx].checked + "&";
	}
	return rtn;
}

function AddUsers() {
	var users = document.getElementsByName('AddUsers[]');
	for ( var idx = 0; idx < users.length; idx++) {
		if (users[idx].checked) {
			var name = users[idx].value
					.slice(users[idx].value.indexOf("||||") + 4);
			var id = users[idx].value.substring(0, users[idx].value
					.indexOf("||||"));
			if (!document.getElementById('AU' + id)) {
				document.getElementById('userlist').innerHTML += "<span id='AU"
						+ id
						+ "'><table cellspacing=0 cellpadding=0><tr><Td valign='top'><input name=\"AddUserList[]\" value='"
						+ id + "' type=hidden>"
						+ "<input type='checkbox' name=\"AddedUsers[]\" "
						+ "value='" + id + "'></td><td>" + name
						+ "</td></tr></table></span>";
			}
		}
	}
}

function AddGroup() {
	users = document.getElementById('Group');
	if (users.value) {
		var name = users.options[document.getElementById('Group').selectedIndex].text;
		var id = users.value;
		if (!document.getElementById('GU' + id)) {
			document.getElementById('grouplist').innerHTML += "<span id='GU"
					+ id
					+ "'><table cellspacing=0 cellpadding=0><tr><Td valign='top'><input name=\"AddGroupList[]\" value='"
					+ id + "' type=hidden>"
					+ "<input type='checkbox' name=\"AddedGroups[]\" "
					+ "value='" + id + "'></td><td>" + name
					+ "</td></tr></table></span>";
		}
	}
}
function RemoveGroup() {
	var users = document.getElementsByName('AddedGroups[]');
	var removeid;
	var parentid = document.getElementById('grouplist')
	var ulength = users.length;
	for ( var idx = 0; idx < ulength; idx++) {
		if (users[idx].checked) {
			removeid = document.getElementById('GU' + users[idx].value);
			idx--;
			parentid.removeChild(removeid);
		}
	}
}

function RemoveUsers() {
	var users = document.getElementsByName('AddedUsers[]');
	var removeid;
	var parentid = document.getElementById('userlist')
	var ulength = users.length;
	for ( var idx = 0; idx < ulength; idx++) {
		if (users[idx].checked) {
			removeid = document.getElementById('AU' + users[idx].value);
			idx--;
			parentid.removeChild(removeid);
		}
	}
}

function AddSurvey() {
	var users = document.getElementsByName('AddSurvey[]');
	for ( var idx = 0; idx < users.length; idx++) {
		if (users[idx].checked) {
			var name = users[idx].value
					.slice(users[idx].value.indexOf("||||") + 4);
			var id = users[idx].value.substring(0, users[idx].value
					.indexOf("||||"));
			if (!document.getElementById('SL' + id)) {
				document.getElementById('surveylist').innerHTML += "<span id='SL"
						+ id
						+ "'><table cellspacing=0 cellpadding=0><tr><td class=vm><input name=\"AddSurveyList[]\" value='"
						+ id
						+ "' type=hidden>"
						+ "<input type='checkbox' name=\"AddedSurveys[]\" "
						+ "value='"
						+ id
						+ "'></td><td class=vm>"
						+ name
						+ "</td></tr></table></span>";
			}
		}
	}
}

function RemoveSurvey() {
	var users = document.getElementsByName('AddedSurveys[]');
	var removeid;
	var parentid = document.getElementById('surveylist')
	var ulength = users.length;
	for ( var idx = 0; idx < ulength; idx++) {
		if (users[idx].checked) {
			removeid = document.getElementById('SL' + users[idx].value);
			idx--;
			parentid.removeChild(removeid);
		}
	}
}

function AddEdu() {
	var users = document.getElementsByName('AddEdu[]');
	for ( var idx = 0; idx < users.length; idx++) {
		if (users[idx].checked) {
			var name = users[idx].value
					.slice(users[idx].value.indexOf("||||") + 4);
			var id = users[idx].value.substring(0, users[idx].value
					.indexOf("||||"));
			if (!document.getElementById('ED' + id)) {
				document.getElementById('EduList').innerHTML += "<span id='ED"
						+ id
						+ "'><table cellspacing=0 cellpadding=0><tr><Td valign='top'><input name=\"AddSurveyList[]\" value='"
						+ id + "' type=hidden>"
						+ "<input type='checkbox' name=\"AddedEdu[]\" "
						+ "value='" + id + "'></td><td>" + name
						+ "</td></tr></table></span>";
			}
		}
	}
}

function RemoveEdu() {
	var users = document.getElementsByName('AddedEdu[]');
	var removeid;
	var parentid = document.getElementById('EduList')
	var ulength = users.length;
	for ( var idx = 0; idx < ulength; idx++) {
		if (users[idx].checked) {
			removeid = document.getElementById('ED' + users[idx].value);
			idx--;
			parentid.removeChild(removeid);
		}
	}
}

function GetUsers() {
	var users = document.getElementsByName('ProcPeople[]');
	var url = "";
	ulength = users.length;
	for ( var idx = 0; idx < ulength; idx++) {
		url += "user" + idx + "=" + users[idx].value + "&";
	}
	var groups = document.getElementsByName('ProcGroups[]');
	var glength = groups.length;
	for ( var idx = 0; idx < glength; idx++) {
		url += "groups" + idx + "=" + groups[idx].value + "&";
	}

	return url;
}

function ShowComments(Start) {
	var ie = (document.defaultCharset && document.getElementById && !window.home);
	var area = document.getElementById("CommDisp");
	var state = document.getElementById("isShared");
	var display = ie ? 'block' : 'table-row';
	if (state.value == 0) {
		area.style.display = 'none';
	} else {
		area.style.display = display;
	}

}

function ShowComments2(Start, Name) {
	var ie = (document.defaultCharset && document.getElementById && !window.home);
	var area = document.getElementById(Name + "ShowComm");
	var state = document.getElementById(Name);
	var display = ie ? 'block' : 'table-row';
	if (state.value == 1) {
		area.style.display = 'none';
	} else {
		area.style.display = display;
	}

}

function SwitchRedemption(selval) {
	var eupdisp = document.getElementById("EmailUpload");
	var ecount = document.getElementById("ECount");
	var EmailComm = document.getElementById("EmailCommunication").checked;
	var MailComm = document.getElementById("MailingCommunication").checked;
	var ie = (document.defaultCharset && document.getElementById && !window.home);
	var display = ie ? 'block' : 'table-row';
	if (selval == 2 && !EmailComm && !MailComm) {
		eupdisp.style.display = "none";
		ecount.style.display = display;
	} else {
		eupdisp.style.display = display;
		ecount.style.display = "none";
	}

}

function GetReportFilter() {
	var interval = document.getElementById('Interval0').checked ? "day" : "";
	interval = document.getElementById('Interval1').checked ? "month"
			: interval;
	interval = document.getElementById('Interval2').checked ? "year" : interval;

	var rtnstr = "date1=" + document.filterval.filter4.value;
	rtnstr += "&date2=" + document.filterval.filter5.value;
	rtnstr += "&Active=" + document.filterval.Active.checked;
	rtnstr += "&IActive=" + document.filterval.IActive.checked;
	rtnstr += "&Interval=" + interval;
	rtnstr += document.filterval.Email ? "&Email="
			+ document.filterval.Email.value : "";
	return rtnstr;
}

function ReportLoading(rid) {

	AjaxRequest("report", "createreportajx.php", "rid=" + rid);
	setTimeout('CheckReport(' + rid + ')', 500);
}

function CheckReport(rid) {
	if (document.getElementById('rstatus').value == 1) {
		document.statform.submit();
		return true;
	} else {
		AjaxRequest("report", "createreportajx.php", "rid=" + rid);
		setTimeout('CheckReport(' + rid + ')', 500);
	}

}

var Spell_Win_Name;
var Work_Form_Name;
var Work_Field_Name;

function SpellCheck(form_name, field_name) {
	var textform = self.document[form_name][field_name].value;

	if (!Spell_Win_Name || Spell_Win_Name.closed
			|| (Work_Form_Name != form_name || Work_Field_Name != field_name)) {
		Spell_Win_Name = win_pop('');
		if (Spell_Win_Name.focus) {
			Spell_Win_Name.focus();
		}

		self.document.hidden_form.form_name.value = form_name;
		self.document.hidden_form.field_name.value = field_name;
		self.document.hidden_form.first_time_text.value = textform;
		self.document.hidden_form.submit();

		Work_Form_Name = form_name;
		Work_Field_Name = field_name;
	} else {
		if (Spell_Win_Name.focus) {
			Spell_Win_Name.focus();
		}
	}
}

function win_pop(URL) {
	winname = window
			.open(URL, 'WIN',
					'width=800,height=580,left=210,top=210,resizable=yes,scrollbars=yes,status=yes');
	return winname;
}

function WriteBack(form_name, field_name, corrected_text) {
	opener.document[form_name][field_name].value = corrected_text;
}

function repositionFooter() {
	var myWidth = 0, myHeight = 0;
	if (typeof (window.innerWidth) == 'number') {
		// Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if (document.documentElement
			&& (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
		// IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if (document.body
			&& (document.body.clientWidth || document.body.clientHeight)) {
		// IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}

	var mainContent = $('mainContent');
	var element = $('leftNav');
	var footer = $('footer');
	if (!element || !mainContent || !footer) return;

	if (element.style)
	{
		element.style.height = "auto";
		var offset = Position.cumulativeOffset(element);
		var height = element.offsetHeight;
		if (myHeight < mainContent.offsetHeight+200) {
			myHeight = mainContent.offsetHeight+200;
		}
		if (myHeight < element.offsetHeight+200) {
			myHeight = element.offsetHeight+200;
		}
		topHeight = 168; //top content offset before the leftNav
		footerHeight = footer.offsetHeight;
		element.style.height = (myHeight - topHeight - footerHeight)+ "px";
	}
}

function printElement(element) {
	var win=null;
	win = window.open();
	self.focus();
	win.document.open();
	win.document.write('<'+'html'+'><'+'head'+'><'+'style'+'>');
	win.document.write('body, td { font-family: Verdana; font-size: 10pt;}');
	win.document.write('<'+'/'+'style'+'><'+'/'+'head'+'><'+'body'+'>');
	win.document.write(element.innerHTML);
	win.document.write('<'+'/'+'body'+'><'+'/'+'html'+'>');
	win.document.close();
	win.print();
	win.close();
}

Event.observe(window, "load", repositionFooter);

