/************************************************************************
* 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)) + "');");
}

/****************************************************************************/

// this function adds the nav hover effects on the home page
function _navHover(vId)
{
	if (document.getElementById(vId).style.visibility == "hidden")
		document.getElementById(vId).style.visibility = "visible";
	else
		document.getElementById(vId).style.visibility = "hidden";
}

// this function adds the nav hover effects on the home page
function _navInternalHover(vId, vAction)
{
	if (document.getElementById(vId).style.visibility == "visible" && vAction == 1)
		document.getElementById(vId).style.visibility = "visible";
	else if (document.getElementById(vId).style.visibility == "hidden")
		document.getElementById(vId).style.visibility = "visible";
	else
		document.getElementById(vId).style.visibility = "hidden";
}

// this function adds the left menu hover effects
function _leftMenuHover(vId)
{
	if (document.getElementById(vId).style.backgroundColor == "")
		document.getElementById(vId).style.backgroundColor = "#66cc33";
	else
		document.getElementById(vId).style.backgroundColor = "";
}

// this function adds the left menu hover effects
function _leftFinancialMenuHover(vId)
{
	if (document.getElementById(vId).style.backgroundColor == "")
		document.getElementById(vId).style.backgroundColor = "#00c4f4";
	else
		document.getElementById(vId).style.backgroundColor = "";
}

// this function adds the left menu hover effects
function _leftWealthMenuHover(vId)
{
	if (document.getElementById(vId).style.backgroundColor == "")
		document.getElementById(vId).style.backgroundColor = "#a7a9ac";
	else
		document.getElementById(vId).style.backgroundColor = "";
}

/****************************************************************************/

// Exit Notice
function ExitNotice()
{
    if (confirm("You are now leaving the Mutual Bank website.\n\nWe do not endorse, approve, certify or control external sites and do not guarantee the accuracy, completeness, efficacy, timeliness or accurate sequencing of the information contained on them.\n\nTo remain at our site, click Cancel.  To leave our site for the link you selected, click OK.\n\nThank you for visiting our site."))
    {
        return true;
    }
    else
    {
        history.go(0);
        return false;
    }
}

// USA Patriot Act Notice
/*function USPatNotice()
{
    var s = "The USA Patriot Act has paved the way for financial institutions to help prevent fraud, identity theft, and the spread of terrorism.  It requires financial institutions to obtain more information from an individual or legal entity to help establish identity.\n\n";
    s += "Your cooperation is needed when you open a new account or request a loan.  You may be asked more questions to establish and confirm your identity.  It may also be required for you to provide one or more of the following types of identification:\n\n";
    s += "* Driver's license\n\n";
    s += "* Passport and country of issuance\n\n";
    s += "* U.S. taxpayer identification (ID) number\n\n";
    s += "* Alien ID card\n\n";
    s += "* Any other government-issued document evidencing nationality or residence\n\n";

    alert(s);
}

// Email Notice
function EmailNotice()
{
	var s = "Visitors to our web site are advised that e-mail communications\n";
    s += "may not be wholly secure from interception.\n";
    s += "Please do not include confidential information such as social security or\n";
    s += "account numbers in your e-mail communications.";

	alert(s);
}*/

/***************************************************************************/

// submit the contact form
function _submitContactForm()
{
	if (document.form1.MessageType.value=="")
	{
		alert("Please select the purpose of your message.");
		document.form1.MessageType.focus();
	}
	else if (document.form1.RegardedInformation.value=="")
	{
		alert("Please tell us what your message is about.");
		document.form1.RegardedInformation.focus();
	}
	else if (document.form1.Details.value=="")
	{
		alert("Please provide details about your message.");
		document.form1.Details.focus();
	}
	else if (document.form1.ContactMethod.value=="")
	{
		alert("Please provide the best method for us to respond to you.");
		document.form1.ContactMethod.focus();
	}
	else if (document.form1.UserName.value=="")
	{
		alert("Please provide your name.");
		document.form1.UserName.focus();
	}
	else if (document.form1.Phone.value==""||!IsNumeric(document.form1.Phone.value))
	{
		alert("Please provide your phone number.");
		document.form1.Phone.focus();
	}
	else if (document.form1.UserEmail.value==""||document.form1.UserEmail.value.indexOf("@")<2||document.form1.UserEmail.value.indexOf(".")<2)
	{
		alert("Please provide your email address.");
		document.form1.UserEmail.focus();
	}
	else
	{
		document.form1.action = "/scripts/FormMailer.asp?InfoType=!! Contact Submitted from bankwithmutual.com !!&Refer=/businesscontact.asp";
		document.form1.submit();
	}
}

