/*
	Standard JavaScript Library 0.1 alpha
	Last Modified: December 14, 2004
*/

///////////////////////////////////////////////
//////////////// MATH & LOGICAL ///////////////

/*
	Returns a random integer within the absolute magnitude
	of pMax. Essentially returns random integer between
	0 and pMax-1.
*/
function Rand_Abs(pMax)
{
	return parseInt(Math.random()*(pMax));
}


//Returns a random integer between 1 and pMax.
function Rand_Int(pMax)
{
	return parseInt(Math.random()*(pMax+1));
}

///////////////////////////////////////////////
////////////////// ARRAY //////////////////////
///////////////////////////////////////////////


function Find_In(pArray, pItem)
{
	var jLength = pArray.length;
	for(var i=0; i<jLength; i++)
	{
		if(pArray[i] == pItem) {return i;}
	}
	return -1;
}

////////////////////////////////////////////////
///////////////// MATH & NUMBERS ///////////////
////////////////////////////////////////////////

// checks if pData is a number
function Is_Num(pData)
{
	return !isNaN(pData);
}

// checks if pNum is float. Returns boolean.
function Is_Float(pNum)
{
	pNum = parseFloat(pNum);
	if(isNaN(pNum)) {return false;}
	if(pNum%1 == 0)
	{
		return false;
	} else {
		return true;
	}
}

// checks if pNum is integer. Returns boolean.
function Is_Integer(pNum)
{
	pNum = parseFloat(pNum);   // we must use parseFloat() or we risk false/positives by converting floats to integers.
	if(isNaN(pNum)) {return false;}
	if(pNum%1 != 0)
	{
		return false;
	} else {
		return true;
	}
}

function Luhn_10(pNum)
{
	// clean the string a little first just to be safe
	pNum      = Num_To_String(pNum);      // number in string format
	pNum      = Remove_Non_Numeric(pNum); // remove any characters that are not numbers
	pNum      = Remove_Char(pNum, "-");   // remove negative sign
	pNum      = Remove_Char(pNum, ".");   // remove decimals
	var jLen  = pNum.length;              // number length
	var jCsum = 0;                        // will contain checksum value by end of algorithm
	var jNum;

	for(var i=0; i<jLen; i++)	// step 1, double alternating digits (except the last)
	{
		i%2 ? jNum=parseInt(pNum[i]) : jNum=parseInt(pNum[i])*2;
		if(jNum>9) {jNum = parseInt(1+(jNum%10));}	// step 2, reduce double digit numbers by adding together
		jCsum+=jNum;
	}
	return jCsum%10;	//step 3, mod 10 and return
}

///////////////////////////////////////////////
////////////////// STRING /////////////////////
///////////////////////////////////////////////

// casts a number data type to a string data type
function Num_To_String(pNum)
{
	return '' + pNum;
}

// searches and removes specified char 'pChar' from string 'pString'.
// returns the cleaned string
function Remove_Char(pString, pChar)
{
   var jClean = '';
   for(var i=0; i<pString.length; i++)
   {
      if(pString.charAt(i) != pChar) {jClean += pString.charAt(i);}
   }
   return jClean;
}

// removes blank spaces from string 'pSting' and returns it
function Remove_Spaces(pString)
{
   var jClean = '';
   for(var i=0; i<pString.length; i++)
   {
      if(pString.charAt(i) != ' ') {jClean += pString.charAt(i);}
   }
   return jClean;
}

// smart check for empty string. Look at both length and for real chars.
function Is_Empty_String(pString)
{
	// returns 0 if empty string, otherwise returns true
	//if(pString.length == 0) {return 0;}

	var jLength = pString.length;
	for(var i=0; i<jLength; i++)
	{
		if(pString[i] != " ") {return false;}
	}
	return true;
}

// removes non-numeric characters from a string. Leaves '-' and '.' for negatives and floats.
function Remove_Non_Numeric(pString)
{
	pString = Num_To_String(pString)
	var jLegal = "01234567890.-";
	var jClean = "";
	for(var i=0; i<pString.length; i++)
	{
		if(jLegal.indexOf(pString[i]) != -1) {jClean += pString[i];}
	}
	return jClean;
}

///////////////////////////////////////////////
////////////////// WINDOW /////////////////////
///////////////////////////////////////////////

/*

	The HTML below is compatible for both IE and Firefox and is recommended for calling the NewWindow function.
	//<a href="somepage.html" onClick="NewWindow(this.href,'WindowNameHere','440','320','no','no','no','no');return false;">LinkTextHere</a>
*/

function New_Window(pUrl, pName, pHeight, pWidth, pMenu, pScroll, pResize)
{
	if(!pMenu) {pMenu=1;}
	if(!pScroll) {pScroll=1;}
	if(!pResize) {pResize=1;}
	if(!pMenu) {pMenu=0;}
	var jOptions = "height='" + pHeight + "'," + "width='" + pWidth + "'," + "menubar='" + pMenu + "'," + "scrollbars='" + pScroll + "'," + "resizable='" + pResize + "'";
	window.open(pUrl, pName, jOptions);
	return false;
}

///////////////////////////////////////////////
////////////////// FROMS //////////////////////
///////////////////////////////////////////////

function Limit_Chars(pObj, pMax)
{

	if(pObj.value.length>pMax)
	{
		alert('Sorry, a maximum of 132 characters is allowed in this field.')
		pObj.value = pObj.value.substr(0, 131);
	}
}

function Confirm_Delete()
{
	var jAns = confirm("Are you sure you wish to delete this item?");
	if(jAns) {return true;}
	return false;
}

function Strong_Confirm_Delete()
{
	var jAns = confirm("Are you sure you wish to delete this item?");
	if(jAns==true)
	{
		var jAns2 = confirm("Click 'Ok' to delete the item, or 'Cancel' to abort.");
		if(jAns2) {return true;}
	}
	return false;
}

///////////////////////////////////////////////
//////////////// ROLLOVERS ////////////////////
///////////////////////////////////////////////

function Button_Swap(pObj, pImg)
{
	pObj.src = pImg.src;
}