﻿var mywindow = null;

document.onclick = showpop;
document.ondblclick = showpop;

function addFocusEvent(func) {
	var oldonfocus = window.onfocus;
	if (typeof window.onfocus != 'function') {
		window.onfocus = func;
	}
	else {
		window.onfocus = function () {
			oldonfocus();
			func();
		}
	}
}
addFocusEvent
(
	function () {
		if (mywindow) {
			try {
				if (mywindow.closed) {
					PopBlock.style.height = 1;
					PopBlock.style.width = 1;
					window.status = '';
				}
			}
			catch (ex) {
				PopBlock.style.height = 1;
				PopBlock.style.width = 1;
				window.status = '';
			}

			// TODO: validate specific controll only
			//if (Page_ClientValidate != 'undefined'
			//	&& typeof(Page_ClientValidate) == 'function')
			//{
			//	Page_ClientValidate();
			//}
		}
	}
);

function FixNull(obj) {
	if (obj == null) {
		obj = "";
	}
	return obj;
}

function showpop() {
	if (window.focus && mywindow && !mywindow.closed) {
		mywindow.focus()
	}
}

function ShowPopUp(URL, Name, width, height, scrollBars) {
	if (!scrollBars) {
		scrollBars = 'no';
	}
	else {
		scrollBars = 'yes';
	}

	PopBlock.style.height = document.body.clientHeight;
	PopBlock.style.width = document.body.clientWidth;
	window.status = 'Please select ' + Name;

	var w = width;
	var h = height;

	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;

	var settings = 'height=' + h + ',';
	settings += 'width=' + w + ',';
	settings += 'top=' + wint + ',';
	settings += 'left=' + winl + ',';
	settings += 'scrollbars=' + scrollBars + ',';
	settings += 'resizable=no';

	// open popup	
	mywindow = open('', Name, settings);
	URL = urlAddQueryStringParameter(URL, 'xyz', Math.random());
	mywindow.location.href = URL;
	if (mywindow.opener == null) mywindow.opener = self;
}

function pickMunicipality(
	allowNonLocal,
	showNational,
	userFiltered,
	downwardsOnly,
	delegateOnly,
	filterMunicipalityID) {
	var queryString = "";

	if (allowNonLocal) {
		queryString = "?allowNonLocal=1";
	}
	if (showNational) {
		queryString += (queryString == "" ? "?" : "&");
		queryString += "showNational=1";
	}
	if (userFiltered) {
		queryString += (queryString == "" ? "?" : "&");
		queryString += "userFiltered=1";
	}
	if (downwardsOnly) {
		queryString += (queryString == "" ? "?" : "&");
		queryString += "downwardsOnly=1";
	}
	if (delegateOnly) {
		queryString += (queryString == "" ? "?" : "&");
		queryString += "delegateOnly=1";
	}
	if (filterMunicipalityID) {
		queryString += (queryString == "" ? "?" : "&");
		queryString += "filterMunicipalityID=" + filterMunicipalityID;
	}
	ShowPopUp('../Popups/Municipalities.aspx' + queryString, 'Municipality', 450, 450, 'yes');
}

function pickMunicipalitySearch(
	includeForPermanentDelegateRoles,
	includeForDelegateRoles,
	includeForEvaluatorRoles,
	municipalityID) {
	var url = '../Popups/MunicipalitySearch.aspx';
	if (includeForPermanentDelegateRoles) {
		url = addQueryStringParameter(url, 'IncludeForPermanentDelegateRoles=1');
	}
	if (includeForDelegateRoles) {
		url = addQueryStringParameter(url, 'IncludeForDelegateRoles=1');
	}
	if (includeForEvaluatorRoles) {
		url = addQueryStringParameter(url, 'IncludeForEvaluatorRoles=1');
	}
	if (municipalityID) {
		url = addQueryStringParameter(url, 'MunicipalityID=' + municipalityID);
	}

	ShowPopUp(url, 'Municipality', 450, 450, 'yes');
}


function pickMunicipalityClose(
	id,
	municipalityName,
	districtName,
	provinceName) {
	municipalityPicked(id, municipalityName, districtName, provinceName);
	window.status = '';
}

function pickSubPlaceSearch(
	municipalityID1,
	municipalityID2) {
	var url = '../Popups/SubPlaceSearch.aspx';
	if (municipalityID1) {
		url = addQueryStringParameter(url, 'municipalityID1=' + municipalityID1);
	}
	if (municipalityID2) {
		url = addQueryStringParameter(url, 'municipalityID2=' + municipalityID2);
	}

	ShowPopUp(url, 'SubPlace', 450, 450, 'yes');
}


function pickSubPlaceClose(
	id,
	subPlaceName) {
	subPlacePicked(id, subPlaceName);
	window.status = '';
}

