var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;
var isNN = (navigator.appName.indexOf("Netscape")!=-1) ? true : false;

if(isIE == true)
{
	document.write('<scri');
	document.write('pt language="vbscript" type="text/vbscript" src="/goodies/atomic.vbs"');
	document.write('><');
	document.write('/scri');
	document.write('pt>');
}

// Minor version of Flash required
var requiredMinorVersion = 0;
// Revision of Flash required
var requiredRevision = 0;

function getCookie(cookieName)
{
	if (document.cookie.length>0)
	  {
	  chrPos=document.cookie.indexOf(cookieName + '=');
	  if (chrPos!=-1)
	  { 
			chrPos=chrPos + cookieName.length+1; 
	    chrEnd=document.cookie.indexOf(';',chrPos);
	    if (chrEnd==-1) chrEnd=document.cookie.length;
	    return unescape(document.cookie.substring(chrPos,chrEnd));
    } 
  }
	return '';
} 

function getWidth()
{
	var frameWidth = '';
  if (self.innerWidth)
  {
		frameWidth = self.innerWidth;
  }
  else if (document.documentElement && document.documentElement.clientWidth)
  {
		frameWidth = document.documentElement.clientWidth;
  }
  else if (document.body)
  {
		frameWidth = document.body.clientWidth;
  }
  return frameWidth;
}

function getHeight()
{
	var frameHeight = '';
  if (self.innerHeight)
  {
		frameHeight = self.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight)
  {
		frameHeight = document.documentElement.clientHeight;
  }
  else if (document.body)
  {
		frameHeight = document.body.clientHeight;
  }
  return frameHeight;
}

function getStyleObject(objectId) 
{
  if(document.getElementById && document.getElementById(objectId)) 
	{
		// W3C DOM
		return document.getElementById(objectId).style;
  } else if (document.all && document.all(objectId)) {
		// MSIE 4 DOM
		return document.all(objectId).style;
  } else if (document.layers && document.layers[objectId]) {
		// NN 4 DOM
		return document.layers[objectId];
  } else {
		return false;
  }
} 

function changeObjectVisibility(objectId, newVisibility, newZindex, newDisplay)
{
  var styleObject = getStyleObject(objectId);
  if(styleObject) 
	{
		styleObject.display = newDisplay;
		styleObject.visibility = newVisibility;
		styleObject.zIndex = newZindex;
		return true;
	} 
	else 
	{
		//couldn't find the object, can't change its visibility
		return false;
  }
}	

function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function mainMenu()
{
	var rootAddress = currentLoc('root');
	var currentSection = currentLoc('section');
	var menuString = '<div id="mainMenu">';
	menuString += '<a id="buttonHome" class="button';
	if(currentSection == 'home'){menuString += 'Live';}
	menuString += '" href="http://'+rootAddress+'">Home</a>';
	menuString += '<a id="buttonAbout" class="button';
	if(currentSection == 'about-us'){menuString += 'Live';}
	menuString += '" href="http://'+rootAddress+'/about-us/">About Us</a>';
	menuString += '<a id="buttonCarSeat" class="button';
	if(currentSection == 'car-seat-check'){menuString += 'Live';}
	menuString += '" href="http://'+rootAddress+'/car-seat-check/">Car Seat Check</a>';
	menuString += '<a id="buttonMembers" class="button';
	if(currentSection == 'our-members'){menuString += 'Live';}
	menuString += '" href="http://'+rootAddress+'/our-members/">Our Members</a>';
	menuString += '<a id="buttonNews" class="button';
	if(currentSection == 'news-and-events'){menuString += 'Live';}
	menuString += '" href="http://'+rootAddress+'/news-and-events/">News &amp; Events</a>';
	menuString += '<a id="buttonFaq" class="button';
	if(currentSection == 'faq'){menuString += 'Live';}
	menuString += '" href="http://'+rootAddress+'/faq/">F.A.Q.</a>';
	menuString += '<a id="buttonLinks" class="button';
	if(currentSection == 'links'){menuString += 'Live';}
	menuString += '" href="http://'+rootAddress+'/links/">Links</a>';
	menuString += '<a id="buttonContact" class="button';
	if(currentSection == 'contact-us'){menuString += 'Live';}
	menuString += '" href="http://'+rootAddress+'/contact-us/">Contact Us</a>';
	menuString += '</div>';
	document.write(menuString);
}

