/*
TouchVision JavaScript Library
Coded by Devin Agnew dagnew@touchvision.com 9/99-10/01
if you don't understand it, don't use it!

Contents:

getImageState(sWhichGraphic)										//returns the state of an image

turnIt(sWhichGraphic,sWhichState, sCheckState)						//simple function toggles an image

choice(sNavTo, sWhichBtn, sWhichState, sCheckState)					//takes 4 params, swaps button then navigates

replaceInString(sSearchIn, sLookFor, sReplaceWith)					//useful string function for replacing stuff

trimString(sTheString)												//function is similar to the trim fn() in VBScript, trims outside spaces in a string

getClock(sWhat) 													//fairly simple digital clock that gets the time from the client

isAState(sStateCode)												//pass this function a state code, and it will verify it.

extractDigits(sMixedString) 										//simple function that removes non-Numerical characters from a string.
																	//great for phone numbers and credit cards from forms
																					
redrawImages()														//call this function after a page loads to refresh all of the images on the page

formatNumber(fNumber,iNumberOfDecimals)								//function will set number of decimal places

*/

function getImageState(sWhichGraphic) 									//returns the state of an image
{
	//using the same nameing convention with the "turnIt()" function, this function
	//returns the current state of the image.
	eval("tempName=document." + sWhichGraphic + ".src");
	theState=tempName.substring(tempName.lastIndexOf("_")+1,tempName.lastIndexOf("."));
	return theState;
}

function turnIt(sWhichGraphic, sWhichState, sCheckState) 		//simple function toggles an image
{
	//This function takes three strings for parameters to change image states for any image on the page
	//This function works in IE 4+ and Netscape 3+
	//    the graphic needs a specific naming format.
	//    the underscore '_' is neccessary
	//    examples:
	//        button1_over.gif      --You would pass 'over' in the function
	//        button1_down.gif      --You would pass 'down' in the function
	//        graphic2_off.jpg      --You would pass 'off' in the function
	//        graphic2_on.jpg       --You would pass 'on' in the function
	//
	//The first parameter is the NAME of the image
	//    example: <img src='images/button.gif' NAME='bob' ID='bob'>
	//    You would pass the function 'bob'
	//
	//The second paramether is the state. i.e. 'over', 'up', 'down' that you want to change the image to
	//
	//The third is optional, it is the state the graphic needs to be, in order for it to work.
	//
	//This function works with any extension: gif jpg png bmp etc...
	//
	//<A HREF="http://www.touchvision.com" onMouseOver="turnIt('FirstBullet','over'" onMouseOut="turnIt('FirstBullet','up'" onMouseDown="turnIt('FirstBullet','down'"><img src="images/bullet_up.gif" NAME="FirstBullet"></A>
	//  the previous would require
	//  bullet_up.gif
	//  bullet_over.gif
	//  bullet_down.gif
	if (sCheckState) //first see if sCheckState exists
	{
	    if (getImageState(sWhichGraphic)==sCheckState) //now check to see if the state is the sCheckState
	    {
	  		eval("tempName=document." + sWhichGraphic + ".src");
	  		theExstension=tempName.substring(tempName.lastIndexOf("."),tempName.length);
	  		tempName=tempName.substring(0,tempName.lastIndexOf("_"));
	  		eval("document." + sWhichGraphic + ".src='" + tempName + "_" + sWhichState + theExstension + "'");
	  		return true;
	    }
	    else
	    {
	  		return false;
	    }
	}
	else //they didn't do the the check state, so just use the funciton the old fashion way
	{
	    eval("tempName=document." + sWhichGraphic + ".src");
	    theExstension=tempName.substring(tempName.lastIndexOf("."),tempName.length);
	    tempName=tempName.substring(0,tempName.lastIndexOf("_"));
	    eval("document." + sWhichGraphic + ".src='" + tempName + "_" + sWhichState + theExstension + "'");
	    return true;
	}
}

