﻿//function ClientTagID(id)
//{
//    var length = document.body.childNodes.length;
//    var clientID;
//    var index;
//    var word;
//    var foundMatch = false;
//    var reg = new RegExp("^" + id + "$");
//    
//    alert(length);
//    for(var loop = 0; loop < length; loop++)
//    {
//        index = 0;
//        clientID = new String(document.body.childNodes[loop].id);
////        alert(clientID);
////        
////        index = clientID.indexOf(id);
////        word = clientID.substring(index);
////        foundMatch = reg.test(word);
////        alert("id:" + id + " cycle:" + clientID + " match:" + foundMatch);
////        if(foundMatch == true)
////            break;
//    }
//    
//    if(foundMatch == true)
//        return clientID;
//    else
//        return "0";

//}


/*
 * This function is used to retrieve a match of form controls.  Since asp.net changes the controls id
 * using this script will check for a match of the original id and then return the id that you would access
 * from calling the controls.ClientID
 *
 * Example: var document.getElementById(ClientID('TextBoxUser');
 */
function ClientID(id)
{
    var elements = document.forms[0].elements;
    var clientID;
    var index;
    var word;
    var foundMatch = false;
    var reg = new RegExp("^" + id + "$");
    
    for( var i=0; i < elements.length; i++)
    {
        index = 0;
        clientID = new String(elements[i].id);
        
        index = clientID.indexOf(id);
        word = clientID.substring(index);
        foundMatch = reg.test(word);
        //alert("id:" + id + " cycle:" + clientID + " match:" + foundMatch);
        if(foundMatch == true)
            break;
        
        
        /*
        index = clientID.indexOf(id);
        if(index != -1)
        {
            foundMatch = true;
            break;
        }
        */
    }
    
    if(foundMatch == true)
        return clientID;
    else
        return "0";

}

function ClientControl(id)
{
    var control = ClientID(id);
    
    return document.getElementById(control);
}



/*
 * This function is used to create a countdown then it will redirect
 * Usage: window.setTimeout("ExecuteCountDown('id', 'url')",100);
 * id : the control that will display the countdown
 * url: the url to redirect to when the navigation is done
 *
 * Example: window.setTimeout("ExecuteCountDown('clock', 'http://www.google.com', 'You will be redirected in', 'seconds')",100);
 */
var countDownStartDate = new Date();
countDownStartDate = Date.parse(countDownStartDate)/1000;
var countDownCounts = 15;
	

function ExecuteCountDown(id, url, start_message, end_message)
{
	var now = new Date();
	now = Date.parse(now) / 1000;
	var x = parseInt( countDownCounts - (now - countDownStartDate), 10);
	
	document.getElementById(id).innerHTML = start_message + x + end_message;
	
	if(x>0)
	{
		timerID = setTimeout("ExecuteCountDown('" + id + "', '" + url + "','" + start_message + "','" + end_message + "')", 100)
	}
	else
	{
		location.href = url;
	}
}



/*
 * This function is used to find the controls x position of the object
 */
function FindXPosition(obj)
{
  var curleft = 0;
  if(obj.offsetParent)
    while(1) 
    {
        curleft += obj.offsetLeft;
        if(!obj.offsetParent)
          break;
        
        obj = obj.offsetParent;
    }
    else if(obj.x)
      curleft += obj.x;
    
  return curleft;
}

/*
 * This function is used to find the y position of the object
 */
function FindYPosition(obj)
{
  var curtop = 0;
    
  if(obj.offsetParent)
    while(1)
    {
      curtop += obj.offsetTop;
      if(!obj.offsetParent)
        break;
          
      obj = obj.offsetParent;
    }
  else if(obj.y)
    curtop += obj.y;
    
  return curtop;
}


function FindRealPosition(obj) 
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function X(obj) {
    var x = obj.offsetLeft
    while (obj = obj.offsetParent) x += obj.offsetLeft
    return x
} 
    
function getPosition(el) 
{
    var x,w,y,h;

    if (document.getBoxObjectFor) 
    {
        var bo = document.getBoxObjectFor(el);
        x = bo.x;
        w = bo.width;
        y = bo.y;
        h = bo.height;
    }
    else if (el.getBoundingClientRect) 
    {
        var rect = el.getBoundingClientRect();
        x = rect.left;
        w = rect.right - rect.left;
        y = rect.top;
        h = rect.bottom - rect.top;
    } 

    el.x = x;
    el.y=y;
    el.w= w;
    el.h = h;
//alert("Left: " + x + "\rTop: " + y + "\rWidth: " + w + "\rHeight: " + h);
    return [x,y,w,h];
}


function hideElement (elementId) 
{
    var element;
    
        if (document.all)
            element = document.all[elementId];
        else if (document.getElementById)
            element = document.getElementById(elementId);
        
        if (element && element.style)
            element.style.display = 'none';
}

function showElement (elementId) 
{
    var element;
    
    if (document.all)
        element = document.all[elementId];
    else if (document.getElementById)
        element = document.getElementById(elementId);

    if (element && element.style)
        element.style.display = '';
}

function setStyle( object, styleText) 
{
    object.style.cssText = styleText
}


function clearListControl(object)
{
    for(x=object.length; x>=0; x--)
    {
        object[x] = null;
    }
        
}

function delay(millis)
{
    var date = new Date();
    var curDate = null;

    do 
    { 
        curDate = new Date(); 
    }
    while(curDate-date < millis);
} 


function setSelectedListIndex(control, value)
{
    var buff;
    var index = 0;
    var test = "";
    
    test += value;
    for(var x = 0; x < control.length; x++)
    {
        buff = control[x].value;
        if(buff.toLowerCase() == test.toLowerCase())
        {
           index = x;
           break; 
        }
    }
    
    control.selectedIndex = index;
    
}