﻿
// This function places the cursor inside the first textbox of the first form it finds.
// DEPRECATED: Use the .Net 2.0 Focus API instead!
function FocusOnFirstTextBox()
{
    var firstForm;      // the first form on the page
    var currentElement; // the current element as we loop through the form
   
    firstForm = window.document.forms[0]; // get a handle to the first form
    
    if(firstForm != null) // if we found a form
    {
        for (i=0;i<firstForm.length;i++) // loop through the form elements
        {
            currentElement = firstForm.elements[i]; // get a handle to the current element
            if(currentElement.type == "text") // if it's a text box
            {
                currentElement.focus(); // place the cursor inside
                break; // break out of the loop
            }
        }
    }
}

// This function returns the number of non alphanumeric characters in a given string
// it is the javascript equivalent of the Cboc.Website2005.Resource.Utility method of the same name.
function NonAlphaNumericCharacterCount(input)
{
    var matches;    // an array of regex matches
    var matchCount; // the number of matches found
    
    matches = input.match(/[\W]/g); // get an array of non alpha characters found in the string
    matchCount = matches.length; // count the elements in the array
    
    return matchCount;
}

// Determines whether it is morning, afternoon or evening in the client's local time.
function GetPortionOfDay()
{
    var currentTime;
    var portion;
    
    currentTime = new Date();
    if (currentTime.getHours() < 12)
	{
	    portion = "morning";
	}
    else if (currentTime.getHours() < 17)
	{
	    portion = "afternoon";
	}
    else
    {
        portion = "evening";
    }
    return portion;
}

// enables highlighting of text fields on focus.
function EnableHighlighting(highlight,normal)
{
    
    var f = document.forms[0];
    for (i=0;i<f.length;i++)
    {
        element = f.elements[i];
        if(element.focus)
        {
            element.onfocus = function () {this.style.backgroundColor = highlight;}
            alert(normal);
            element.onblur = function () {this.style.backgroundColor = normal;}
        }
     } 
 }  
 
 // Removes non numeric characters from an input field
function StripNonNumeric(source)
{
    if(source.value)
    {
        source.value = source.value.replace(/[^0-9]/g,"");
    }
}


function Mask(textbox,loc,delim)
{
    var locs = loc.split(',');
    var str = textbox.value;
    for (var i = 0; i <= locs.length; i++)
    {
	    for (var k = 0; k <= str.length; k++)
	    {
	        if (k == locs[i])
	        {
	            if (str.substring(k, k+1) != delim)
	            {
	                str = str.substring(0,k) + delim + str.substring(k,str.length)
	            }
	        }
	    }
    }   
    textbox.value = str
}

function MakeValueAPhoneNumber(source)
{
    StripNonNumeric(source);
    Mask(source,'3,7','-');
    return true;
}

function ClearInitialValue(source,initial)
{
    if(source.value = initial)
    {
        source.value = '';
    }
}

// Remove all leading and trailing spaces from a given string
function Trim(input)
{
    var output;
    output = RTrim(LTrim(input));
    return output;
}

// Remove all trailing spaces from a given string
function RTrim(input)
{
    var output;
    output = input;
    while(output.charAt((output.length -1))==" ")
    {
        output = output.substring(0,output.length-1);
    }
    return output;
}

// Remove all leading spaces from a given string
function LTrim(input)
{
    var output;
    output = input;
    while(output.charAt(0)==" ")
    {
        output = output.replace(output.charAt(0),"");
    }
    return output;
}

// V 1.0
//Force open a new window
function winBRopen(theURL) {

winProp ='width='+screen.width +',height='+screen.height+',left=0,top=0,scrollbars=yes,';

Win = window.open(theURL, document.title, winProp);

//if (parseInt(navigator.appVersion) >= 4){
//Win.window.focus();
//}

}