function choice(sNavTo, sWhichBtn, sWhichState, sCheckState)	//takes 4 params, swaps button then navigates
{
	 //1st parameter is the URL to Navigate to
	 //2nd is the name of the button you want to toggle
	 //3rd is the state you want to change to
	 //4th param is optional...it makes sure the button is that state before it navigates...it its not, it will do nothing
	 // here are examples
	 // this would work
	 //<div onMouseDown="choice('http://www.touchvision.com','goBtn','down','on')"><img src="goTv_on.gif" name="goBtn"></div>
	 // in this case, the function would do nothing because the state is 'off'
	 //<div onMouseDown="choice('http://www.touchvision.com','goBtn','down','on')"><img src="goTv_off.gif" name="goBtn"></div>
	 // in this case it would go because the 4th parameter isn't there
	 //<div onMouseDown="choice('http://www.touchvision.com','goBtn','down')"><img src="goTv_off.gif" name="goBtn"></div>

	 if (sCheckState)
	 {
		  if (turnIt(sWhichBtn, sWhichState, sCheckState))
		  {
				//now navigate in 250 ms, to show button click
				//alert('setTimeout("location.href=' + String.fromCharCode(39) + sNavTo + String.fromCharCode(39) + '",250)');
				eval('setTimeout("location.href=' + String.fromCharCode(39) + sNavTo + String.fromCharCode(39) + '",250)');
		  }
	 }
	 else
	 {
		  //use the turnIt fn()
		  turnIt(sWhichBtn, sWhichState);
		  //now navigate in 250 ms, to show button click
		  eval('setTimeout("location.href=' + String.fromCharCode(39) + sNavTo + String.fromCharCode(39) + '",250)');
	 }
}

function replaceInString(sSearchIn, sLookFor, sReplaceWith)		//useful string function for replacing stuff
{
	//this function takes 3 parameters
	//the first is a string it searches in
	//the second is the original string you want to replace
	//the third is the replacement string
	//it removes the character from the string in every instance and replaces it with the new one
	//it does the following.
	//
	//if i want to replace the spaces from "my big dog" with "-"
	//call the function like this: temp=replaceInString("my big dog"," ","-",)
	//it will return "my-big-dog"
	//
	if (( sSearchIn != "" ) || (sSearchIn !="null"))
	{
		while ( sSearchIn.indexOf(sLookFor)!=-1 )
		{
			//contains the string so we must call it again!
			sSearchIn=sSearchIn.substring(0,sSearchIn.indexOf(sLookFor)) + sReplaceWith + sSearchIn.substring(sSearchIn.indexOf(sLookFor)+sLookFor.length,sSearchIn.length);
		}
	}
	return sSearchIn;
}

function trimString(sTheString)											//function is very similar to the trim fn in VBScript
{
	 //example: newString=trimString("   there are     spaces around this    ");
	 //newString="there are     spaces around this"
	 //
	 ret=sTheString;
    if (sTheString.charAt(0)==" ")
	 {
		  ret=trimString(sTheString.substring(1,sTheString.length));
	 }
	 else if(sTheString.charAt(sTheString.length-1)==" ")
	 {
		  ret=trimString(sTheString.substring(0,sTheString.length-1));
	 }
	 return ret;
}