function aboutMenu()
{
	var currentPage = currentLoc('file');
	var menuString = '<div id="submenuAbout">';
	menuString += '<a href="our-mission.asp"';
	if(currentPage == 'our-mission'){menuString += 'class="live"';}
	menuString += '>Our Mission</a>:';
	menuString += '<a href="what-we-do.asp"';
	if(currentPage == 'what-we-do'){menuString += 'class="live"';}
	menuString += '>What We Do</a>:';
	menuString += '<a href="why-we-do-it.asp"';
	if(currentPage == 'why-we-do-it'){menuString += 'class="live"';}
	menuString += '>Why We Do It</a>:';
	menuString += '<a href="from-the-director.asp"';
	if(currentPage == 'from-the-director'){menuString += 'class="live"';}
	menuString += '>From the Director</a>';
	menuString += '</div>';
	document.write(menuString);
}

function faqMenu()
{
	var currentPage = currentLoc('file');
	var menuString = '<div id="submenuFaq">';
	menuString += '<a href="index.asp"';
	if(currentPage == 'index'){menuString += 'class="live"';}
	menuString += '>Facts &amp; Statistics</a>';
	menuString += ':<a href="atv-safety.asp"';
	if(currentPage == 'atv-safety'){menuString += 'class="live"';}
	menuString += '>ATV Safety</a>';
	menuString += ':<a href="bike-safety.asp"';
	if(currentPage == 'bike-safety'){menuString += 'class="live"';}
	menuString += '>Bike Safety</a>';
	menuString += ':<a href="car-safety.asp"';
	if(currentPage == 'car-safety'){menuString += 'class="live"';}
	menuString += '>Car Safety</a>';
	menuString += ':<a href="fire-safety.asp"';
	if(currentPage == 'fire-safety'){menuString += 'class="live"';}
	menuString += '>Fire Safety</a>';
	menuString += ':<a href="water-safety.asp"';
	if(currentPage == 'water-safety'){menuString += 'class="live"';}
	menuString += '>Water Safety</a>';
	menuString += '</div>';
	document.write(menuString);
}

function currentLoc(whatLoc)
{
	var returnValue = '';
	var urlString = location.toString();
	var urlArray = urlString.split('/');
	if(whatLoc == 'root')
	{
		var rootName = urlArray[2];
		returnValue = rootName;
	}
	if(whatLoc == 'file')
	{
		var fileName = urlArray[urlArray.length-1];
		fileName = fileName.substr(0,fileName.lastIndexOf('.'));
		returnValue = fileName;
	}
	if(whatLoc == 'section')
	{
		var sectionName = urlArray[urlArray.length-2];
		if(sectionName.indexOf('shandssafekids') > -1){sectionName = 'home';}
		returnValue = sectionName;
	}
	return returnValue;
}

function findPos(styleObject) {
	var curleft = 0;
	var curtop = 0;
	if (styleObject.offsetParent) {
		curleft = styleObject.offsetLeft
		curtop = styleObject.offsetTop
		while (styleObject = styleObject.offsetParent) {
			curleft += styleObject.offsetLeft
			curtop += styleObject.offsetTop
		}
	}
	return [curleft,curtop];
}

function getFlashMovieObject(movieName)
{
	if (window.document[movieName])
	{
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet")==-1)
	{
		if (document.embeds && document.embeds[movieName])
		return document.embeds[movieName];
	}
	else
	{
		return document.getElementById(movieName);
	}
}

function playFlash(whatMovie)
{
	var flashMovie = getFlashMovieObject(whatMovie);
	//var flashMovie = window.document[whatMovie];
	if(flashMovie == null)
	{
//    window.setTimeout('playFlash("'+whatMovie+'")',500);
	}
	else
	{
		flashMovie.Play();
	}
}

function ResetFlashMovie(whatMovie,whatFrame)
{
	var flashMovie = getFlashMovieObject(whatMovie);
	flashMovie.GotoFrame(whatFrame);
}

function SendDataToFlashMovie(whatMovie,whatTarget,whatData)
{
	var flashMovie=getFlashMovieObject(whatMovie);
	flashMovie.SetVariable(whatTarget,whatData);
}

function JSGetSwfVer(i)
{
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
      		var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			descArray = flashDescription.split(" ");
			tempArrayMajor = descArray[2].split(".");
			versionMajor = tempArrayMajor[0];
			versionMinor = tempArrayMajor[1];
			if ( descArray[3] != "" ) {
				tempArrayMinor = descArray[3].split("r");
			} else {
				tempArrayMinor = descArray[4].split("r");
			}
      		versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
            flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
      	} else {
			flashVer = -1;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) flashVer = 4;
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) flashVer = 3;
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) flashVer = 2;
	// Can't detect in all other cases
	else {
		
		flashVer = -1;
	}
	return flashVer;
} 

