﻿// JScript File

function GetWindowHeight()
 {
    var myHeight = 0;
    if( typeof( window.innerHeight ) == 'number' ) 
    {
    //Non-IE
    myHeight = window.innerHeight;
    } 
    else if( document.documentElement && ( document.documentElement.clientHeight ) ) 
    {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
    } 
    else if( document.body && ( document.body.clientHeight ) ) 
    {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
    }
    return myHeight;
 }
 function GetWindowWidth()
 {
    var myWidth = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    } else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    } else if( document.body && ( document.body.clientWidth ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    }
    return myWidth;
 }
 
 function SetSize(element,width,height)
 {
    if (width != null)
    {
        element.attr("width",width);
        element.width(width);
    }
    if (height != null)
    {
        element.attr("height",height);
        element.height(height);
    }
 }
 
function SetPosition(element,top,left)
{
    if ($(element).length)
    {
        if (top != null)
            element.style.top = top.indexOf('px') > -1 ? top : top + 'px';
        if (left != null)
            element.style.left = left.indexOf('px') > -1 ? left : left + 'px';
    }
}
 
function winRefresh()
{
    if (window.location.href.indexOf('isPost=1') <= -1)       
    {
        var query = "";
        if (window.location.href.indexOf('?') > -1)
            query = "&";
        else
            query = "?"; 
        window.location = window.location + query + "isPost=1";
    }
    else
        window.location = window.location;
}
function selectByUrl(btn)
{
//alert('111');
var url = btn.value;
    var theData = "type=add-siteshot-url&url="+url;
    $.ajax(
    {
        type    : "POST",
        url     : SiteUrl + DBPageCallUrl,
        data    : theData,
        async   : true,
        success : function(data) 
        {
            // do something..
        }
    });

//alert(phpSelection + '?loadUrl='+url.replace('&','%2526'));
    leaveSite=true;
window.location = phpSelection + '?loadUrl='+url.replace('&','%2526');
}

function ajaxUpdateCall(theParams){
    var url = SiteUrl + DBPageCallUrl;
    if(GlobalConsts.updateDB)
        $.get(url, theParams);
}
String.prototype.endsWith = function (s) {
    if ('string' != typeof s) {
        throw('IllegalArgumentException: Must pass a ' +
            ' string to String.prototype.endsWith()');
    }
    var start = this.length - s.length;
    return this.substring(start) == s;
};


function delegate( that, thatMethod )
{
    if(arguments.length > 2)
    {
        var _params = [];
        for (var n = 2; n < arguments.length; ++n) _params.push(arguments[n]);
        return function() { 
            return thatMethod.apply(that,_params); 
        }
    }
    else
        return function() { return thatMethod.call(that); }
}

function addEvent (elm, evType, fn, useCapture){
		if (elm.addEventListener) {
			elm.addEventListener(evType, fn, useCapture);
			return true;
		}
		else 
			if (elm.attachEvent) {
				var r = elm.attachEvent('on' + evType, fn);
				return r;
			}
			else {
				elm['on' + evType] = fn;
			}
	}

function getTargetElement(e)
{
    var targ;
    if (e.target)
    {
        targ = e.target;
    }
    else if (e.srcElement)
    {
        targ = e.srcElement;
	}
	if (targ.nodeType == 3) // defeat Safari bug
	{
		targ = targ.parentNode;
	}
	
	return targ;
}

function ValidateEmail(email)
{ 
    var emailPattern = new RegExp("^[_0-9a-zA-Z]+([-+.][_0-9a-zA-Z]+)*@[_0-9a-zA-Z]+([-.][_0-9a-zA-Z]+)*\.[_0-9a-zA-Z]+([-.][_0-9a-zA-Z]+)*$");
    return emailPattern.test(email);
}

function GeneralClickGo(elementClientID,e)
{
    var theEvent = e;
    if(theEvent.which || theEvent.keyCode)
    {
        if ((theEvent.which == 13) || (theEvent.keyCode == 13))
        {
            $("#" + elementClientID).click();
            return false;
        }
    }
    else {return true};
}



/*******************/
/* Image Preloader */
/*******************/

function ImagePreloader(images, callback)
{
   // store the callback
   this.callback = callback;
   
   // initialize internal state.
   this.nLoaded = 0;
   this.nProcessed = 0;
   this.aImages = new Array;
   
   // record the number of images.
   this.nImages = images.length;
   
   // for each image, call preload()
   for ( var i = 0; i < images.length; i++ ) 
      this.preload(images[i]);
}

