﻿// Global variables.
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
var DocWindow;

function newImage( imgUrl )
{
	var img = new Image();
	img.src = imgUrl;
	return img;
}

function changeImages(anchor,imgUrl)
{
    anchor.getElementsByTagName("img")[0].src = imgUrl;
}


function ToggleAll( elementId, display )
{
    var elements
    elements = document.getElementsByName(elementId);
    alert(elements);
    alert(elements.length);
    for( var i = 0; i < elements.length; i++ )
    {
        elements[i].style.display = display;
    }
}


function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
	input.value = input.value.slice(0, len);
	input.form[(getIndex(input)+1) % input.form.length].focus();
}

function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
        if(arr[index] == ele)
            found = true;
        else
            index++;
        return found;
    }
    return true;
}

function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
        if (input.form[i] == input)index = i;
        else i++;
    return index;
}

    /**************************************************************
	** Function to validate the phone number fields.
	** Used with CustomControl validator.
	**
	** Created by:      Gene Ernst
	** Last modified:	1/17/2007										
	***************************************************************/
function CheckPhone(sender, args)
{
    // Define and assign values to variables.
    var strAreaCode = document.forms[0].txtAreaCode.value;
    var strExchange = document.forms[0].txtExchange.value;
    var strNumber = document.forms[0].txtNumber.value;
    
    // Area code value
    if (strAreaCode == "")
        {
            args.IsValid = false;
            return;  
        }
    else if (IsNumeric(strAreaCode) == false)
        {
            args.IsValid = false;
            return;
        }
    else if (strAreaCode.length != 3)
        {
            args.IsValid = false;
            return;
        }        
    
    // Exchange value
    if (strExchange == "")
        {
            args.IsValid = false;
            return;  
        }
    else if (IsNumeric(strExchange) == false)
        {
            args.IsValid = false;
            return;
        }
    else if (strExchange.length != 3)
        {
            args.IsValid = false;
            return;
        }              
    
    // Number value
    if (strNumber == "")
        {
            args.IsValid = false;
            return;  
        }
    else if (IsNumeric(strNumber) == false)
        {
            args.IsValid = false;
            return;
        }
    else if (strNumber.length != 4)
        {
            args.IsValid = false;
            return;
        }             
        
    args.IsValid = true;                
}
    
    /**************************************************************
	** Function to test for numeric value.	
	** Returns false if value passed is not a numeric value
	** 
	** Created by:      Gene Ernst
	** Last modified:	1/17/2007										
	***************************************************************/
function IsNumeric(strString)   
{
    var strValidChars = "0123456789.-";
    var strChar;
    var blnResult = true;

    if (strString.length == 0) return false;

    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length && blnResult == true; i++)
        {
            strChar = strString.charAt(i);
            if (strValidChars.indexOf(strChar) == -1)
            {
                blnResult = false;
            }
        }
   return blnResult;
}

    /**************************************************************
	** Function used to open a new window.	
	**
	** Created by:      Gene Ernst
	** Last modified:	1/17/2007										
	***************************************************************/	
function OpenWindow(strURL, strWidth, strHeight) 
{	
	if (DocWindow && !DocWindow.closed)
		{
		    DocWindow.close();
		}
	
	DocWindow = window.open(strURL,"DocWindow","width="+strWidth+",height="+strHeight+",left=200,top=150,status=no,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=no");
	DocWindow.focus;  
}

    /**************************************************************
	** Function to close any open windows
	**
	** Created by:      Gene Ernst
	** Last modified:	1/17/2007								 						
	***************************************************************/		
function UnloadWindow() 
{
	{
	if (DocWindow && !DocWindow.closed)
		{
		DocWindow.close();
		}
	}
}	       		

/* Copied from WDC */
function addEvent(context, eventName, handler){
    if(context) {
        //strip off 'on' prefix for use with addEventListener
        if(eventName.substring(0, 2) == "on") {
            eventName = eventName.substring(2,eventName.length);
        }
        if (context.addEventListener){
            context.addEventListener(eventName, handler, false);
        } 
        else if (context.attachEvent){
            context.attachEvent("on"+eventName, handler);
        }
    }
}