function DetectFlashVer(reqMajorVer, reqMinorVer, reqRevision) 
{
 	reqVer = parseFloat(reqMajorVer + "." + reqRevision);
   	// loop backwards through the versions until we find the newest version	
	for (i=25;i>0;i--) {	
		if (isIE && isWin && !isOpera) {
			versionStr = VBGetSwfVer(i);
		} else {
			versionStr = JSGetSwfVer(i);		
		}
		if (versionStr == -1 ) 
		{ 
			//alert('no version');
			return false;
		} else if (versionStr != 0) {
			if(isIE && isWin && !isOpera) {
				tempArray         = versionStr.split(" ");
				tempString        = tempArray[1];
				versionArray      = tempString .split(",");				
			} else {
				versionArray      = versionStr.split(".");
			}
			versionMajor      = versionArray[0];
			versionMinor      = versionArray[1];
			versionRevision   = versionArray[2];
			
			versionString     = versionMajor + "." + versionRevision;   // 7.0r24 == 7.24
			versionNum        = parseFloat(versionString);
        	// is the major.revision >= requested major.revision AND the minor version >= requested minor
			if ( (versionMajor > reqMajorVer) && (versionNum >= reqVer) ) {
				return true;
			} else {
				return ((versionNum >= reqVer && versionMinor >= reqMinorVer) ? true : false );	
			}
		}
	}	
	return (reqVer ? false : 0.0);
}

function showFlash(whatMovie,paramString,movieName,W,H,BG,Win,Alignment,whatScale,whatVersion,alternateContent,whatAccess)
{
	var requiredMajorVersion = whatVersion;
	if(whatScale == null || whatScale == '')
	{
		whatScale = 'showAll';
	}
	if(whatAccess == null || whatAccess == '')
	{
		whatAccess = 'sameDomain';
	}
	var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
	if(hasRightVersion)
	{  // if we've detected an acceptable version
    var oeTags = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
    + ' width="'+W+'" height="'+H+'" id="'+movieName+'"'
    + ' codebase="https://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">'
    + '<param name="movie" value="'+whatMovie+paramString+'" />'
    + '<param name="menu" value="false" />'
    + '<param name="quality" value="high" />'
    + '<param name="wmode" value="'+Win+'" />'
    + '<param name="bgcolor" value="#'+BG+'" />'
    + '<param name="scale" value="'+whatScale+'" />'
    + '<embed src="'+whatMovie+paramString+'" menu="false" quality="high"'
    + ' wmode="'+Win+'" bgcolor="#'+BG+'"'
    + ' scale="'+whatScale+'"'
    + ' width="'+W+'" height="'+H+'" name="'+movieName+'" align="'+Alignment+'"'
    + ' play="true"'
    + ' loop="false"'
    + ' quality="high"'
    + ' allowScriptAccess="'+whatAccess+'"'
    + ' swliveconnect="true"'
    + ' type="application/x-shockwave-flash"'
    + ' pluginspage="https://www.macromedia.com/go/getflashplayer">'
    + '<\/embed>'
    + '<\/object>';
		//alert(oeTags);
    document.write(oeTags);   // embed the flash movie
  } 
	else 
	{
		document.write(alternateContent);
  }
}