function pickUser(
    municipalityID,
    roleID) {
	var queryString = "";
	if (typeof municipalityID != "undefined"
		&& municipalityID != null
		&& municipalityID != '') {
		queryString += "MunicipalityID=" + municipalityID;
	}
	if (typeof (roleID) != "undefined"
		&& roleID != null
		&& roleID != '') {
		if (queryString != "") {
			queryString += "&";
		}
		queryString += "RoleID=" + roleID;
	}
	if (queryString != "") {
		queryString = "?" + queryString;
	}
	ShowPopUp('../Popups/Users.aspx' + queryString, 'User', 500, 500, 'yes');
}


function pickUserAdvanced(
    municipalityID,
    roleID,
    usersOnly,
    filterMunicipalityID,
    allowAdd) {
	var queryString = "?Filter=1";
	if (typeof (municipalityID) != "undefined"
		&& municipalityID != null
		&& municipalityID != '') {
		if (queryString != "") {
			queryString += "&";
		}
		queryString += "MunicipalityID=" + municipalityID;
	}
	if (typeof (roleID) != "undefined"
		&& roleID != null
		&& roleID != '') {
		if (queryString != "") {
			queryString += "&";
		}
		queryString += "RoleID=" + roleID;
	}
	if (typeof (usersOnly) != "undefined"
		&& usersOnly == true) {
		if (queryString != "") {
			queryString += "&";
		}
		queryString += "usersOnly=1";
	}
	if (typeof (filterMunicipalityID) != "undefined") {
		if (queryString != "") {
			queryString += "&";
		}
		queryString += "filterMunicipalityID=" + filterMunicipalityID;
	}
	if (typeof (allowAdd) != "undefined") {
		if (queryString != "") {
			queryString += "&";
		}
		queryString += "allowAdd=1";
	}
	ShowPopUp('../Popups/UsersAdvanced.aspx' + queryString, 'User', 500, 500, 'yes');
}


function pickUserClose(
    id,
    fullName,
    municipality,
    role) {
	userPicked(id, fullName, municipality, role);
	window.status = '';
}

function pickProject(
	delegateOnly,
	preImplementation,
	design,
	implementation,
	completed,
	suspended,
	withdrawn) {
	var url = '../Popups/Projects.aspx';
	if (preImplementation && preImplementation > 0) {
		url += '?preImplementation=1';
	}
	if (design && design > 0) {
		url += url.indexOf("?") == -1 ? "?" : "&";
		url += 'design=1';
	}
	if (implementation && implementation > 0) {
		url += url.indexOf("?") == -1 ? "?" : "&";
		url += 'implementation=1';
	}
	if (completed && completed > 0) {
		url += url.indexOf("?") == -1 ? "?" : "&";
		url += 'completed=1';
	}
	if (suspended && suspended > 0) {
		url += url.indexOf("?") == -1 ? "?" : "&";
		url += 'suspended=1';
	}
	if (withdrawn && withdrawn > 0) {
		url += url.indexOf("?") == -1 ? "?" : "&";
		url += 'withdrawn=1';
	}
	if (delegateOnly) {
		url += url.indexOf("?") == -1 ? "?" : "&";
		url += 'delegateOnly=1';
	}
	ShowPopUp(url, 'Project', 800, 400, 'yes');
}

function pickProjectClose(id, projectID, projectName, municipalityName, misReference, natReference, provReference, municReference) {
	projectPicked(id, projectID, projectName, municipalityName, misReference, natReference, provReference, municReference)

	window.status = '';


}

function pickCompany(companyIDToExclude, projectID, showConsultants, showContractors) {
	var url = '../Popups/Companies.aspx';
	if (companyIDToExclude && companyIDToExclude > 0) {
		url += '?companyIDToExclude=' + companyIDToExclude;
	}
	if (projectID && projectID > 0) {
		url += url.indexOf("?") == -1 ? "?" : "&";
		url += 'projectID=' + projectID;
	}
	if (showConsultants) {
		url += url.indexOf("?") == -1 ? "?" : "&";
		url += 'showConsultants=1';
	}
	if (showContractors) {
		url += url.indexOf("?") == -1 ? "?" : "&";
		url += 'showContractors=1';
	}
	ShowPopUp(url, 'Company', 600, 550, 'yes');
}

function pickParentCompany(
	companyIDToExclude) {
	var qs = '';
	if (companyIDToExclude) {
		qs = '&companyIDToExclude=' + companyIDToExclude
	}
	ShowPopUp('../Popups/Companies.aspx?hideNewReg=true' + qs, 'ParentCompany', 600, 400, 'yes');
}

function pickCompanyClose(id, companyName) {
	companyPicked(id, companyName);

	window.status = '';
}

