// Get index of element in the array by it client id.
function GetIndexOf(array, clientId)
{
    for (var i = 0; i < array.length; i++)
    {
        if ((typeof(array[i].id) != "undefined") && 
            (array[i].id.indexOf(clientId) != -1))
        {
            return i;
        }  
    }
    
    return -1;
}

//KVV: Calculates sum of given arguments skipping non numeric ones
function CalculateSum() {
	var sum = 0;
	for (i = 0; i<arguments.length; i++) {
		var value = arguments[i] - 0;
		sum += isNaN(value) ? 0 : value;
	}
	return sum;
}

//KVV: Retrives number value from control
function GetNumberValue(inputNumId) {
	var ctrl = document.getElementById(inputNumId);
	return GetCtrlNumberValue(ctrl);	
}

//Retrives number value from control (not ControlId).
function GetCtrlNumberValue(ctrl) {
	if (ctrl.number != null) {
		if (typeof(ctrl.number) != "number") {
			return parseFloat(ctrl.number + "");
		} else {
			return ctrl.number;
		}
	} else {
		var strValue = ctrl.value;
		if (strValue) {
			if (ctrl.thDelim) {
				strValue = strValue.replace(new RegExp(ctrl.thDelim,"g"),"");
			}
			if (ctrl.decDelim) {
				strValue = strValue.replace(new RegExp(ctrl.decDelim,"g"),".");
			}
			return parseFloat(strValue);
		} else {
			return null;
		}
	}
}

//Set number value to control with specified ID
function SetNumberValue(inputNumID, NumberValue) {
	var ctrl = document.getElementById(inputNumID);
	ctrl.number = NumberValue;
	ctrl.value = renderNumber(ctrl);
}

//Sets number value to control directly.
function SetCtrlNumberValue(ctrl, NumberValue) {
	if (ctrl==null) return;
	ctrl.number = NumberValue;
	ctrl.value = renderNumber(ctrl);
}

function FloatToStringComma(value, decLen) {
	if (decLen == null) {
		decLen = 2;
	}
	return (Math.round(value * Math.pow(10, decLen)) / Math.pow(10, decLen)).toFixed(decLen).replace(/\./g, ",");
}

function A3ButtonDisable(obj) {
	obj.disabled = true;
	obj.className = "A3ButtonDis"
}

function A3ButtonEnable(obj) {
	obj.disabled = false;
	obj.className = "A3Button"
}

//KVV: Enables validator
function EnableValidator(validator) {
	EnableValidatorSilent(validator);
	ValidatorValidate(validator);
}

//KVV: Disables validator
function DisableValidator(validator) {
	validator.enabled=false;
	validator.disabled=true;
	ValidatorValidate(validator);
}

function EnableValidatorSilent(validator) {
	validator.enabled=true;
	validator.disabled=false;
}

//AS:Returns validator state.
function ValidatorIsValid(validator){
	var isvalid = true;
    if (validator.enabled != false) {
        if (typeof(validator.evaluationfunction) == "function") {
            isvalid = validator.evaluationfunction(validator); 
        }
    }
    return isvalid;
}

//KVV: Clears items selected in WGP
function ClearWGPSelectedItems()
{
	var hidden = document.getElementById("A3WGPWebGrid_SelectedItems");
	if (hidden && hidden.value) {
		hidden.value = "";
	}
}

function ClearWGPSelectedItems(gridId)
{
	if (!gridId||gridId=='') gridId = "A3WGPWebGrid";
	var hidden = document.getElementById(gridId + '_SelectedItems');
	if (hidden && hidden.value) {
		hidden.value = "";
	}
}

function FormatNumber(number, length, decLength) {
	if (!length) return number.toString();
	if (!decLength) decLength = 0;
	
	var numStr = FloatToStringComma(number, decLength);
	
	for (var dif = length - numStr.length; dif > 0; dif--) {
		numStr = "0" + numStr;
	}
	return numStr;
}

function FormatDate(date, format) {
	var year = date.getDate();
	var month = date.getMonth() + 1;
	var day = date.getFullYear();

	formatArray = format.toUpperCase().split("/");

	var dateString = "";

	for(var i = 0; i < formatArray.length; i++) {
		switch (formatArray[i].charAt(0)) {
			case "D":
				dateString = "" + dateString + FormatNumber(year, formatArray[i].length);
				break;
			case "M":
				dateString = "" + dateString + FormatNumber(month, formatArray[i].length);
				break;
			case "Y":
				dateString = "" + dateString + FormatNumber(day, formatArray[i].length);
				break;
		}
		if (i != formatArray.length - 1) dateString = dateString + "/";
	}
	
	return dateString;
}

function PutDateIntoA3InputDate(object, date)
{
	object.value = FormatDate(date, object.clientFormat);
	object.DateValue = FormatDate(date, object.formatDate);
}