function verify(f)
{
  var empty_fields = "";
  var fCount = 0;
  var empty_options = "";
  var oCount = 0;
	var empty_checks = '';
	var cCount = 0;
  var errors = "";
  var eCount = 0;
  var msg = "";
	var checkedSets = new Array();

  for(var i = 0; i < f.length; i++)
  {
    var e = f.elements[i];
    if(((e.type == "text") || (e.type == "textarea") || (e.type == "password")) && !e.optional)
    {
      if (((e.value == null) || (e.value == "") || isblank(e.value)) && !e.conditional)
      {
        empty_fields += "\n" + replace(e.name,"_"," ") + " is required";
        fCount += 1
        continue;
      }
		}
		//CHECKBOXES
		/*
		if((e.type == 'checkbox') && (e.optional != true || !e.conditional))
		{
			var setName = e.name;
			var setChecked = false;
			if(!Array.indexOf)
			{
				for(var cs=0; cs<checkedSets.length; cs++)
				{
					if(checkedSets[cs]==setName)
					{
						setChecked = true;
						break;
					}
				}
			}
			if(Array.indexOf)
			{
				if(checkedSets.indexOf(setName) > -1)
				{
					setChecked = true;
				}
			}
			if(!setChecked)
			{
				var checkVal = false;
				var currentSet = f[setName];
				for(zz=0;zz<currentSet.length;zz++)
				{
					if(currentSet[zz].checked)
					{
						checkVal = true;
						break;
					}
				}
				if(!checkVal)
				{
					empty_checks += "\n" + replace(e.name,"_"," ") + " selection is required";
					cCount = cCount + 1;
				}
				checkedSets.push(setName);
			}
		}
		*/
		//END OF CHECKBOXES
		
		//RADIOS
		if(e.type == 'radio' && (!e.optional || !e.conditional))
		{
			var setName = e.name;
			var currentSet = f[setName];
			var setChecked = false;
			if(!Array.indexOf)
			{
					for(var cs=0; cs<checkedSets.length; cs++)
					{
						if(checkedSets[cs]==setName)
						{
							setChecked = true;
							break;
						}
					}
			}
			if(Array.indexOf)
			{
				if(checkedSets.indexOf(setName) > -1)
				{
					setChecked = true;
				}
			}
			if(!setChecked)
			{
				var checkVal = false;
				var currentSet = f[setName];
				for(zz=0;zz<currentSet.length;zz++)
				{
					if(currentSet[zz].checked || currentSet[zz].optional)
					{
						checkVal = true;
						break;
					}
				}
				if(!checkVal)
				{
		       empty_checks += "\n" + replace(e.name,"_"," ") + " selection is required";
		       cCount = cCount + 1;
				}
				checkedSets.push(setName);
			}
		}	//END OF RADIOS
		
		//CONDITONAL FIELDS
    if(e.conditional)
    {
			var triggered = false;
			var failed = false;
			//FIRST CHECK IF THE CONDITONAL FIELD IS REQUIRING THIS TO HAVE A VALUE
			var conditionField = e.condition;
			if(regExTest(conditionField,/[\d]/))
			{
				var whatField = f.elements[conditionField];
			}
			else
			{
				var whatField = f[conditionField];
			}
			var fieldType = whatField.type;
			var fieldName = whatField.id;
				//WHAT VALUE TO TRIGGER THIS
			var conditionalValue = e.conValue;
			//alert(e.name+','+e.type+','+e.condition+','+conditionalValue+','+e.optional);
			//WHAT TYPE OF FIELD WAS SELECTED TO TRIGGER THIS
			if(fieldType == 'checkbox' && whatField.checked)
			{
				triggered = true;
			}
			if(fieldType == 'radio' && whatField.checked)
			{
				triggered = true;
			}
			if(fieldType == 'text' && ((whatField.value != null) || (whatField.value != "") || !isblank(whatField.value)))
			{
				triggered = true;
			}
			if(whatField.options)
	    {
      	//if (whatField.selectedIndex == (whatField.length-1))
				if(whatField.value == conditionalValue)
      	{
					triggered = true;
				}
			}
			if(triggered == true)
			{
				//alert('triggered');
				//CHECK THAT THE CONDITION IS MET
				if((e.options || (e.type == 'select-one')) && !e.optional)
    		{
      		var which = e.options.selectedIndex;
					if(which < 0)
					{
						failed = true;
					}else{
						var choice = e.options[which].value;
						if ((choice == null) || (choice == ""))
						{
							failed = true;
						}
					}
				}
				if((e.type == "text") || (e.type == "textarea"))
				{
					if((e.value == null) || (e.value == "") || isblank(e.value))
					{
						failed = true;
					}
				}
				if((e.type == 'checkbox') || (e.type == 'radio'))
				{
					var setName = e.name;
					var setChecked = false;
					if(!Array.indexOf)
					{
							for(var cs=0; cs<checkedSets.length; cs++)
							{
								if(checkedSets[cs]==setName)
								{
									setChecked = true;
									break;
								}
							}
					}
					if(Array.indexOf)
					{
						if(checkedSets.indexOf(setName) > -1)
						{
							setChecked = true;
						}
					}
					if(!setChecked)
					{
						var checkVal = false;
						var currentSet = f[setName];
						for(zz=0;zz<currentSet.length;zz++)
						{
							if(currentSet[zz].checked)
							{
								checkVal = true;
								break;
							}
						}
						if(!checkVal)
						{
							failed = true;
						}
						checkedSets.push(setName);
					}
				}
				if(failed == true)
				{
					//alert("failed");
        	empty_fields += "\n" + replace(e.name,"_"," ") + " is required: " + replace(fieldName,"_"," ") + " = " + whatField.value + " selected";
          fCount += 1
				}
			}
    }
		
		if(e.time)
		{
			if(regExTest(e.value,"[^0-9:]") == true)
			{
			  errors += "\nA valid " + replace(e.name,"_"," ") + " is required. Format as: xx:xx";
			  eCount += 1;
			}
    }
		
    if(e.phone)
    {
			var rangePattern = /[^0-9-]/;
			if(regExTest(e.value,rangePattern) == true)
			{
         errors += "\n" + replace(e.name,"_"," ") + " must be numeric. Format as: xxx-xxx-xxxx";
         eCount = eCount + 1;
			}
    }
		
    if(e.money)
    {
			var moneyPattern = /[^0-9\$\.\,]/;
			if(regExTest(e.value,"[^0-9\.\$,]") == true)
			{
         errors += "\n" + replace(e.name,"_"," ") + " must be dollars and cents";
         eCount = eCount + 1;
			}
    }
		
    if(e.numeric)
    {
			var numPattern = /[^0-9\.\,]/;
			if(regExTest(e.value,numPattern) == true)
			{
         errors += "\n" + replace(e.name,"_"," ") + " must be numeric";
         eCount = eCount + 1;
			}
    }
		
    if(e.numericRange)
    {
			var rangePattern = /[^0-9\s\$\.\,-]/;
			if(regExTest(e.value,rangePattern) == true)
			{
         errors += "\n" + replace(e.name,"_"," ") + " must be numeric";
         eCount = eCount + 1;
			}
    }
		
		if(e.date)
		{
			var datePattern = /\d{1,2}\/\d{1,2}\/\d{4}/;
			if(regExTest(e.value,datePattern) == false)
			{
			  errors += "\nA valid " + replace(e.name,"_"," ") + " is required. Format as: xx/xx/xxxx";
			  eCount += 1;
			}
    }
			
    if(e.count != null)
    {
	    if (e.value.length < e.count)
      {	  
  	    errors += "\n" + replace(e.name,"_"," ") + " must be at least " + e.count + " characters in length";
        eCount = eCount + 1;
      }
    }
    if(e.minValue != null)
    {
		  var v = parseFloat(e.value);
      if (isNaN(v) || ((e.minValue != null) && (v < e.minValue)))
      {	  
        errors += "\n" + replace(e.name,"_"," ") + " must be greater than " + e.minValue;
        eCount = eCount + 1;
      }
    }
    if(e.maxValue != null)
    {
		  var v = parseFloat(e.value);
      if (isNaN(v) || ((e.maxValue != null) && ( v > e.maxValue)))
      {	  
        errors += "\n" + replace(e.name,"_"," ") + " must be less than " + e.maxValue;
        eCount = eCount + 1;
      }
    }
    if(e.email)
    {
			if(regExTest(e.value,"^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$") == false)
			{
			  errors += "\nA valid email address is required";
			  eCount += 1;
			}
    }

    if (e.options && !e.optional && !e.conditional)
    {
      var which = e.options.selectedIndex;
			if(which < 0)
			{
        empty_options += "\n" + replace(e.name,"_"," ") + " selection is required";
        oCount = oCount + 1;
			}else{
				var choice = e.options[which].value;
				if ((choice == null) || (choice == "") && !e.conditional)
				{
					empty_options += "\n" + replace(e.name,"_"," ") + " selection is required";
					oCount = oCount + 1;
					//continue;
				}
			}
    }
  }

  if ((!empty_fields) && (!empty_options) && (!empty_checks) && !errors)
  {
    return true;
  }
  else
  {
    msg = "Please be sure to complete all selections or fill in all required fields.\n";
    if (empty_fields)
    {
      fieldsVar = "field"
      if(fCount > 1){fieldsVar += "s"}
      msg += "\n" + fCount + " required " + fieldsVar + " left blank.";
      msg += empty_fields += "\n";
    }
    if (errors)
    {
      fieldsVar = "field"
      if(eCount > 1){fieldsVar += "s"}
      msg += "\n" + eCount + " " + fieldsVar + " with invalid entries.";
      msg += errors += "\n";
    }
    if (empty_options)
    {
      choicesVar = "drop-down selection"
      if (oCount > 1)
        {choicesVar += "s"}
      msg += "\n" + oCount + " required " + choicesVar + " not completed.";
      msg += empty_options += "\n";
    }
    if (empty_checks)
    {
      choicesVar = "checkbox\/radio option"
      if (cCount > 1)
        {choicesVar += "s"}
      msg += "\n" + cCount + " required " + choicesVar + " not chosen.";
      msg += empty_checks += "\n";
    }
    alert(msg);
    return false;
  }
}

