﻿// JScript File   
    function a(e)
	{
		var evt = (window.event) ? window.event : e;
	}
	
    function CloseSession(e) 
    {
        var evt = (window.event) ? window.event : e;
		if (evt.clientY < 0) 
		{
			var arrControl = new Array();
            arrControl[0] = "CloseSession";
            arrControl[1] = "1";//Dummy parameter
            getValueByAJAX(arrControl, '../Ajax/Ajax.aspx');
		}
    }
	
	var _request = null;
    function getValueByAJAX(ajaxParameter, ajaxFilePath)
    {// use executeAjaxMethod method here
	    // branch for native XMLHttpRequest object
	    if (_request == null)
	    {
	        if(window.XMLHttpRequest && !(window.ActiveXObject)) {
		        try {
			        _request = new XMLHttpRequest();
		        } catch(e) {
			        _request = null;
		        }
		        // branch for IE/Windows ActiveX version
	        } else if(window.ActiveXObject) {
		        try {
			        _request = new ActiveXObject("Msxml2.XMLHTTP");
		        } catch(e) {
			        try {
				        _request = new ActiveXObject("Microsoft.XMLHTTP");
			        } catch(e) {
				        _request = null;
			        }
		        }
	        }
		}
	    if(_request)
	    {
	        _request.open("POST", ajaxFilePath, false);
	        _request.send("AJAX:"+ajaxParameter); 
		    return _request.responseText;
	    }

	    return null;
    }
    
//This function will return the control in the aspx, you are required
//to pass the td(cell) in which the control is placed. and the position 
//the control occupies inside the cell and field name is the corresponding message
//you need to display

function VerifyEmptyData(tdID, controlPosition, message)
    {
       var txtField = document.getElementById(tdID).childNodes(controlPosition);
        //this condition will be used when the control is a  dropdown
        if(tdID == 'tdTitle' || tdID == 'tdCountry' || tdID =='tdSecQuestion')
        {
            var myindex  = txtField.selectedIndex;
            var SelValue = txtField.options[myindex].value;
            if(SelValue == '0')
            {
                alert('Please select ' + message);
                return false;
            }
        }
       if(txtField.value == '')
       {
            alert('Please fill ' + message);
            return false;
       } 
       return true;
    }

/*  */
function isValidObject(object) {
    return (object != null && object != 'undefined');
}

/*  */
function isValidDocumentControl(controlID) {
    var docControl = document.getElementById(controlID);
    return isValidObject(docControl);
}

/*  */
function setImage(control, imgLink) {
    if (isValidObject(control)) {
        control.src = imgLink;
    }
}

/*  */
function setStatusBarText(text) {
    if (text == '') {
        window.status = 'TKalec';
    }
    else {
        window.status = text;
    }

    document.returnValue = true;
}

/*  */
function setColor(control, color) {
    if (isValidObject(control)) {
        control.style.color = color;
    }
}

/*  */
function setBackgroundColor(control, backgroundColor) {
    if (isValidObject(control)) {
        control.style.backgroundColor = backgroundColor;
    }
}

/*  */
function getControlInTable(table, controlID) {
    var foundControl = null;
    if (isValidObject(table)) {
        var rows = table.rows;
        for (var rowIndex = 0; rowIndex < rows.length; rowIndex++) {
            var cells = rows.item(rowIndex);
            for (var colIndex = 0; colIndex < cells.length; colIndex++) {
                var controls = cells.item(colIndex).childNodes;
                for (var index = 0; index < controls.length; index++) {
                    var control = controls.item(index);
                    if (isValidObject(control.id)) {
                        if (control.id.indexOf(controlID) != -1) {
                            // TODO
                            // Push all controls into an array and return
                            // Pass row index as parameter
                        }
                    }
                }
            }
        }
    }

    return foundControl;
}

/* */
function trim(pstrString)
{
    var intLoop=0;
    for(intLoop=0; intLoop<pstrString.length;)
    {
        if(pstrString.charAt(intLoop)==" ")
        pstrString=pstrString.substring(intLoop+1, pstrString.length);
        else
        break;
    }

    for(intLoop=pstrString.length-1; intLoop>=0; intLoop=pstrString.length-1)
    {
        if(pstrString.charAt(intLoop)==" ")
        pstrString=pstrString.substring(0,intLoop);
        else
        break;
    }
    return pstrString;
}

/* */
	//selectall checkbox functionality to select all the checkbox