function F4WaitingDIV(Text){
    var workAreaFrameIn = document.frames.parent;
    var workAreaFrame = workAreaFrameIn.parent["WorkArea"];
    var actionsFrame = workAreaFrameIn.parent["Actions"];

    ModalWaitingDIV(Text, workAreaFrame, actionsFrame, focusAgainF4);
}

function WizardWaitingDIV(Text){
    var workAreaFrameIn = document.frames.parent;
    var workAreaFrame = workAreaFrameIn.parent["WorkArea"];
    var actionsFrame = workAreaFrameIn.parent["Actions"];
    
    ModalWaitingDIV(Text, workAreaFrame, actionsFrame, focusAgainWizard);
}


function ModalWaitingDIV(Text, workAreaFrameIn, actionsFrameIn, focusFunction){
	var o;
	var realDoc = workAreaFrameIn.document;
    var workAreaFrame = workAreaFrameIn;
    var actionsFrame = actionsFrameIn;

	if (realDoc.getElementById('lblWaiting')== null)
	{
		o = realDoc.createElement("<label id='lblWaiting' align='center' style='top:100px;height:0px;border:2px #807860 solid; font-family:Arial; font-weight:bold; position:absolute; color:#4a4739; background-color:#ded6b5; vertical-align:baseline; text-align:center; display:inline'>");
		realDoc.body.insertBefore(o,realDoc.forms[0]);
	}
	else{
		o = realDoc.getElementById('lblWaiting')
	}
	
	if (Text.length > 0) {
		o.innerText = Text;
		o.style.left = new String(realDoc.body.scrollLeft + (realDoc.body.clientWidth/2)) + "px";
		o.style.top = new String(realDoc.body.scrollTop + (realDoc.body.clientHeight/2)) + "px";
		o.style.left = parseInt(o.style.left) - (o.clientWidth/2) + "px";
		o.style.top = parseInt(o.style.top) - (o.clientHeight/2) + "px";
        o.style.zIndex = 1000;
        
        disableFrame(true,workAreaFrame, true, focusFunction);
		disableFrame(true,actionsFrame, false, focusFunction);
	}
	else {
		o.removeNode(true);
        disableFrame(false,workAreaFrame, false, focusFunction);
        disableFrame(false,actionsFrame, false, focusFunction);
	}
}

function disableFrame(state, frame, setActive, focusFunction)
{
   	var realDoc = frame.document;
    if (state)
    {
        if(realDoc.getElementById('BlockLayer') == null)
	    {
		    var objDiv = realDoc.createElement("<DIV id='BlockLayer' style='background-color:white;'></DIV>");

		    objDiv.style.top = 0;
		    objDiv.style.left = 0;
		    objDiv.style.width = "100%";
		    objDiv.style.height = "100%";
		    objDiv.style.position = 'absolute';
		    objDiv.hideFocus = true;
		    objDiv.style.zIndex = 900;
		    objDiv.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=50)";
		    objDiv.style.visibility = 'visible';
		    realDoc.body.appendChild(objDiv);
		    if (setActive)
		    {
		        objDiv.focus();
//		        objDiv.onfocusout = focusAgainF4;
		        objDiv.onfocusout = focusFunction;
		    }
		}
    }
    else
    {
        if (realDoc.getElementById('BlockLayer') != null)
            realDoc.body.removeChild(realDoc.getElementById('BlockLayer'));
    }
}

function focusAgainF4()
{
    var workAreaFrame = document.frames.parent.parent["WorkArea"];
    var realEvent = workAreaFrame.window.event;
    if ((realEvent!=null)&&(realEvent.srcElement!=null))
        realEvent.srcElement.focus();
}

function focusAgainWizard()
{
    var workAreaFrame = document.frames.parent["WorkArea"];
    var realEvent = workAreaFrame.window.event;
    if ((realEvent!=null)&&(realEvent.srcElement!=null))
        realEvent.srcElement.focus();
}

function ShowMultipleSelectF4(SelectorControl, AppId)//pageURL, resultInputId, submitButtonId, selectorWidth, selectorHeight)
{
    var isOK = ShowA3Modal(AppId, encodeURI(SelectorControl.pageURL), 
        SelectorControl.modalWidth, SelectorControl.modalHeight);
	if (isOK){
	    document.getElementById(SelectorControl.resultInputId).value = isOK;
	    document.getElementById(SelectorControl.submitButtonId).click();
	}
	else
	{
	    document.getElementById(SelectorControl.resultInputId).value = '';
	}
}

function UpdateValidatorById(validatorId)
{
    if(event.propertyName=='value')
    {
        var validator = document.getElementById(validatorId);
        if (validator != null) ValidatorValidate(validator);
    }
}
