﻿/********************************************************
*	Constants											*
********************************************************/
var textClass = "messageInformationFields";
var titleClass = "messageInformationTitle";
var windowContainerID = "windowContainer";
var messageContainerID = "popupWindow";
var warningImagePath = "Images\\info.png";
var popupStatus = 0;

function showAlertMessageIfTestResultSelected(nonIncludeID, message, ok, cancel, warning)
{
	var elementsList = document.getElementsByTagName("input");

	var countOfCheckedElements = 0;
	
	for (var count = 0; count < elementsList.length; count++) {
		if ((elementsList[count].type == "checkbox") && (elementsList[count].id != nonIncludeID.id)) {
			if (elementsList[count].checked) {
				countOfCheckedElements++;
			}
		}
	}
	
	if (countOfCheckedElements > 0)
		return showAlertBox( message, ok, cancel, warning );
	else
		return false;
}

function messageBox( message, ok, warning, cancel, registerPostPack) {

	if ( document.getElementById( windowContainer ) ) return;

	var windowContainer = createWindowContainer();
	document.getElementsByTagName("body")[0].appendChild( windowContainer );
	
	windowContainer.appendChild( messageContainer = createMessageContainer() );
	
	//Create table element
	messageContainer.appendChild( baseTable = createBaseTable() );
	
	//Create tbody Element
	baseTable.appendChild( baseTableTBody = document.createElement("tbody") );
	
	//Create row element
	baseTableTBody.appendChild( baseTableRowTitle = document.createElement("tr") );
	
	//Create cell element
	baseTableRowTitle.appendChild( createCell( "messageTitle", warning ) );
	
	//Create body row
	baseTableTBody.appendChild( baseTableRowBody = document.createElement("tr") );
	
	//Create body cell
	baseTableRowBody.appendChild( baseTableCellBody = document.createElement("td") );
	
	//Create FieldSet
	baseTableCellBody.appendChild( baseTableBodyFieldSet = document.createElement("fieldset") );
	
	if (cancel != null)
		baseTableBodyFieldSet.appendChild( createAlertBody( message, ok, cancel, registerPostPack ) );
	else
		baseTableBodyFieldSet.appendChild( createAlertBody( message, ok, null, registerPostPack ) );

	disableSelection( baseTable );
	
	return false;
}

/********************************************************
*	Alert box for All tab on previous result page		*
********************************************************/
function createAlertBody ( message, ok, cancel, registerPostPack ) {
	
	//Create base table
	var bodyTable = createMainTable();
	
	//Create tbody
	bodyTable.appendChild( tBody = document.createElement("tbody") );
	
	//Create first row
	tBody.appendChild( firstTR = document.createElement("tr") );
	
	//Create cell in first row (for image)
	firstTR.appendChild( firstTD = createCellRowSpanWidth(2, 60) );
	
	//Create image and append to prevoius cell
	firstTD.appendChild( createImage( warningImagePath ) );
	
	//Create cell with message text
	firstTR.appendChild( secondTD = createCellColSpanText( 2, message ) );
	
	//Create second row
	tBody.appendChild( secondTR = document.createElement("tr") );
	
	//Create cell
	secondTR.appendChild( sFirstTD = createCellColSpanAlign(3, "right" ) );
	
	//Add Ok button to prevoiud cell
	sFirstTD.appendChild( createWarningOkButton( ok, registerPostPack ) );
	
	if (cancel != null)
		sFirstTD.appendChild( createWarningCancelButton( cancel ) );		
		
	//return body instance
	return bodyTable;

};

//Show alert box (show on previous result page)
function showAlertBox( message, ok, cancel, warning ) {
	
	if ( document.getElementById( windowContainer ) ) return;

	var windowContainer = createWindowContainer();
	document.getElementsByTagName("body")[0].appendChild( windowContainer );
	
	windowContainer.appendChild( messageContainer = createMessageContainer() );
	
	//Create table element
	messageContainer.appendChild( baseTable = createBaseTable() );
	
	//Create tbody Element
	baseTable.appendChild( baseTableTBody = document.createElement("tbody") );
	
	//Create row element
	baseTableTBody.appendChild( baseTableRowTitle = document.createElement("tr") );
	
	//Create cell element
	baseTableRowTitle.appendChild( createCell( "messageTitle", warning ) );
	
	//Create body row
	baseTableTBody.appendChild( baseTableRowBody = document.createElement("tr") );
	
	//Create body cell
	baseTableRowBody.appendChild( baseTableCellBody = document.createElement("td") );
	
	//Create FieldSet
	baseTableCellBody.appendChild( baseTableBodyFieldSet = document.createElement("fieldset") );
	
	baseTableBodyFieldSet.appendChild( createAlertBody( message, ok, cancel, true ) );

	disableSelection( baseTable );
	
	return false;
};

