﻿//=============================================================================
/**
* Get query string paramter and return the value... 
*
* @param	 sParamName - Parameter Name...
*
* @return	 Parameter's Value...
*
* howToUse - getQueryVariable("userMail")
*
* TODO: ...
*/        
//=============================================================================
function getQueryVariable(sParamName) 
{
    var sParamValue = null;
  
    var query = window.location.search.substring(1);    
    var vars = query.split("&");
    for(var i = 0; i < vars.length; i++) 
    {
	    var pair = vars[i].split("=");
         if(pair[0] == sParamName) 
         {
	         sParamValue = pair[1];
              break;
         }
    } 
    return sParamValue;
}
//=============================================================================
/**
* ... 
*
* @param	 ...
*
* @return	 ...
*
* howToUse - 
*
* TODO: ...
*/        
//=============================================================================function $(eleName) 
{
    if(document.getElementById && document.getElementById(eleName)) 
    {
         return document.getElementById(eleName);
    }
    else if(document.all && document.all(eleName)) 
    {
         return document.all(eleName);
    }
    else if(document.layers && document.layers[eleName]) 
    {
         return document.layers[eleName];
    } 
    else 
    {
         return null;
    }
}
//=============================================================================
function changeVisibility(objRef, setVisibleFlag)
{   
    objRef.style.display = setVisibleFlag ? "" : "none";    
}
//=============================================================================
function getValueByText(dropDownList, textToFind)
{
    var tempVal = ""; 
    for(i = 0; i < dropDownList.options.length; i++)
    {
         if(dropDownList.options[i].text == textToFind)
         {
              tempVal = dropDownList.options[i].value;
              break;
         }
    }
    return tempVal;
}
//=============================================================================
function addRangeToSelect(oSelectID, from , to)
{
    for(i = from; i <= to; i++)
    {
         var oOption = document.createElement("OPTION"); 
         oSelectID.options.add(oOption);        
         if(i < 10)
         {
              oOption.innerText = "0" + i;
              oOption.value     = "0" + i;
         }
         else
         {
              oOption.innerText = i;
              oOption.value     = i;                            
         }  
    }
}
//=============================================================================
function getElementPosition(element) 
{
    var result = new Object();
    result.x = 0;
    result.y = 0;
    result.width = 0;
    result.height = 0;
    if(element.offsetParent) 
    {
         result.x = element.offsetLeft;
         result.y = element.offsetTop;
         var parent = element.offsetParent;
         while(parent) 
         {
              result.x += parent.offsetLeft;
              result.y += parent.offsetTop;
              var parentTagName = parent.tagName.toLowerCase();
              if(parentTagName != "table" && parentTagName != "body" && parentTagName != "html" && parentTagName != "div" && parent.clientTop && parent.clientLeft) 
              {
                   result.x += parent.clientLeft;
                   result.y += parent.clientTop;
              }
              parent = parent.offsetParent;
         }
    }
    else if(element.left && element.top) 
    {
         result.x = element.left;
         result.y = element.top;
    }
    else 
    {
         if(element.x) 
         {
              result.x = element.x;
         }
         if(element.y) 
         {
              result.y = element.y;
         }
    }
    if(element.offsetWidth && element.offsetHeight) 
    {
         result.width  = element.offsetWidth;
         result.height = element.offsetHeight;
    }
    else if(element.style && element.style.pixelWidth && element.style.pixelHeight) 
    {
         result.width  = element.style.pixelWidth;
         result.height = element.style.pixelHeight;
    }
    return result;
}
//=============================================================================
//escape(key)
function urlEncode(strUrl)
{
	var strSpecialUrl = " <>\"#%{}|^~[]`'&?+=";
	var strEncode = "";
	var i;
	var chUrl;
	var iCode;
	strUrl += "";
	for (i=0; i<strUrl.length; i++)
	{
		chUrl=strUrl.charAt(i);
		iCode=chUrl.charCodeAt(0);
		if(iCode <= parseInt(0x7F))
		{
			if (strSpecialUrl.indexOf(chUrl) != -1)
			{
				strEncode += "%" + iCode.toString(16).toUpperCase();
			}
			else
			{
				strEncode += chUrl;
			}
		}
	}
	return strEncode;
}
//=============================================================================
Array.prototype.IndexOf = function(value) 
{ 
    var i = 0;
    for(var inx in this) 
    { 
         if(this[inx] == value)
         {
              return i; 
         } 
         i++;                    
    } 
    return -1; 
};
//=============================================================================
if(!Array.prototype.remove) 
{
    Array.prototype.remove = function(value) 
    {
         var i = this.IndexOf(value);
         if(i != -1)
         {
              this.splice(i, 1);
         }
    };
}
//=============================================================================
//New window in the middle of the screen...
function popupWindowCenter(mypage, myname, w, h, scroll) 
{
    var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    winprops = 'height=' + h + ',width=' + w + ',top=' + wint + ',left=' + winl + ',scrollbars=' + scroll + ',resizable';
    win = window.open(mypage, myname, winprops)
    if(parseInt(navigator.appVersion) >= 4) 
    { 
         win.window.focus(); 
    }
}
//=============================================================================
function addSiteToFavorite()
{
    var title = "title";
    var url = "http://www.trekinu.com";
    if(window.sidebar) 
    {
         window.sidebar.addPanel(title, url, "");
    }	
    else if(window.external)
    {
         window.external.AddFavorite(url, title); 
    }
} 
//=============================================================================
function urlFileName(url)
{ 
    return url.substring(url.lastIndexOf('/') + 1);
} 
//=============================================================================
function enforceLoginByResponse(xmlMessage)
{
    if(xmlMessage.trim() != "<Result><Status>Error</Status><Description>LoginRequired</Description></Result>") 
        return false;

    document.location.href = "/memberLogin.aspx?ReturnUrl=" + urlFileName(document.location.href);
    return true;
}
//=============================================================================
function getTotalRecords(xml)
{
    var totalRecords;
    
    try 
    { 
        sTotalRecords = xml.getTagValue("totalRecords", 0, 0);
        if(sTotalRecords == "")
        {
            sTotalRecords = "0";
        }
        totalRecords = parseInt(sTotalRecords); 
    }
    catch(e)
    { 
        totalRecords = 0; 
    } 
    return totalRecords;
}
//=============================================================================
function handleErrorXml(xmlParser)
{
    if(null == xmlParser)
    {
        showPopWin('Error', 'generalmessage', 'xml parser is null', '', '', 250, 100, null);
        return;
    }
        
    var rc = xmlParser.getTagValue("Status", 0, 0);
    
    //this is not en xml error
    if("Error" != rc)
    {
        return;
    }
    
    var errDesc = xmlParser.getTagValue("Description", 0, 0);    
    showPopWin("Error" , 'generalmessage', 'generalerror', escape(errDesc), '', 250, 100, null);
}
//=============================================================================
function getScrollYPosition()
{
    var YPosition = 0;
    if(self.pageYOffset) 
    {
         YPosition = self.pageYOffset;
    } 
    else if(document.documentElement && document.documentElement.scrollTop)
    {
	    YPosition = document.documentElement.scrollTop; 
    } 
    else if(document.body) 
    {
	    YPosition = document.body.scrollTop;
    }
    return YPosition;
}
//=============================================================================
function setScrollVisible(height, overflow)
{
    return;

    var bod            = document.getElementsByTagName('body')[0];
    var htm            = document.getElementsByTagName('html')[0];
        
    bod.style.height   = htm.style.height = height;
    bod.style.overflow = htm.style.overflow = overflow; 
}
//=============================================================================
function getBodyHeight()
{
    var htm            = document.getElementsByTagName('html')[0];
    return  htm.scrollHeight;
}
//=============================================================================
