/************************************************************************
* Function: Functions													*
* Product: Common Site Functions										*
* Company: Fusework Studios, a Division of Rutter Communications 		*
*			Network LLC.												*
* Author: Brandon Coppernoll											*
* Date Created: August 4, 2006											*
* Date Modified: December 21, 2007										*
*																		*
* Copyright: Copyright (C) 2007 Fusework Studios, a Division of Rutter	*
*			 Communications Network LLC. 								*
*            See "fwslicense.txt" for details regarding 				*
*            licensing, usage, disclaimers, distribution and general 	*
*            copyright requirements. If you don't have a copy of this 	*
*            file, you may request one at support@fuseworkstudios.com	*
*																		*
* This object allows the creation of the xmlHttp object that will do	*
* most, if not all, AJAX related operations.							*
* ********************************************************************* *
* Modifications:														*
* ***	10/16/2006	Added print content functionality					*
* ***	11/22/2006	Added StringBuilder functionality					*
* ***   12/21/2007  Updated the isDate functionality					*
* ***   12/21/2007  Added pop-up window functionality					*
* ***   3/23/2009	Updated getRequestBody with escape() function		*
************************************************************************/
/*
* This function starts the timer and tracks the current time and changes it.
*/
function startTime()
{
	var today=new Date();
	var h=today.getHours();
	var m=today.getMinutes();
	var s=today.getSeconds();
	var tod="AM";
	// add a zero in front of numbers<10
	m=checkTime(m);
	s=checkTime(s);
	if (h==12)
		tod = "PM";
	else if (h>12)
	{
		tod = "PM";
		h = h - 12;
	}
	else if (h==0)
	{
		tod = "AM";
		h = 12;
	}
	else
		tod = "AM";
	document.getElementById("tm_clock").innerHTML=h+":"+m+":"+s+" "+tod;
	t=setTimeout("startTime()",500);
}

// adds a zero in front of numbers < 10
function checkTime(i)
{
	if (i<10) 
 	{
		i="0" + i;
	}
  	return i;
}

// string concatenation can take a long time, this loop will shorten all of that and output it properly
// so that the data can be transmitted quickly and save a lot of programming time
// * NOTE: THIS IS TO BE USED FOR POSTED FORMS. THIS CAN BE USED TO GENERATE THE QUERYSTRING. SEARCHES FORM BY ID...NOT NAME! *
function getRequestBody( oForm )
{
	var aParams = new Array();
	var elemFound = false;
	
	for (var i=0; i < oForm.elements.length; i++)
	{
		if (oForm.elements[i].type=="text"||oForm.elements[i].type=="password"||oForm.elements[i].type=="hidden")
		{
			var sParam = escape(oForm.elements[i].id);
			sParam += "=";
			sParam += escape(oForm.elements[i].value);
			aParams.push(sParam);
		}
		else if (oForm.elements[i].type=="checkbox")
		{
			if (oForm.elements[i].checked)
			{
				var sParam = escape(oForm.elements[i].id);
				sParam += "=";
				sParam += escape(oForm.elements[i].value);
				aParams.push(sParam);
			}
			else
			{
				var sParam = escape(oForm.elements[i].id);
				sParam += "=";
				aParams.push(sParam);
			}
		}
		else if (oForm.elements[i].type=="radio")
		{
			for (var j=0; j < aParams.length; j++)
			{
				if (aParams[j].match(oForm.elements[i].id)!=null)
					elemFound = true;
			}
			
			if (elemFound==false&&oForm.elements[i].checked)
			{
				var sParam = escape(oForm.elements[i].id);
				sParam += "=";
				sParam += escape(oForm.elements[i].value);
				aParams.push(sParam);
			}
			elemFound = false;
		}
		else
		{
			var sParam = escape(oForm.elements[i].id);
			sParam += "=";
			sParam += escape(oForm.elements[i].value);
			aParams.push(sParam);
		}
	}
	
	return aParams.join("&");
}

/*
* This function checks to see if numeric.
*/
function IsNumeric(sText)
{
	var ValidChars = "0123456789.-";
	var IsNumber=true;
	var Char;
   
	for (i = 0; i < sText.length && IsNumber == true; i++) 
   	{ 
    	Char = sText.charAt(i); 
      	if (ValidChars.indexOf(Char) == -1) 
        	IsNumber = false;
   	}
   	return IsNumber;
}

/*
* This function is to print the content from a Web page.
*/
function Clickheretoprint()
{ 
	var disp_setting="toolbar=yes,location=no,directories=yes,menubar=yes,"; 
	    disp_setting+="scrollbars=yes,width=650, height=600, left=100, top=25"; 
	var content_value = document.getElementById("print_content").innerHTML; 
	
	var docprint=window.open("","",disp_setting); 
		docprint.document.open(); 
		docprint.document.write("<html><head><title></title>"); 
		docprint.document.write("<link href='/scripts/_style.css' rel='stylesheet' type='text/css'>"); 
		docprint.document.write("</head><body><div width='100%' align='left'>");          
		docprint.document.write(content_value);          
		docprint.document.write("</div></body></html>"); 
		docprint.document.close(); 
		docprint.focus(); 
}

/****************************************************************************
* The StringBuilder functionality - to improve the speed of string			*
* concatenation over IE.													*
****************************************************************************/
/*
* This function initializes a new instance of the StringBuilder class and appends the given value if supplied
*/ 
function StringBuilder(value)
{
	this.strings = new Array("");
	this.append(value);
}

/*
* Appends the given value to the end of this instance.
*/
StringBuilder.prototype.append = function(value)
{
	if (value)
	{
		this.strings.push(value);
	}
}

/*
* Clears the string buffer
*/
StringBuilder.prototype.clear = function()
{
	this.strings.length = 1;
}

/*
* Converts this instance to a String.
*/
StringBuilder.prototype.toString = function()
{
	return this.strings.join("");
}

/***************************************************************************/

/****************************************************************************
* The date functions.														*
****************************************************************************/
/*
* This function checks a date object to be sure if it is a date or not.
*/
function isDate(dString)
{
	var tempString;
	var dMonth, dDay, dYear;
	var dateBool = true;
	
	tempString=dString.split("/");
	if(tempString.length==3)
	{
		dMonth = tempString[0];
		dDay = tempString[1];
		dYear = tempString[2];
	}
	else
		return false;

    if (dMonth < 1 || dMonth > 12)
        dateBool = false;
	else if (dDay < 1 || dDay > 31)
        dateBool = false;
	else if ((dMonth==4 || dMonth==6 || dMonth==9 || dMonth==11) && dDay==31)
        dateBool = false;
	else if (dMonth == 2) 
	{ 
		// check for february 29th
        var isleap = (dYear % 4 == 0 && (dYear % 100 != 0 || dYear % 400 == 0));
        if (dDay > 29 || (dDay==29 && !isleap)) 
            dateBool = false;
    }	
	else if (!IsNumeric(dYear))
		dateBool = false;
		
	return dateBool;
}
/***************************************************************************/

/*
* This function is to open the content in a new page.
*/
function popUp(URL,vWidth,vHeight) 
{
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=" + vWidth + ",height=" + vHeight + ",left=" + Math.round((document.body.clientWidth/2)-(vWidth/2)) + ",top=" + Math.round((document.body.clientHeight/2)-(vHeight/2)) + "');");
}