function getClock(sWhat)													//fairly simple digital clock that gets the time from the client
{
	 // fairly simple digital clock that gets the time from the client
	 //clock function that returns dates and times
	 //sWhat can be:
	 //	'date'      						(Friday August 13)
	 //	'shortDate' 						(08/13/2001)
	 //	'time'		 						(8:32:25 PM)
	 //	'shortTime'		 					(8:32 PM)
	 // 'month'								(August)
     // 'weekday'							(Friday)
	 // 'day'								(13)
	 // 'year'								(2001)
	 //  else gives all date and time!		(Friday August 13 8:32:25 PM)
	 d = new Date();
	 sTheHour=parseInt(d.getHours());
	 //sTheHour=sTheHour + 1;
	sAMPM="AM"
	if (sTheHour > 11)
	{
		sAMPM="PM"
	}
	if (sTheHour==0)
	{
		sTheHour=12;
	}
	else if(sTheHour > 12)
	{
		sTheHour=sTheHour-12;
	}
	sTheHour=String(sTheHour);
	sTheWeekDay=d.getDay();
	if (sTheWeekDay==0)
	{
		sTheWeekDay="Sunday";
	}
	else if(sTheWeekDay==1)
	{
		sTheWeekDay="Monday";
	}
	else if(sTheWeekDay==2)
	{
		sTheWeekDay="Tuesday";
	}
	else if(sTheWeekDay==3)
	{
		sTheWeekDay="Wednesday";
	}
	else if(sTheWeekDay==4)
	{
		sTheWeekDay="Thursday";
	}
	else if(sTheWeekDay==5)
	{
		sTheWeekDay="Friday";
	}
	else
	{
		sTheWeekDay="Saturday";
	}
	sTheMonth=d.getMonth();
	if (sTheMonth==0)
	{
		sTheMonth="January";
	}
	else if (sTheMonth==1)
	{
		sTheMonth="February";
	}
	else if (sTheMonth==2)
	{
		sTheMonth="March";
	}
	else if (sTheMonth==3)
	{
		sTheMonth="April";
	}
	else if (sTheMonth==4)
	{
		sTheMonth="May";
	}
	else if (sTheMonth==5)
	{
		sTheMonth="June";
	}
	else if (sTheMonth==6)
	{
		sTheMonth="July";
	}
	else if (sTheMonth==7)
	{
		sTheMonth="August";
	}
	else if (sTheMonth==8)
	{
		sTheMonth="September";
	}
	else if (sTheMonth==9)
	{
		sTheMonth="October";
	}
	else if (sTheMonth==10)
	{
		sTheMonth="November";
	}
	else if (sTheMonth==11)
	{
		sTheMonth="December";
	}
	iTheShortMonth=d.getMonth()+1
	if (iTheShortMonth<10)
	{
		 iTheShortMonth="0" + iTheShortMonth
	}
	sTheDay=d.getDate();
	sTheMinute=d.getMinutes();
	if (sTheMinute<10)
	{
		sTheMinute= "0" + sTheMinute;
	}
	sTheSeconds=d.getSeconds();
	if (sTheSeconds<10)
	{
		sTheSeconds= "0" + sTheSeconds;
	}
	sTheDate=sTheWeekDay + " " + sTheMonth + " " + sTheDay;
	sTheShortDate=iTheShortMonth + "/" + sTheDay + "/" + d.getYear()
	sTheTime=sTheHour + ":" + sTheMinute + ":" + sTheSeconds + " " + sAMPM;
	sTheShortTime=sTheHour + ":" + sTheMinute + " " + sAMPM;
	
	if (sWhat=="time")
	{
		 return sTheTime;
	}
	else if (sWhat=="shortTime")
	{
		 return sTheShortTime;
	}
	else if (sWhat=="month")
	{
		 return sTheMonth;
	}
	else if (sWhat=="weekday")
	{
		 return sTheWeekDay;
	}
	else if (sWhat=="day")
	{
		 return sTheDay;
	}
	else if (sWhat=="date")
	{
		 return sTheDate;
	}
	else if (sWhat=="year")
	{
		 return d.getYear();
	}
	else if (sWhat=="shortDate")
	{
		 return sTheShortDate;
	}
	else
	{
		 return sTheDate + " " + sTheTime;
	}
}