ImagePreloader.prototype.preload = function(image)
{
   // create new Image object and add to array
   var oImage = new Image;
   this.aImages.push(oImage);

   // set up event handlers for the Image object
   oImage.onload = ImagePreloader.prototype.onload;
   oImage.onerror = ImagePreloader.prototype.onerror;
   oImage.onabort = ImagePreloader.prototype.onabort;

   // assign pointer back to this.
   oImage.oImagePreloader = this;
   oImage.bLoaded = false;

   // assign the .src property of the Image object
   oImage.src = image;
}

ImagePreloader.prototype.onComplete = function()
{
   this.nProcessed++;
   if ( this.nProcessed == this.nImages )
   {
      this.callback(this.aImages, this.nLoaded);
   }
}

ImagePreloader.prototype.onload = function()
{
   this.bLoaded = true;
   this.oImagePreloader.nLoaded++;
   this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onerror = function()
{
   this.bError = true;
   this.oImagePreloader.onComplete();
}

ImagePreloader.prototype.onabort = function()
{
   this.bAbort = true;
   this.oImagePreloader.onComplete();
}




/**********************/
/* Leave Page Control */
/**********************/

function LeavePageControl() {
    
}

LeavePageControl.prototype._isActive;
LeavePageControl.prototype._dataChanged;
LeavePageControl.prototype._lastClicked;
LeavePageControl.prototype._alerted;
LeavePageControl.prototype._onbeforeunloadMsg;
LeavePageControl.prototype._skipIDs;
LeavePageControl.prototype._lastDdlSelectdIndex;

LeavePageControl.prototype.Instantiate = function Instantiate() {
    window.onbeforeunload = delegate(this, this.UnloadMessage);
//    if (window.addEventListener) {
//        window.addEventListener('beforeunload', delegate(this, this.UnloadMessage), false); 
//    } else if (window.attachEvent) {
//        window.attachEvent('onbeforeunload', delegate(this, this.UnloadMessage));
//    }

    
    this._isActive = true;
    this._dataChanged = true;
    
    this._lastClicked = '';
    this._alerted = '';
    this._onbeforeunloadMsg = '';
    
    this._skipIDs = new Array();
    
    document.onclick = this.Clicked;
    if (document.captureEvents) {
        document.captureEvents(Event.CLICK);
    }
}
/*not in use
function HandleOnBeforeUnload() {
    return delegate(this, this.UnloadMessage);
}
*/
LeavePageControl.prototype.UnloadMessage = function UnloadMessage() {
    //var lpc = LeavePageControl;
    if (this._isActive) {
        var skipAlert = false;
        if (leaveSite) skipAlert = true;
        if (numOfFrames==0) skipAlert = true;
        
        var i;
        for (i=0; i<this._skipIDs.length; i++) {
            if (this._lastClicked && this._lastClicked.id && this._lastClicked.id.indexOf(this._skipIDs[i]) > -1) {
                skipAlert = true;
            }
        }

        if (this._alerted != '' && this._lastClicked == this._alerted) {
            skipAlert = true;
        }

        this._alerted = this._lastClicked;
        //alert('bye bye...');
	    if (this._dataChanged && !skipAlert ) {
	        return this._onbeforeunloadMsg;
        }
    } else {
        // dont return anything so the confirmation box wont show.
    }
}

LeavePageControl.prototype.Clicked = function Clicked(e) {
    this._alerted = '';
    
    var ev = e || window.event || arguments.callee.caller.arguments[0];
    
    var element = getTargetElement(ev);
    
    this._lastClicked = element;
}

LeavePageControl.prototype.AddSkipID = function AddSkipID(id) {
    this._skipIDs[this._skipIDs.length] = id;
}

LeavePageControl.prototype.GetPropertiesValues = function GetPropertiesValues(a) {
    var msg = '';
    for (var prop in a) {
        msg+= prop+'\t'+ eval('a.'+prop) +'\n';
    }
    return msg;
}

LeavePageControl.prototype.SetDebugText = function SetDebugText(txt) {
    document.getElementById('aTmp').innerText = txt;
}

LeavePageControl.prototype.Activate = function Activate(from) {
    this._isActive = true;
  //  alert('Activate '+ from);
}

LeavePageControl.prototype.Disable = function Disable(from) {
    window.setTimeout(delegate(this, function() {
        // TODO: add another boolean that will indicate
        // if the disabling should occur..
        this._isActive = false;
       // alert('Disabled '+from);
    }), "1500");
}