function toggleCheckAll(selectPrefix, isSelectAll, rowCount)
{
    var selectSuffix = '_chkSelect';
    
	for(var count=2; count<=rowCount+1; count++)
	{
		document.getElementById(selectPrefix + count + selectSuffix).checked = isSelectAll;
	}
}
	
/* */
//Confirm Delete Message Box.
function ConfirmDelete()
{
    if(confirm('Are you sure you want to delete?'))
    {
        return true;
    }
    else
    {
        return false;
    }
}    

function validateEmailForEnglish(controlID)
{
    var control = document.getElementById(controlID);
    return validateEmail(control, 0);
}
        
function validateEmail(mail, lang)
{				
    var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (filter.test(mail.value)) 
    { 
	    return true;
    }
    else 
    {
        if(lang == 0)
        { 
            alert('E-mail address is not properly written!');
            mail.focus();
	        return false;
	    }
	    else
	    {
	        alert('E-Mail adresse ist nicht richtig geschrieben!');
	        mail.focus();
	        return false;
	    }
    }
    return true;
}
    
function alertDDLNotSelectedValue(controlClientID, controlDesc)
{
    var control = document.getElementById(controlClientID);
    if (control.value == "" || control.value == "0")
    {
        alert("Select " + controlDesc + "!");
        control.focus();
        return false;
    }
    
    return true;
}

function alertIfEmptyTextBox(controlClientID, controlDesc)
{
    var control = document.getElementById(controlClientID);
    if (control.value == "")
    {
        alert("Please insert " + controlDesc + "!");
        control.focus();
        return false;
    }
    
    return true;
}

function alertIfNotNumber(controlClientID, controlDesc)
{
    var control = document.getElementById(controlClientID);
    if (control.value != "")
    {
        if (isNaN(control.value) || control.value<0)
        {
            alert(controlDesc  + ' is not properly written!');
            control.focus();
            return false;
        }
    }
    return true;
}

function getXMLHttpObject()
{// use executeAjaxMethod method here
	// branch for native XMLHttpRequest object
	var req = null;
	if(window.XMLHttpRequest && !(window.ActiveXObject)) {
		try {
			req = new XMLHttpRequest();
		} catch(e) {
			req = null;
		}
		// branch for IE/Windows ActiveX version
	} else if(window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				req = null;
			}
		}
	}
    return req	
}

function ShowPrintDialog()
{
    window.print();
    return false;
}

function RevealModal(divID)
{
    window.onscroll = function () { document.getElementById(divID).style.top = document.body.scrollTop; };
    document.getElementById(divID).style.display = "block";
    document.getElementById(divID).style.visibility = "visible";
    document.getElementById(divID).style.top = document.body.scrollTop;
    
    return false;
}

function HideModal(divID)
{
    document.getElementById(divID).style.display = "none";
    return false;
}
function ValidateEmail(mail, lang)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(mail.value)) 
	{ 
		return true;
	}
	else 
	{
	    if(lang == 0)
        { 
	        alert('E-mail address is not properly written!');
	        mail.focus();
		    return false;
		}
		else
		{
		    alert('E-Mail adresse ist nicht richtig geschrieben!');
		    mail.focus();
		    return false;
		}
	}
	return true;
}

function setLanguage(lang)
{
    var arrControl = new Array();
    arrControl[0] = "SetLanguage";
    arrControl[1] = lang;
    var msg = getValueByAJAX(arrControl, '../Ajax/Ajax.aspx');
}
function getHTMLContent(divClientID, methodName)
{
    var arrControl = new Array();
    arrControl[0] = methodName;
    arrControl[1] = "0";
    document.getElementById(divClientID).innerHTML = getValueByAJAX(arrControl, '../Ajax/Ajax.aspx');
}
        
function BeepByAsynchornousAJAX(ajaxFilePath, ajaxParameter, callBack)
{
    var req = getXMLHttpObject();            		
    if(req)
    {
        req.open("POST", ajaxFilePath, true);
        req.onreadystatechange = function () {callBack(req);};
        req.send("AJAX:" + ajaxParameter); 
    }

    return null;
}

function Beep(res)
{
}

function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

function EnterKeyPress(e, btnClientId) 
{ 
    // look for window.event in case event isn't passed in 
    if (window.event) { e = window.event; } 
    if (e.keyCode == 13) 
    { 
            document.getElementById(btnClientId).click(); 
    } 
} 