function isAState(sStateCode)												//pass this function a state code, and it will verify it.
{
	//aStates is an array with all 50 states and Washington DC
	//it contains the 2 letter abrieviation, then it contains the name of the state
	aStates=new Array("AL - ALABAMA","AK - ALASKA","AZ - ARIZONA","AR - ARKANSAS","CA - CALIFORNIA","CO - COLORADO","CT - CONNECTICUT","DE - DELAWARE","DC - DISTRICT OF COLUMBIA","FL - FLORIDA","GA - GEORGIA","HI - HAWAII","ID - IDAHO","IL - ILLINOIS","IN - INDIANA","IA - IOWA","KS - KANSAS","KY - KENTUCKY","LA - LOUISIANA","ME - MAINE","MD - MARYLAND","MA - MASSACHUSETTS","MI - MICHIGAN","MN - MINNESOTA","MS - MISSISSIPPI","MO - MISSOURI","MT - MONTANA","NE - NEBRASKA","NV - NEVADA","NH - NEW HAMPSHIRE","NJ - NEW JERSEY","NM - NEW MEXICO","NY - NEW YORK","NC - NORTH CAROLINA","ND - NORTH DAKOTA","OH - OHIO","OK - OKLAHOMA","OR - OREGON","PA - PENNSYLVANIA","RI - RHODE ISLAND","SC - SOUTH CAROLINA","SD - SOUTH DAKOTA","TN - TENNESSEE","TX - TEXAS","UT - UTAH","VT - VERMONT","VA - VIRGINIA","WA - WASHINGTON","WV - WEST VIRGINIA","WI - WISCONSIN","WY - WYOMING")
	ret=false;
	i=0;
	while ((i < aStates.length) && (!(ret)))
	{
		ret=(aStates[i].substring(0,2)==sStateCode)
		i+=1;
	}
	return ret;
}

function extractDigits(sMixedString)									//simple function that removes non-Numerical characters from a string.  great for phone numbers and credit cards from forms
{
	//The user may enter the card number with dashes or spaces to indicate the groupings of the digits on the card.
	//You could check for non-numeric characters and ask the user to enter the number without them, but it is better
	//to just ignore them. The extractDigits function will return a string that contains only the digits from the given string.
	//make numbers a string..so we don't actually add them together
	var sDigitsOnly = '';
	var sThisDigit  = '';
	for (var i = 0; i < sMixedString.length; i++)
	{
		sThisDigit = sMixedString.charAt(i);
		if (sThisDigit >= '0' && sThisDigit <= '9')
		{
			sDigitsOnly = sDigitsOnly + sThisDigit;
		}
	}
	return sDigitsOnly;
}

function redrawImages()														//call this function after a page loads to refresh all of the images on the page
{
	 //set all of the images
	 if (document.images.length!=0)
	 {
		 for (i=0; i<document.images.length; i++)
		 {
			 document.images(i).src=document.images(i).src;
		 }
	 }
	 //set all of the backgrounds
	 if (document.all.length!=0)
	 {
		 for (i=0; i<document.all.length; i++)
		 {
			 document.all(i).background=document.all(i).background;
		 }
	 }
}

function formatNumber(fNumber,iNumberOfDecimals)
{
	if (iNumberOfDecimals==0)
	{
		ret=parseInt(fNumber)
	}
	else
	{		
		i=iNumberOfDecimals+1
		sNumber=String(fNumber)
		ret=parseFloat(sNumber.substring(0,sNumber.lastIndexOf(".")+i));
	}
	return ret;
}

function preloadImages()
{
	//preload images used in mouseovers
	image1 = new Image();
	image2 = new Image();
	image3 = new Image();
	image4 = new Image();
	image5 = new Image();
	image6 = new Image();
	image1.src="images/btn-nav-about_on.gif";
	image2.src="images/btn-nav-services_on.gif";
	image3.src="images/btn-nav-cabinets_on.gif";
	image4.src="images/btn-nav-products_on.gif";
	image5.src="images/btn-nav-portfolio_on.gif";
	image6.src="images/btn-nav-contact_on.gif";
}
