/* 
	Standard JavaScript Library 1.0 alpha
	Last Modified: November 16, 2006
*/


/*	{{{
	Math & Logial
	}}} */

/*
	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));
}








/*	{{{
	Data Types
	}}} */

function Is_Num(pData)
{
	return !isNaN(pData);
}//endfun



function Is_Float(pNum)
{
	pNum = parseFloat(pNum);
	if(isNaN(pNum)) {return false;}
	if(pNum%1 == 0)
	{
		return false;
	} else {
		return true;
	}
}//endfun



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;
	}
}//endfun



function Num_To_String(pNum)
{
	return '' + pNum;
}//endfun










/*	{{{
	Strings
	}}} */


// 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;
}//endfun



// 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;
}//endfun



// 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;
}//endfun



// 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;
}//endfun











/*	{{{
	Miscellaneous
	}}} */


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;
}//endfun











/*	{{{
	Forms
	}}} */


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);
	}
}//endfun



function Confirm_Delete()
{
	var jAns = confirm("Are you sure you wish to delete this item?");
	if(jAns) {return true;}
	return false;
}//endfun



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;
}//endfun

function Paranoid_Confirm_Delete()
{
	var jAns = confirm("Are you sure you wish to delete this item?");
	if(jAns==true) 
	{
		if(confirm("Are you really, really sure you wish to delete this item?"))
		{
			var jAns2 = confirm("Click 'Ok' to delete the item, or 'Cancel' to abort.");
			if(jAns2) {return true;}
			
		} else {
			return false;
		}
	}
	return false;
}//endfun



function Magic_Field_Text(pField, pText)
{
	if(pField.value == pText)
	{
		pField.value = '';
	} else if (pField.value == '') {
		pField.value = pText;
	}
}//endfun







/*	{{{
	URLs
	}}} */


function Get_Url_Param(name)
{
	var regexS 	= "[\\?&]"+name+"=([^&#]*)";
	var regex 	= new RegExp(regexS);
	var tmpURL	= window.location.href;
	var results = regex.exec(tmpURL);
	
	if(results==null) 
	{
		
		return "";
	} else {
		return results[1];
	}
}//endfun

