﻿//Page Load event handler. Write all code needed to be executed on pageLoad
function pageLoad(sender, args){
    if(!args.get_isPartialLoad()){
        //  add our handler to the document's
        //  keydown event
        $addHandler(document, "keydown", onKeyDown);
    }
    
    // If function contentPageLoad exists, execute it.
    if(typeof contentPageLoad == 'function') {
        contentPageLoad();
    }
}
function onKeyDown(e){
}

//Page Unload event handler. Write all code needed to be executed on pageUnload
function pageUnload()
{
    Sys.Net.WebRequestManager.remove_invokingRequest(onInvokingRequest);
    Sys.Net.WebRequestManager.remove_completedRequest(onCompleteRequest);
    //Sys.WebForms.PageRequestManager.getInstance().remove_beginRequest(onInvokingRequest);
    //Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(onCompleteRequest);
}

/* Browser View Port Class */
BrowserViewPort = function() {
    // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
    if (typeof window.innerWidth != 'undefined')
    {
        this._viewportwidth = window.innerWidth,
        this._viewportheight = window.innerHeight
    }

    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
    else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
    {
       this._viewportwidth = document.documentElement.clientWidth,
       this._viewportheight = document.documentElement.clientHeight
    }

    // older versions of IE
    else
    {
       this._viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       this._viewportheight = document.getElementsByTagName('body')[0].clientHeight
    }
}

BrowserViewPort.prototype = {

    getViewPortWidth: function() {
        return this._viewportwidth;
    },    
    getViewPortHeight: function() {
        return this._viewportheight;
    }
}

// Notify ScriptManager that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();



//var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height, scrollbars)
{
    var winl = (screen.width - width) / 2;
	var wint = (screen.height - height) / 2;	
	if (left != 0) winl=left;
	if (top != 0) wint=top;
	if (!scrollbars) scrollbars = 'no';
    
    /*if (popUpWin != null)
    {
        if (!popUpWin.closed) 
            popUpWin.close();
    }*/
    
    var now = new Date();

    var popUpWin = open(URLStr, 'popUpWin' + now.getMilliseconds(), 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=' + scrollbars + ',resizable=no,copyhistory=yes,width=' + width + ',height=' + height + ',left=' + winl + ', top=' + wint + ',screenX=' + winl + ',screenY=' + wint + '');
}

function popUpWindowResizeable(URLStr, left, top, width, height, scrollbars)
{
    var winl = (screen.width - width) / 2;
	var wint = (screen.height - height) / 2;	
	if (left != 0) winl=left;
	if (top != 0) wint=top;
	if (!scrollbars) scrollbars = 'no';
	
	/*if (popUpWin != null)
	{
	if (!popUpWin.closed) 
	popUpWin.close();
	}*/
    
    var popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars='+scrollbars+',resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+winl+', top='+wint+',screenX='+winl+',screenY='+wint+'');
}

function popUpSearchWindow(URLStr, left, top, width, height)
{	
    var winl = (screen.width - width) / 2;
	var wint = (screen.height - height) / 2;
	
	if (left != 0) winl=left;
	if (top != 0) wint=top;	

	var returnVal = window.showModalDialog(URLStr,'', 
	'dialogWidth:'+width+'px; dialogHeight:'+height+'px; dialogTop:'+wint+'px; dialogLeft:'+winl+'px; help:no; resizable:no; status:no; ');
	try
	{
		splitstring(returnVal);
	}
	catch(e)
	{}
}

function makePopUpOnTop()
{
	try
	{
		if(popUpWin)
		{
			popUpWin.focus();
		}
	}
	catch(e){}
}

function splitstring(stringtosplit)
{
	var listAllVal = stringtosplit.split("*");
	var listArray = stringtosplit.split("|");
	if (listAllVal.length > 1)
	{
		listArray = listAllVal[0].split("|");
	}
    for (var i=0;i<listArray.length; i++)
    {
		if (listArray[i].indexOf("Refresh") > -1 || listArray[i].indexOf("Search") > -1)
		{
			try
			{
				var tempexec = "__doPostBack('" + listArray[i] + "','');";
				eval(tempexec);
			}
			catch(e) {}
		}
		else
		{		
		    if (eval("document.getElementById('" + listArray[i] + "')"))
		    {
			    var tempexec = "document.getElementById('" + listArray[i] + "').value='"+ listArray[i+1]+"';";
			    eval(tempexec);
			}
		}
		i++;
    }    
    if (listAllVal.length > 1)
    {
		listArray = listAllVal[1].split("|");		
		for (var i=0;i<listArray.length; i++)
		{
			var tempexec = "__doPostBack('" + listArray[i] + "','" + listArray[i+1] + "');";
			eval(tempexec);
			i++;
		}
    }
}

function execReturnValue(returnVal)
{   
    try
    {
	    splitstring(returnVal);  
    }
    catch(e)
    {}
}

