var prm = Sys.WebForms.PageRequestManager.getInstance();

Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);

function CancelAsyncPostBack() {
    if (prm.get_isInAsyncPostBack()) {
      prm.abortPostBack();
    }
}

function InitializeRequestHandler(sender, args)
{
    //var prm1 = Sys.WebForms.PageRequestManager.getInstance();
    if (prm.get_isInAsyncPostBack())
    {
        args.set_cancel(true);
        //prm.abortPostBack();
        //args.set_cancel(false);
        //var elem = args.get_postBackElement();
        //alert(elem.value);
    }
}

function BeginRequestHandler(sender, args)
{    
    if (prm.get_isInAsyncPostBack())
    {
        //alert("Don't stop this process!!!");
        args.set_cancel(false);
    }
    var oProgressControlName = document.forms[0].hiddenProgressControlName;
    var sProgressControlName = "divProgress";
    if (oProgressControlName != null)
        sProgressControlName = oProgressControlName.value;
    var oDiv = document.getElementById(sProgressControlName);
    if (oDiv == null)
    {
        oDiv = document.getElementById('divProgress');
	}
	// To get the position for the div to display
	var divWidth = oDiv.offsetWidth ? oDiv.offsetWidth : oDiv.style.width ? parseInt( oDiv.style.width ) : 0;
    var divHeight = oDiv.offsetHeight ? oDiv.offsetHeight :  oDiv.style.height ? parseInt( oDiv.style.height ) : 0;
    // Calculating setX and setX so the div will be centered in the viewport.
    var setX = ( getDocumentWidth() - divWidth ) / 2;
    var setY = ( getDocumentHeight() - divHeight ) / 2
    // If setX or setY have become smaller than 0, make them 0.
    if( setX < 0 ) setX = 0;
    if( setY < 0 ) setY = 0;
	if (sProgressControlName == "divProgress")
	{
        // Position the div in the center of the page and make it visible.
        oDiv.style.left = setX + document.documentElement.scrollLeft + "px";
        oDiv.style.top = setY + document.documentElement.scrollTop + "px";
    }
	oDiv.style.display = 'block';
    oDiv.style.visibility = 'visible';
}

function EndRequestHandler(sender, args)
{
    var oDivDefault = document.getElementById('divProgress');
    oDivDefault.style.display = 'none';
    oDivDefault.style.visibility = 'hidden';
    //oDivDefault.style.height = "100px";
    // hide others
    //var oDivVolume = document.getElementById('divVolumeProgress')
    var oDivOther = document.forms[0].hiddenProgressControlName;
    if (oDivOther != null)
    {
        var sOther = oDivOther.value;
        var oDiv2Hide = document.getElementById(sOther);
        if (oDiv2Hide != null)
        {
            oDiv2Hide.style.display = 'none';
            oDiv2Hide.style.visibility = 'hidden';
        }
    }
}

// Show/Hide Modal Popup
function showZoomIcon(zoomIconID)
{
    var oZoomIcon = document.getElementById(zoomIconID);
    oZoomIcon.style.display = 'block';
    oZoomIcon.style.visibility = 'visible';
}

function hideZoomIcon(zoomIconID)
{
    var oZoomIcon = document.getElementById(zoomIconID);
    oZoomIcon.style.display = 'none';
    oZoomIcon.style.visibility = 'hidden';
}
        
function showModalPopupSmartURL(sSmartURL)
{
    //$addHandler($get("showModalPopupClientButton"), 'mouseover', showModalPopupViaClient);
    var iframeobj=document.getElementById? document.getElementById("ifSamrtURL") : document.all.ifSamrtURL
    iframeobj.src = sSmartURL;
    var modalPopupBehavior = $find('proModalPopupSmartURL');
    //alert(modalPopupBehavior);
    modalPopupBehavior.show();
}

//function loadScreen(sViewType)
//{
//    //var oHiddenView = document.getElementById('hiddenViewType');
//    //oHiddenView.value = "tone";
//    $find('hiddenViewType').value = "tone";
//    __doPostBack('lbtLoadScreen','');
//}

