﻿var xmlhttp = null;
var xmlhttpCallback = null;
/*
function getXMLdoc(url, callbackFunc)
{
    if (xmlhttp != null)
    {
        xmlhttp.abort();
    }
    
    xmlhttp = null;
    xmlhttpCallback = '';
    
    if (window.ActiveXObject)
    {
        xmlhttp = new ActiveXObject("microsoft.xmlhttp");
    }
    else
    {
        xmlhttp = new XMLHttpRequest();
    }    
    
    if (xmlhttp != null)
    {
        xmlhttpCallback = callbackFunc;
        xmlhttp.onreadystatechange = xmlStateChanged;
        xmlhttp.open("GET",url,true);			
		xmlhttp.send();
    }
}

function xmlStateChanged()
{
    try
    {
        if (xmlhttp != null)
        {
            if (xmlhttp.readyState == 4)
            {
                if (xmlhttp.status == 200)
                {
                    var exec = xmlhttpCallback + "(xmlhttp.responseText)";
                    xmlhttpCallback = '';
                    eval(exec);
                }
            }
        }
    }
    catch(e)
    {
    }
}
*/

var ajaxError = function(t)
{
    alert('Error: ' + t.status + ', ' + t.statusText);
}

function getXMLdoc(url, callbackFunc)
{
    new Ajax.Request(url, {onSuccess:callbackFunc, onFailure:ajaxError});
}

function postXMLdoc(url, prms, callbackFunc)
{
    new Ajax.Request(url, {method:'post', parameters: prms, onSuccess:callbackFunc, onFailure:ajaxError});
}

// mozXPath [http://km0ti0n.blunted.co.uk/mozxpath/] km0ti0n@gmail.com
// Code licensed under Creative Commons Attribution-ShareAlike License 
// http://creativecommons.org/licenses/by-sa/2.5/
if( document.implementation.hasFeature("XPath", "3.0") )
{
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++)
		{
			aResult[i] =  aItems.snapshotItem(i);
		}
		
		return aResult;
	}
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; } 

		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 )
		{
			return xItems[0];
		}
		else
		{
			return null;
		}
	}

	Element.prototype.selectNodes = function(cXPathString)
	{
		if(this.ownerDocument.selectNodes)
		{
			return this.ownerDocument.selectNodes(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

	Element.prototype.selectSingleNode = function(cXPathString)
	{	
		if(this.ownerDocument.selectSingleNode)
		{
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

}

function resizeImage(img, maxX, maxY)
{
    //img1 = new Image();
    //img1.src = 'http://localhost:77/Images/Pics/11.jpg';
    //img1.onload = alert(img1.width);  
    
    var elem = img;
    
    if (elem == undefined || elem == null) 
    {
    return false;    
    }
    if (maxY == undefined) maxY = maxX;
    
    elem.style.display = 'inline';
    
    if (elem.width == 0 || elem.height == 0)
    {
        elem.width = maxX;
        elem.height = maxY;
    }

    if ((elem.width/maxX) > (elem.height/maxY))
    {
        elem.width = maxX;
            
        elem.style.marginTop =((maxY-elem.height)/2) + 'px';
    }
    else
    {
        elem.height = maxY;
    }    
    
    elem.style.display = 'none';    
    elem.style.display = 'block';
}

function generateRandomParameter()
{
    return "&" + Math.floor(Math.random()*10000000);
}

function isIE()
{
    return (navigator.appName=="Microsoft Internet Explorer");
}



////////////////////////////Image Resizer//////////////////////////////////////


function ImagePreloader(imageUrl, imageObj, maxWidth, maxHeight)
{
   // store the callback
   this.callback = setSize;

   // initialize internal state.
   this.nLoaded = 0;
   this.nProcessed = 0;
   this.aImages = new Array(); 
   this.maxHeight = maxHeight;
   this.maxWidth = maxWidth;
   this.imageObj = imageObj;

   // record the number of images.
   this.nImages = 1;

   this.preload(imageUrl);
}


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, this.maxWidth, this.maxHeight, this.imageObj);
   }
}

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();
}

function setSize(){
  var maxWidth = arguments[2];
  var maxHeight = arguments[3];
  var imgObject = arguments[4];

  var img = arguments[0][0];

  var newWidth = img.width;
  var newHeight = img.height;

  if ((newWidth/maxWidth) > (newHeight/maxHeight))
  {
     newHeight = newHeight * (maxWidth/newWidth);
     newWidth = maxWidth;
  }
  else
  {
     newWidth = newWidth * (maxHeight/newHeight);
     newHeight = maxHeight;
  }  

  document.getElementById(imgObject).style.width = newWidth + "px";
  document.getElementById(imgObject).style.height = newHeight + "px"
  document.getElementById(imgObject).src = img.src;
  document.getElementById(imgObject).style.display = 'block';
  document.getElementById(imgObject).style.marginTop =((maxHeight-newHeight)/2) + 'px';
  
}

function SetDropDownListValue(ddl,value)
{
    for (i=0; i<ddl.options.length; i++)
    {
        ddl.options[i].selected = (ddl.options[i].value == value);
    }
}

function is_child_of(parent, child)
{
	if( child != null ) {			
		while( child.parentNode ) {
			if( (child = child.parentNode) == parent ) {
				return true;
			}
		}
	}
	return false;
}

function fixOnMouseOut(element, event, JavaScript_code)
{
	var current_mouse_target = null;
	if( event.toElement ) {				
		current_mouse_target 			 = event.toElement;
	} else if( event.relatedTarget ) {				
		current_mouse_target 			 = event.relatedTarget;
	}
	if( !is_child_of(element, current_mouse_target) && element != current_mouse_target ) {
		eval(JavaScript_code);
	}
}

function disablePageContents(disable)
{
    $('divDisableMainPageContents').style.display = disable ? 'block' : 'none';
}

function bookmarkSite()
{
    title = "Copenda.com";
    url = "http://www.copenda.com/";
    if (window.sidebar) // firefox
	    window.sidebar.addPanel(title, url, "");
    else if(window.opera && window.print){ // opera
	    var elem = document.createElement('a');
	    elem.setAttribute('href',url);
	    elem.setAttribute('title',title);
	    elem.setAttribute('rel','sidebar');
	    elem.click();
    } 
    else if(document.all)// ie
	    window.external.AddFavorite(url, title);
}

function getAttributeValue(xmlNode, name)
{
    for (i=0; i<xmlNode.attributes.length; i++)
    {
        if (xmlNode.attributes[i].name == name)
        {
            return xmlNode.attributes[i].value;
        }
    }
    
    return null;
}