function createOkButton( value ) {
	var bodyTableRowButton = document.createElement("tr");
	var bodyTableCellButton = document.createElement("td");
	bodyTableCellButton.colSpan = "2";
	bodyTableCellButton.align = "right";
	var okButton = document.createElement("button");
	okButton.align = "right";
	
	//Get first argument
	okButton.value = value;
	
	okButton.className = "okButton";
	okButton.onclick = function() {removeControl(); return false;}
	
	bodyTableCellButton.appendChild(okButton);
	bodyTableRowButton.appendChild(bodyTableCellButton);
	
	return bodyTableRowButton;
};

function createButton( value ) {

	var okButton = document.createElement("button");
	
	okButton.align = "right";
	
	//Get first argument
	okButton.value = value;
	
	okButton.className = "okButton";	
	
	return okButton;
};

/********************************************************
*	Helps method to show custom alert boxes				*
********************************************************/
//Create window container
function createWindowContainer () {
	//Create div control
	var windowContainer = document.createElement( "div" );
	
	//Set parameters
	windowContainer.id = windowContainerID;
	windowContainer.style.height = document.body.scrollHeight + "px";
	
	//Return control instance
	return windowContainer;
};

//Create image element
function createImage( imagePath ) {
	//Create image control
	var image = document.createElement("img");
	
	//Set image path
	image.src = imagePath;
	
	//Set image parameters
	image.width = "37";
	image.height = "37";
	
	image.alt = "warning";
	
	//Return control instance
	return image;
}

//Create warning ok button for post back to server
function createWarningOkButton( text, registerPostPack ) {
	//Create button control
	var button = createButton( text );
	
	//Set button maring
	button.style.marginRight = "5px";
	
	//Add onlick event handler
	button.onclick = function() {
		//Onclick remove window
		removeControl();
		
		if (registerPostPack == true)
			//Onclick call server event handler
			AllPagePostBack();
	};
	
	//Return control instance
	return button;
};


//Create warning cancel button
function createWarningCancelButton( text ) {
	//Create button control
	var button = createButton( text );
	
	//Add onlick event handler
	button.onclick = function() {
		//Onclick remove window
		removeControl();
		
		//Call candidates tab click server event handler
		try {
			CandidatesTabPostBack();
		}
		catch( error ) {
			return button;
		}
	};
	
	//Return control instance
	return button;
};

//Create main table for alert
function createMainTable() {
	//Create table control
	var mainTable = document.createElement("table");
	
	//Set parameters: height and width
	mainTable.style.height = "100%";
	mainTable.style.width = "100%";
	
	//mainTable.border = 1;
	
	//Return control instance
	return mainTable;
};

//Create cell by row span and width parameters
function createCellRowSpanWidth( rowSpan, width ) {
	//Create cell control
	var cell = document.createElement("td");
	
	//Set parameters: row span and width
	cell.rowSpan = rowSpan;
	cell.width = width + "px";
	cell.align = "center";
	
	//Return control instance
	return cell;
}

//Create cell by col span and inner text
function createCellColSpanText( colSpan, text ) {
	//Create cell control
	var cell = document.createElement("td");
	
	//Set parameters: col span and inner text
	cell.colSpan = colSpan;
	cell.innerText = text;
	
	//Return control instance
	return cell;
}

//Create cell by col span and align
function createCellColSpanAlign( colSpan, align ) {
	//Create cell control
	var cell = document.createElement("td");
	
	//Set parametes: col span and align
	cell.colSpan = colSpan;
	cell.align = align;
	
	//Return control instance
	return cell;
}

//Get the document height
function getDocumentHeight() {
	//Return document haight
	return parseInt( document.documentElement.offsetHeight );
};

//Get the document width
function getDocumentWidth() {
	//Return document offset width value
	return parseInt( document.documentElement.offsetWidth );
};

//Get the message box left position
function getMessageBoxLeft( messageContainer ) {
	return parseInt( ( getDocumentHeight() / 2 ) - 150 ) + "px";
};

//Get the message box top positoin
function getMessageBoxTop( messageContainer ) {
	return parseInt( ( getDocumentWidth() / 2 ) - 200 ) + "px";
};

//Create message container
function createMessageContainer () {
	//Create div control
	var messageContainer = document.createElement( "div" );
	
	//Set id value
	messageContainer.id = messageContainerID;
	
	messageContainer.style.top = getMessageBoxLeft( messageContainer );
	messageContainer.style.left = getMessageBoxTop( messageContainer );
	messageContainer.style.display = "block";
	
	return messageContainer;
};

function createBaseTable() {
	var baseTable = createTable();
	baseTable.className = "";
	baseTable.cellPadding = "3";
	baseTable.cellSpacing = "5";
	baseTable.width = "100%";
	return baseTable;
};

function removeControl() {

	document.getElementsByTagName("body")[0].removeChild( document.getElementById( windowContainerID ) );
	
	if (popupStatus == 1) {
		$("#backgroundPopup").fadeOut("slow");  
		popupStatus = 0;  
	};
	
	$("#popupWindow").remove();
};