function PopupApproveMM(regNoControl, regDateControl, approveReasonControl, ResolutionNo, ResolutionDate, comments) {
	window.status = 'Please complete fields in order to approve.';
	var strAnswer = popupModalDialog('../Popups/ApproveMM_Popup.aspx', ResolutionNo + '|' + ResolutionDate + '|' + comments, 'dialogWidth=450px;dialogHeight=230px;scrollbars=no;center=yes;border=thin;help=no;status=no');
	if (strAnswer != undefined) {
		args = strAnswer.split('|');
		regNoControl.value = args[0];
		regDateControl.value = args[1];
		approveReasonControl.value = args[2];
		window.status = '';
	}
	else {
		regNoControl.value = 'cancelled';
		window.status = 'Action Cancelled';
	}

}

function PopupApproveReject(action, isFinalApproval, comments, reasonControl) {
	window.status = 'Please complete fields in order to ' + action;
	var strAnswer = popupModalDialog('../Popups/ApproveReject.aspx', action + '|' + isFinalApproval + '|' + comments, 'dialogWidth=500px;dialogHeight=200px;scrollbars=no;center=yes;border=thin;help=no;status=no');

	if (strAnswer != undefined) {
		reasonControl.value = strAnswer;
	}
	else {
		reasonControl.value = 'cancelled';
		window.status = 'Action Cancelled';
	}
}

function urlAddQueryStringParameter(url, param, val) {
	if (url.indexOf('?') == -1) {
		url = url + '?';
	}
	else {
		url = url + '&';
	}
	url = url + param + '=' + val;
	return url;
}

/*************************************************************************/
// should be a function, the return handler of the currently opened dialog.
Dialog_return = null;

// constant, the currently opened dialog
Dialog_modal = null;

// the dialog will read it's args from this variable
Dialog_arguments = null;

function popupModalDialog(url, dialogArgs, strFeatures, action, init) {
	var isOpera = navigator.userAgent.indexOf('Opera') > -1;
	var isIE = navigator.userAgent.indexOf('MSIE') > 1 && !isOpera
		|| navigator.userAgent.indexOf('Trident/') > 1 && !isOpera;
	var isMoz = navigator.userAgent.indexOf('Mozilla/5.') == 0 && !isOpera;
	var isMozFirefox = isMoz && navigator.userAgent.indexOf('Firefox') > -1;

	if (typeof init == 'undefined') {
		init = window;	// pass this window object by default
	}
	// Always use showModalDialog() now
	//if (isIE)
	//{
	if (typeof strFeatures == 'undefined') {
		strFeatures = 'dialogWidth=450px;dialogHeight=430px;scrollbars=no;center=yes;border=thin;help=no;status=no';
	}

	var value = init.showModalDialog(url, dialogArgs, strFeatures);
	if (action) {
		action(value);
	}
	return value;
	//}
	//else if (isMoz)
	//{
	//	if (typeof strFeatures == 'undefined')
	//	{
	//		strFeatures = 'toolbar=no,menubar=no,personalbar=no,center=yes;,width=450px,height=430px,scrollbars=no,resizable=no';
	//	}
	//	return showFakeModal(url, dialogArgs, strFeatures);
	//}
}

function Dialog_CollbackFunc(val) {
	getEl('txtHidden3MultiLine').value = val;
	activeBtn.click();
}

//function  popupModalDialog(url, dialogArgs, strFeatures, action, init)
function showFakeModal(url, dialogArgs, strFeatures, action, init) {
	var dlg = open(url, "Popup", strFeatures);
	Dialog_modal = dlg;
	Dialog_arguments = dialogArgs; // init;
	dlg.dialogArguments = dialogArgs;
	dlg.callBackFunc = Dialog_CollbackFunc;

	// capture some window's events
	function capwin(w) {
		w.addEventListener("click", Dialog_parentEvent, true);
		w.addEventListener("mousedown", Dialog_parentEvent, true);
		w.addEventListener("focus", Dialog_parentEvent, true);
	};
	// release the captured events
	function relwin(w) {
		w.removeEventListener("focus", Dialog_parentEvent, true);
		w.removeEventListener("mousedown", Dialog_parentEvent, true);
		w.removeEventListener("click", Dialog_parentEvent, true);
	};
	capwin(window);

	// capture other frames
	for (var i = 0; i < window.frames.length; capwin(window.frames[i++]));

	// make up a function to be called when the Dialog ends.
	Dialog_return = function (val) {
		if (val && action) {
			action(val);
		}
		relwin(window);
		// release other frames
		for (var i = 0; i < window.frames.length; relwin(window.frames[i++]));
		Dialog_modal = null;
	};
}

Dialog_parentEvent = function (ev) {
	if (Dialog_modal && !Dialog_modal.closed) {
		Dialog_modal.focus();
		// we get here in Mozilla only, anyway, so we can safely use the DOM version.
		ev.preventDefault();
		ev.stopPropagation();
	}
};

function addQueryStringParameter(url, strToAppend) {
	if (url.indexOf('?') == -1) {
		url = url + '?';
	}
	else {
		url = url + '&';
	}
	url = url + strToAppend;
	return url;
}


/*************************************************************************/