/***************************************************************************/

// submit the Makeover That Room Contest Form
function _submitContestForm()
{
	if (document.formContest.First_Name.value=="")
	{
		alert("Please provide your first name.");
		document.formContest.First_Name.focus();
	}
	else if (document.formContest.Last_Name.value=="")
	{
		alert("Please provide your last name.");
		document.formContest.Last_Name.focus();
	}
	else if (document.formContest.Date_of_Birth.value==""||!isDate(document.formContest.Date_of_Birth.value))
	{
		alert("Please provide your date of birth.");
		document.formContest.Date_of_Birth.focus();
	}
	else if (document.formContest.City.value=="")
	{
		alert("Please provide your city.");
		document.formContest.City.focus();
	}
	else if (document.formContest.State.value=="")
	{
		alert("Please provide your state.");
		document.formContest.State.focus();
	}
	else if (document.formContest.Zip_Code.value==""||!IsNumeric(document.formContest.Zip_Code.value))
	{
		alert("Please provide your zip code.");
		document.formContest.Zip_Code.focus();
	}
	else if (document.formContest.User_Email.value==""||document.formContest.User_Email.value.indexOf("@")<2||document.formContest.User_Email.value.indexOf(".")<2)
	{
		alert("Please provide your email address.");
		document.formContest.User_Email.focus();
	}
	else if (document.formContest.Day_Phone.value==""||!IsPhone(document.formContest.Day_Phone.value))
	{
		alert("Please provide your day phone.");
		document.formContest.Day_Phone.focus();
	}
	else if (document.formContest.Evening_Phone.value!=""&&!IsPhone(document.formContest.Evening_Phone.value))
	{
		alert("Please provide your evening phone.");
		document.formContest.Evening_Phone.focus();
	}
	else if (document.formContest.Find_Out.value=="")
	{
		alert("Please provide how you found out about this contest.");
		document.formContest.Find_Out.focus();
	}
	else if (document.formContest.Need_Makeover.value=="")
	{
		alert("Please provide why you are in need of a room makeover.");
		document.formContest.Need_Makeover.focus();
	}
	else if (document.formContest.Accomplish.value=="")
	{
		alert("Please provide what you are looking to accomplish with your room makeover.");
		document.formContest.Accomplish.focus();
	}
	else if (document.formContest.Photo_1.value!=""&&(document.formContest.Photo_1.value.indexOf(".jpg")<=0&&document.formContest.Photo_1.value.indexOf(".jpeg")<=0&&document.formContest.Photo_1.value.indexOf(".gif")<=0&&document.formContest.Photo_1.value.indexOf(".JPG")<=0&&document.formContest.Photo_1.value.indexOf(".JPEG")<=0&&document.formContest.Photo_1.value.indexOf(".GIF")<=0))
	{
		alert("Please only upload .gif, .jpg or .jpeg images.");
		document.formContest.Photo_1.focus();
	}
	else if (document.formContest.Photo_2.value!=""&&(document.formContest.Photo_2.value.indexOf(".jpg")<=0&&document.formContest.Photo_2.value.indexOf(".jpeg")<=0&&document.formContest.Photo_2.value.indexOf(".gif")<=0&&document.formContest.Photo_2.value.indexOf(".JPG")<=0&&document.formContest.Photo_2.value.indexOf(".JPEG")<=0&&document.formContest.Photo_2.value.indexOf(".GIF")<=0))
	{
		alert("Please only upload .gif, .jpg or .jpeg images.");
		document.formContest.Photo_2.focus();
	}
	else if (document.formContest.Photo_3.value!=""&&(document.formContest.Photo_3.value.indexOf(".jpg")<=0&&document.formContest.Photo_3.value.indexOf(".jpeg")<=0&&document.formContest.Photo_3.value.indexOf(".gif")<=0&&document.formContest.Photo_3.value.indexOf(".JPG")<=0&&document.formContest.Photo_3.value.indexOf(".JPEG")<=0&&document.formContest.Photo_3.value.indexOf(".GIF")<=0))
	{
		alert("Please only upload .gif, .jpg or .jpeg images.");
		document.formContest.Photo_3.focus();
	}
	else if (document.formContest.Photo_1.value==""&&document.formContest.Photo_2.value==""&&document.formContest.Photo_3.value=="")
	{
		alert("Please provide at least one photo before submitting your form.");
		document.formContest.Photo_1.focus();
	}
	else if (!document.formContest.Authenticity.checked)
	{
		alert("Please affirm that you have complied with all the terms of this contest.");
		document.formContest.Authenticity.focus();
	}
	else
	{
		document.formContest.action="/contest/scripts/formMailer.asp?InfoType=!! Makeover That Room Entry !!&Refer=/contest";
		document.getElementById("btn_Submit").disabled = true;
		document.formContest.submit();
	}
}