function isLetter(str)
{
  var fieldErrors = ""
  var rex = /[^a-zA-Z\s]/

  if ((str == "") || (str == null))
  {
  fieldErrors = "You must enter text.\n";
  }

  if(rex.test(str))
  {
  fieldErrors += "Invalid data was entered.";
  }

  if (fieldErrors)
  {
  return false;
  }
  else
  {
  return true;
  }
}

function isAlphaNum(str)
{
  var fieldErrors = ""
  var rex = /[^\w]/

  if ((str == "") || (str == null) || isblank(str))
  {
  fieldErrors = "You must enter something.\n";
  }

  if(rex.test(str))
  {
  fieldErrors += "Check that you have only entered letters and-or numbers.";
  }

  if (fieldErrors)
  {
  return false;
  }
  else
  {
  return true;
  }
}

function isblank(s)
{
  for(var i = 0; i < s.length; i++)
  {
    var c = s.charAt(i);
    if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
  }
  return true;
}

function replace(string,text,by) 
{
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function regExTest(whatString,whatPattern)
{
	var regX = new RegExp(whatPattern);
	return regX.test(whatString);
}

/*AUTOTAB FUNCTIONS*/
function autoTab(input,len, e)
{
	var keyCode = (isNN) ? e.which : e.keyCode; 
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];

	if(input.value.length >= len && !containsElement(filter,keyCode)) 
	{
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
	}
}

