/*****
Name: /javascript/global.js
Author: Jason Napsky
Creation Date: Nov. 11, 2003
Description: Common JS functions are stored here.

Update History:
	Developer:	Jason Napsky (ejn022)
	Date:		02/08/2001
	Description:
*****/

/*
	PURPOSE: Remove leading blanks from our string.
	IN: str - the string we want to LTrim
*/
function LTrim(str)
{
	var whitespace = new String(" \t\n\r");
	var s = new String(str);
	
	if (whitespace.indexOf(s.charAt(0)) != -1)
	{
		// We have a string with leading blank(s)...
		var j=0, i = s.length;
		// Iterate from the far left of string until we don't have any more whitespace...
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;
		// Get the substring from the first non-whitespace character to the end of the string...
		s = s.substring(j, i);
	}
	return s;
}

/*
	PURPOSE: Remove trailing blanks from our string.
	IN: str - the string we want to RTrim
*/
function RTrim(str)
{
        // We don't want to trip JUST spaces, but also tabs, line feeds, etc.  
		// Add anything else you want to "trim" here in Whitespace
        var whitespace = new String(" \t\n\r");
        var s = new String(str);

        if (whitespace.indexOf(s.charAt(s.length-1)) != -1) 
		{
            var i = s.length - 1;       // Get length of string
            // Iterate from the far right of string until we don't have any more whitespace...
            while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
                i--;
            // Get the substring from the front of the string to where the last non-whitespace character is...
            s = s.substring(0, i+1);
        }
        return s;
}

/*
	PURPOSE: Remove trailing and leading blanks from our string.
	IN: str - the string we want to Trim

	RETVAL: A Trimmed string!
*/
function Trim(str)
{
        return RTrim(LTrim(str));
}

// Go to next field after typing 'len' characters.
function goNext(f1, f2, len)
{
	if (f1.value.length == 3) f2.focus();
}

// Return true if value is a numeric.
function IsNumeric(val)
{
	var filter = /\D/;
	if (filter.test(val))
	{
		return(false);
	}
	return(true);
}

//
// give the user an error message and set the focus on the invalid obj.
//
function alertAndSetFocus(obj,msg)
{
	if (msg != '')
	{
		alert(msg);
	}
	obj.focus();
	obj.select();
}

function closeWindow(theURL,winName,features)
{
	window.close(theURL,winName,features);
}

function openWin(winURL,winName,features)
{
	window.open(winURL,winName,features);
}

function openPopup(winURL)
{
	var winWidth=424;
	var winHeight=170;
	var winTop=0;
	var winLeft=0;
	winLeft=Math.floor((Math.abs(screen.availWidth-winWidth))/2);
	winTop=Math.floor((Math.abs(screen.availHeight-winHeight))/2);
	

	strWin=window.open(winURL,'popup','top='+ winTop + ',left=' + winLeft + ',width=' + winWidth + ',height=' + winHeight + ',toolbar=no menubar=no,location=no,directories=no,status=no,resizable=no,scrollbars=no');
	strWin.focus();
}

//
// Performs autotab for phone number fields
//
var phone_field_length = 0;

function TabNext( obj, length, next_field ) 
{
	phone_field_length = obj.value.length;
	
	if ( phone_field_length == length ) 
	{
		next_field.focus();
	}
}

function isValidForm() 
{
	var f = document.AssignItemsForm;
	if ( !isValidLicenseCode(f.licenseNumber) || !isValidPhone(f.phoneAreaCode,f.phonePrefix,f.phoneSuffix) || !isValidIMEI(f.imei) )
	{
		return false;
	}
	else if ( !f.eula.checked ) 
	{
		alert('You must check the Terms of Sale box in order to proceed with your purchase.')
		return false;
	}
	return true;
}

function setDefault(e,strDefault)
{
	if (e.value=="") e.value=strDefault;
}
function clearDefault(e,strDefault)
{
	if (e.value==strDefault) e.value="";
}

function setCharCount(e, maxCharCount)
{
	var l = e.value.length;
	if( l > maxCharCount ) 
	{
		e.value=e.value.substring(0,maxCharCount);
		l=maxCharCount;
	}
	e.form.elements["charCount"].value=l;
	return l;
}
function trapKeyPress(e, maxCharCount)
{
	if( setCharCount(e) >= maxCharCount )
	{
		event.returnValue=false;
	}
}
function trapKeyDown(e, maxCharCount)
{
	if( setCharCount(e) >= maxCharCount )
	{
		if( event.keyCode!=8 && event.keyCode!=37 && event.keyCode!=39 && event.keyCode!=38 && event.keyCode!=40 && event.keyCode!=46 )
		{
			event.returnValue=false;
		}
	}
}

function showImage()
{
	// Set up the image files to be used.
	var theImages = new Array() // do not change this
	// To add more image files, continue with the
	// pattern below, adding to the array.
	
	theImages[0] = 'images/hp_main-image_1.gif'
	theImages[1] = 'images/hp_main-image_2.gif'
	theImages[2] = 'images/hp_main-image_3.gif'
	theImages[3] = 'images/hp_main-image_4.gif'
	theImages[4] = 'images/hp_main-image_5.gif'
	theImages[5] = 'images/hp_main-image_6.gif'
	theImages[6] = 'images/hp_main-image_7.gif'
	
	// do not edit anything below this line
	
	var j = 0
	var p = theImages.length;
	var preBuffer = new Array()
	for (i = 0; i < p; i++)
	{
	   preBuffer[i] = new Image()
	   preBuffer[i].src = theImages[i]
	}
	var whichImage = Math.round(Math.random()*(p-1));
	document.write('<img src="'+theImages[whichImage]+'" border="0" alt="">');
}

//  End -->