// check the image
function _CheckContestImage( vId )
{
	if (document.getElementById(vId).value.indexOf(".jpg")<=0&&document.getElementById(vId).value.indexOf(".jpeg")<=0&&document.getElementById(vId).value.indexOf(".gif")<=0&&document.getElementById(vId).value.indexOf(".JPG")<=0&&document.getElementById(vId).value.indexOf(".JPEG")<=0&&document.getElementById(vId).value.indexOf(".GIF")<=0)
	{
		alert("Please only upload .gif, .jpg or .jpeg images.");
	}
}

// function to count the characters in a contest area
function _countContestArea( vId )
{
	//var old = parseInt(document.getElementById("span" + vId + "Counter").value);
	var limit = 500;
	var hidCounter = 0;
	hidCounter = parseInt(document.getElementById(vId).value.length);
	//document.getElementById("span" + vId + "Counter").innerHTML = limit - hidCounter;
	if ((limit-hidCounter)<0) 
	{
		document.getElementById(vId).value = document.getElementById(vId).value.substr(0, limit);
		hidCounter = limit;
		//document.getElementById("span" + vId + "Counter").innerHTML = limit - hidCounter;
  	}
}

/*
* This function checks to see if phone.
*/
function IsPhone(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;
}

/**************************************************************/
// attempts to submit statements request
function _submitStatementRequest()
{
	if (document.formstatement.txt_FirstName.value=="")
	{
		alert("Please provide your first name.");
		document.formstatement.txt_FirstName.focus();
	}
	else if (document.formstatement.txt_LastName.value=="")
	{
		alert("Please provide your last name.");
		document.formstatement.txt_LastName.focus();
	}
	else if (document.formstatement.txt_Email.value==""||document.formstatement.txt_Email.value.indexOf("@")<2||document.formstatement.txt_Email.value.indexOf(".")<2)
	{
		alert("Please provide your email address.");
		document.formstatement.txt_Email.focus();
	}
	else if (document.formstatement.txt_AccountNumber.value==""||!IsNumeric(document.formstatement.txt_AccountNumber.value))
	{
		alert("Please provide your account number.");
		document.formstatement.txt_AccountNumber.focus();
	}
	else if (document.formstatement.txt_SSN.value!=""&&!IsNumeric(document.formstatement.txt_SSN.value))
	{
		alert("Please provide the last 6 digits of your SSN.");
		document.formstatement.txt_SSN.focus();
	}
	else
	{
		document.formstatement.action="/statements/scripts/srv_Request.asp";
		document.getElementById("btn_Submit").disabled = true;
		document.formstatement.submit();
	}
}