// This function move item in the listbox -1 = Up; +1 = Down
function moveInList( frmName, srcListName, index, to) {
	var form = eval( 'document.' + frmName );
	var srcList = eval( 'form.' + srcListName );
	var total = srcList.options.length-1;

	if (index == -1) {
		return false;
	}
	if (to == +1 && index == total) {
		return false;
	}
	if (to == -1 && index == 0) {
		return false;
	}

	var items = new Array;
	var values = new Array;

	for (i=total; i >= 0; i--) {
		items[i] = srcList.options[i].text;
		values[i] = srcList.options[i].value;
	}
	for (i = total; i >= 0; i--) {
		if (index == i) {
			srcList.options[i + to] = new Option(items[i],values[i], 0, 1);
			srcList.options[i] = new Option(items[i+to], values[i+to]);
			i--;
		} else {
			srcList.options[i] = new Option(items[i], values[i]);
	   }
	}
	srcList.focus();
	return false;
}

function setSelectedValue( frmName, srcListName, value ) {
	var form = eval( 'document.' + frmName );
	var srcList = eval( 'form.' + srcListName );

	var srcLen = srcList.length;

	for (var i=0; i < srcLen; i++) {
		srcList.options[i].selected = false;
		if (srcList.options[i].value == value) {
			srcList.options[i].selected = true;
		}
	}
}

function getSelectedValue( frmName, srcListName ) {
	var form = eval( 'document.' + frmName );
	var srcList = eval( 'form.' + srcListName );

	i = srcList.selectedIndex;
	if (i != null && i > -1) {
		return srcList.options[i].value;
	} else {
		return null;
	}
}

function showLov(URLStr, left, top, width, height) {
	popUpSearchWindow(URLStr, left, top, width, height);
	//popUpWindow(URLStr, left, top, width, height, "no");
	return false;
}

function CheckBoxList_Click(ObjId)
{
    var control = document.getElementById(ObjId);
//    var controlTarget = document.getElementById(ObjTargetId);    
    var selectedItemCount = 0;
    var liIndex = 0;
    var currentListItem = document.getElementById(control.id + '_' + liIndex.toString());
    var firstItem = currentListItem;
//    var firstItemValue = GetLabelForCheckbox(control,firstItem);
//    controlTarget.value = "";
    while (currentListItem != null)
    {
        if (currentListItem.checked)
        {
            selectedItemCount++;
//            if (controlTarget.value != "") controlTarget.value+=",";
//            controlTarget.value += GetLabelForCheckbox(control,currentListItem);
        }
        liIndex++;
        currentListItem = document.getElementById(control.id + '_' + liIndex.toString());
    }
    if (selectedItemCount <= 0)
    {
        firstItem.checked = true;
//        controlTarget.value = firstItemValue;        
    }
//    controlTarget.focus();
}

function GetLabelForCheckbox(control,elem)
{
    var e = control.getElementsByTagName("label");
    for (var i=0; i<e.length; i++)
    {
     if (e[i].htmlFor == elem.id)
     {
        for (var j=0; j<e[i].childNodes.length; j++)
        {
            if (e[i].childNodes[j].nodeType == 3) //text type
            {
                return e[i].childNodes[j].nodeValue;
            }
        }
     }
    }
    return "";
}

// JS Calendar
var calendar = null; // remember the calendar object so that we reuse
// it and avoid creating another

// This function gets called when an end-user clicks on some date
function selected(cal, date) {
    cal.sel.value = date; // just update the value of the input field
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks the "Close" (X) button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
    cal.hide(); 		// hide the calendar

    // don't check mousedown on document anymore (used to be able to hide the
    // calendar when someone clicks outside it, see the showCalendar function).
    Calendar.removeEvent(document, "mousedown", checkCalendar);
}

// This gets called when the user presses a mouse button anywhere in the
// document, if the calendar is shown.  If the click was outside the open
// calendar this function closes it.
function checkCalendar(ev) {
    var el = Calendar.is_ie ? Calendar.getElement(ev) : Calendar.getTargetElement(ev);
    for (; el != null; el = el.parentNode)
    // FIXME: allow end-user to click some link without closing the
    // calendar.  Good to see real-time stylesheet change :)
        if (el == calendar.element || el.tagName == "A") break;
    if (el == null) {
        // calls closeHandler which should hide the calendar.
        calendar.callCloseHandler(); Calendar.stopEvent(ev);
    }
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id, dateFormat, showsTime) {
    var el = document.getElementById(id);
    if (calendar != null) {
        // we already have one created, so just update it.
        calendar.hide(); 	// hide the existing calendar
        // commented by Andri on Oct 15,2006 to always set calendar with value from text box
        //calendar.parseDate(el.value); // set it to a new date
    } else {
        // first-time call, create the calendar
        var cal = new Calendar(true, null, selected, closeHandler);
        calendar = cal; 	// remember the calendar in the global
        cal.setRange(1900, 2070); // min/max year allowed
        if (typeof (dateFormat) != 'undefined') {
            cal.dateFormat = dateFormat; // date format
        }
        if (typeof (showsTime) != 'undefined') {
            cal.showsTime = showsTime; // is time field shown
        }
        calendar.create(); 	// create a popup calendar
    }
    calendar.sel = el; 	// inform it about the input field in use
    calendar.showAtElement(el); // show the calendar next to the input field

    // catch mousedown on the document
    Calendar.addEvent(document, "mousedown", checkCalendar);

    // added by Andri on Oct 15,2006 to always set calendar with value from text box
    calendar.parseDate(el.value);

    return false;
}