function containsElement(arr, ele)
{
	var found = false
	var index = 0;
	while(!found && index < arr.length)
	{
		if(arr[index] == ele)
		{
			found = true;
		}
		else
		{
			index++;
		}
	}
	return found;
}

function getIndex(input) 
{
	var index = -1
	var i = 0
	var found = false;
	while (i < input.form.length && index == -1)
	{
		if (input.form[i] == input)
		{
			index = i;
		}
		else 
		{
			i++;
		}
	}
	return index;
}
/*END OF AUTOTAB*/


function submitForm(formName,whereTo)
{
	var whatForm = document.getElementById(formName);
	if(formName == 'contactForm')
	{
		whatForm.Email.email=true;
		whatForm.Phone.phone=true;
	}
	if(formName == 'appointmentForm')
	{
		whatForm.Car_Seat.optional=true;
		whatForm.Phone.phone=true;
		whatForm.Email.optional=true;
		whatForm.Comments.optional=true;
		whatForm.Seat_Purchase_Type.conditional=true;
		whatForm.Seat_Purchase_Type.condition='Car_Seat';
		whatForm.Seat_Purchase_Type.conValue='Yes';
	}
	var whatReturn = verify(whatForm); 
	if(whatReturn != false)
	{
		whatForm.action = whereTo;
		whatForm.submit();
	}
}