function showModalPopupCluster()
{
    var oPostQuery = document.forms[0].hiddenPostQuery;
    //var oClusterMainQuery = document.forms[0].txtSearchKeyword;
    //if (oPostQuery != null)
    //    sProgressControlName = oProgressControlName.value;    
    var sQuery = encodeURI(oPostQuery.value);
    var ifrCluster=document.getElementById? document.getElementById("ifrCluster") : document.all.ifSamrtURL
    ifrCluster.src = "cluster.aspx" + sQuery;
    var modalPopupBehaviorCluster = $find('proModalPopupCluster');
    modalPopupBehaviorCluster.show();
}

function ShareWithFriend(sID, sModalPopup) {
    document.forms[0].selectedSKU.value = sID;
    var modalPopupBehavior = $find(sModalPopup);
    modalPopupBehavior.show();
}

function showModalPopup(sModalPopup)
{
    var modalPopupBehavior = $find(sModalPopup);
    modalPopupBehavior.show();
}

function hidePopup(sModalName)
{
    var modalPopupBehavior = $find(sModalName);
    if (sModalName == "proModalPopupSmartURL")
    {
        var iframeobj=document.getElementById? document.getElementById("ifSamrtURL") : document.all.ifSamrtURL
        iframeobj.src = "";
        //document.all.ifSamrtURL.src = "";
    }
    document.getElementById('hiddenProgressControlName').value = "divProgress";
    modalPopupBehavior.hide();
}

// My own functions
getDocumentWidth = function() {
	var width = 0;
	if( document.documentElement && document.documentElement.clientWidth ) {
		width = document.documentElement.clientWidth;
	}
	else if( document.body && document.body.clientWidth ) {
		width = document.body.clientWidth;
	}
	else if( window.innerWidth ) {
		width = window.innerWidth - 18;
	}
	//alert("W: " + width);
	return width;
};

getDocumentHeight = function() {
	var height = 0;
	if( document.documentElement && document.documentElement.clientHeight ) {
		height = document.documentElement.clientHeight;
	}
	else if( document.body && document.body.clientHeight ) {
		height = document.body.clientHeight;
	}
	else if( window.innerHeight ) {
		height = window.innerHeight - 18;
	}
	//alert("H: " + height);
	return height;
};

// div allignment $$$$$$$$$$$ 
// positioning div
positionIt = function(divName) {
	if( document.getElementById ) {
		// Get a reference to divTest and measure its width and height.
		var div = document.getElementById( "divProgress" );
		var divWidth = div.offsetWidth ? div.offsetWidth : div.style.width ? parseInt( div.style.width ) : 0;
		var divHeight = div.offsetHeight ? div.offsetHeight :  div.style.height ? parseInt( div.style.height ) : 0;
		
		// Calculating setX and setX so the div will be centered in the viewport.
		var setX = ( getViewportWidth() - divWidth ) / 2;
		var setY = ( getViewportHeight() - divHeight ) / 2;
		
		// If setX or setY have become smaller than 0, make them 0.
		if( setX < 0 ) setX = 0;
		if( setY < 0 ) setY = 0;
		
		// Position the div in the center of the page and make it visible.
		div.style.left = setX + "px";
		div.style.top = setY + "px";
		//div.style.visibility = "visible";
	}
};

getViewportWidth = function() {
	var width = 0;
	if( document.documentElement && document.documentElement.clientWidth ) {
		width = document.documentElement.clientWidth;
	}
	else if( document.body && document.body.clientWidth ) {
		width = document.body.clientWidth;
	}
	else if( window.innerWidth ) {
		width = window.innerWidth - 18;
	}
	return width;
};

getViewportHeight = function() {
	var height = 0;
	if( document.documentElement && document.documentElement.clientHeight ) {
		height = document.documentElement.clientHeight;
	}
	else if( document.body && document.body.clientHeight ) {
		height = document.body.clientHeight;
	}
	else if( window.innerHeight ) {
		height = window.innerHeight - 18;
	}
	return height;
};

//window.onresize = positionIt;
//window.onload = positionIt;