// Tools.js
// written by: David Fudge [ rkstar@mac.com ]
// created on: November 15, 2008
// last modified: November 15, 2008
//
// description:
// this file contains a static class with useful functions for any
// javascript project.
var Tools = {

// number of seconds since epoch
now : function() { return Math.round(new Date().getTime() / 1000); },

// just like the PHP func...
// translates all \n chars to <br>\n
nl2br : function( string ) { return string.replace(/\n/g,"<br>\n"); },
br2nl : function( string ) {
	string = string.replace(/<br>\n/g, "\n");
	string = string.replace(/<br>/g, "\n");
	return string;
},

// just like the PHP func...
// search and replace
str_replace : function( search, replace, haystack ) { return haystack.split(search).join(replace); },

validString	: function( string ) { return (string.length > 0); },

validNumber : function( number ) { return !isNaN(number); },

// validates an email address
validEmail : function( email ) {
	var regex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	return regex.test(email);
},

// validates a phone number
validPhone : function( phone ) {
	var regex = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
	return regex.test(phone);
},

validZipCode : function( zip ) {
	var regex = /^\d{5}$|^\d{5}-\d{4}$/;
	return regex.test(zip);
},

validPostalCode : function( postalcode ) {
	var regex = /^[a-zA-Z]{1}\d{1}[a-zA-Z]{1}[ ]?\d{1}[a-zA-Z]{1}\d{1}/;
	return regex.test(postalcode);
},


// just like PHP func
// search array keys for a value
array_key_exists : function() {
    var args     = Tools.array_key_exists.arguments;
    var haystack = (args[0]) ? args[0] : false;
    var needle   = (args[1]) ? args[1] : false;
    var n;
    // sanity
    if( !haystack || !needle ) { return false; }

    // look thru the object array for a key
    for( n in haystack ) { if( n == needle ) { return true; } }
    // not found...
    return false;
},

// just like the PHP func
// print_r all vars recursively in an object/hash
print_r : function() {
	var args    = Tools.print_r.arguments;
	var thing   = (args[0]) ? args[0] : null;
	var level   = (args[1]) ? args[1] : 0;
	var padding = "";
	var output  = "";
	var i, j;
	// sanity
	if( !thing ) { return null; }
	
	// recursively dump vars into our "output" var
	// set the padding for this level
	for( i=0; i<level; i++ ) { padding += "\t"; }
	
	// check the type of vars
	if( typeof(thing) == "object" )		// "object" => array, hash, object
	{
		// get each item in this thing
		for( var item in thing )
		{
			if( typeof(thing[item]) == "object" )
			{
				// our item is an object, we'll need to send it thru
				// this function too
				if( !thing[item] ) { output += padding+"[ "+item+" ] => null\n"; }
				else
				{
					output += padding+"[ "+item+" ] => {\n";
					output += Tools.print_r( thing[item], (level + 1) );
				}
			}
			else { output += padding+"[ "+item+" ] => "+thing[item].toString().replace(/\n/g,"\n"+padding)+"\n"; }
		}
	}
	else { output += padding + thing + "("+typeof(thing)+")"; }
	
	return output;
}
};
