// JavaScript Document		

 myDate = new Date

		// Customises the Form button images
		if (document.images) {
			submitOn = new Image
			submitOff = new Image
			
			// changes appearance between 4 AM  - 7 PM
		    if (myDate.getHours() >  4 && myDate.getHours() <= 9 || myDate.getHours() >  9 && myDate.getHours() <= 18){
			   submitOn.src = "Assets/images/layout_gr/forms/submit_on.jpg"
			   submitOff.src = "Assets/images/layout_gr/forms/submit_off.jpg"
	        }
	        // changes appearance between 7PM  - 4 AM
  	        if (myDate.getHours() >  18 && myDate.getHours() <= 24 || myDate.getHours() >=  1 && myDate.getHours() < 4){
			    submitOn.src = "Assets/images/layout_gr/forms/submit_on_eve.jpg"
			    submitOff.src = "Assets/images/layout_gr/forms/submit_off_eve.jpg"
	        }
		}
		else {
			submitOn = ""
			submitOff = ""
			document.subButton = ""
		}

// -----------------------------------------------------------------------------------------
// This script checks for and corrects user capitalization errors as the field loses focus (onBlur).
		// Here the field name is passed into the function
		// Script by: Darrin Norton
		function checkCaps(myField) {
		  //tests for available data within the field
		  if (myField.value != "") {
		      //creates two new arrays and a variable
           	  arrWordList = new Array
			  arrSplitWords = new Array
			  newString = ""
             
			  // Splits the incoming string into an array of single words by looking for spaces within the string
			  reSplitMe = /\s/ 
			  arrWordList = myField.value.split(reSplitMe)

                // Loops through the array of words 
			   for (i=0;i<arrWordList.length;i++) {
			   
			       // seperates the first letter from each of the words in the array
			       reFind1st = /(.){1}(.*)/
				   // applies the RegExp to the word in the array, breaking out the two parts into to RegExp variables ($1 & $2)
				   reFind1st.exec(arrWordList[i])
				   
				    // Contcatenates the two RegExpVariables and stores them in an array
		           arrSplitWords[i] = RegExp.$1.toUpperCase() + RegExp.$2.toLowerCase()
				   
				      //  Tests whether the last word is being dealt with or not - this prevents the tacking on of an unintentional space to end of the concatenated string.
                      if (i!=(arrWordList.length-1)) {
					     //  concatenates the capitalized string back into one, adds a space between words
				         newString =(newString + arrSplitWords[i] + " ")
						 
				      } else {
					     //  concatenates the capitalized string back into one, no space at end of words
			             newString =(newString + arrSplitWords[i])
					  }
			      }
			 // reassigns the string back to the form's input box
			  myField.value = newString
		   }
		}
		
// -----------------------------------------------------------------------------------------
// ensures that the state abbreviation is in all caps
		function allCaps(myField) {
			//replaces lowercase entries to all uppers
		   myField.value = myField.value.toUpperCase()
		}
//------------------------------------------------------------------------------------------	
//  ensures that the field entry is letter characters only
		function letters(x){
           x.value=x.value.replace(/([^A-Za-z])/g,"");
       }
// -----------------------------------------------------------------------------------------
//  ensures that the field entry is letter & spaces only
		function letterspace(x){
           x.value=x.value.replace(/([^A-Za-z\s.])/g,"");
       }
// -----------------------------------------------------------------------------------------
// ensures that the field entry is numerical only
     function nums(x){
          x.value=x.value.replace(/([^0-9])/g,"");
     }
	 
	 function nums10(x){	 
        x.value=x.value.replace(/([^0-9])/g,"");
	    if (x.value.length>10){
		   document.getElementById('myForm').phone.focus()
		   document.getElementById('phoneTip').style.visibility = 'visible'
		}
     }
	 
// -----------------------------------------------------------------------------------------

// ensures that the field entry is numerical only
     function zipnums(x){
          x.value=x.value.replace(/([^0-9])/g,"");
		if (x.length>9){
		   document.getElementById('myForm').zip.focus()
		   document.getElementById('zipTip').style.visibility = 'visible'
		}
     }
	 
// -----------------------------------------------------------------------------------------

// ensures that the field entry is numerical only
     function letnums(x){
          x.value=x.value.replace(/([^\sA-Za-z0-9.!#$&?,'])/g,"");
     }
	
// -----------------------------------------------------------------------------------------
        //this function is a spin off of script 9.6 for validating strings of numbers. This is tailored to US Zip Codes.
        function perfectZip(myField) {
		  // tests for an empty zip field and fires only if not empty
		  if (myField.value != "") {
		    // RegExp for either a valid 5 or 9 digit US Zip
		   reZip = /^(\d{5})[\-]?(\d{4})?$/
		   // Applies the expression to the value of the zip field
		   validZip = reZip.exec(myField.value)
			
			//tests for True and then concatenated appropriate output
			if (validZip) {
			   // Write output for short Zip
			    if (myField.value.length == 5) {
			        myField.value = validZip[1]
				// Concatenates output if using Zip + 4
			    } else {
			        myField.value = validZip[1] + "-" + validZip[2]
			    }
			}
			// Error message for anything else entered
		    else {
		        document.getElementById('zipTip').style.visibility = 'visible'
				myField.focus()
				myField.select()
			}
          }
        }


// -----------------------------------------------------------------------------------------
//Regex & function for testing validity of email address strings
         regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/

		function validateEmail(emailfield) {
			if (regex.test(emailfield.value)==false) {
				document.getElementById("emailTip").style.visibility = 'visible'
		    }
		}
// -----------------------------------------------------------------------------------------
// closes tip boxes
		function closeThis(x,z){
			document.getElementById(x).style.visibility = 'hidden';
			sentMess = false;
			if (z != ''){
			   document.getElementById(z).focus(); 
			}
		}
// -----------------------------------------------------------------------------------------
// hides email addresses from spammers
function showEmail(x){
    /***********************************************
     * Tool URL: http://www.dynamicdrive.com/emailriddler/
    * **********************************************/
    //creates an array of char code caracters that represents the email address 
    var myEmail= new Array(100,97,114,114,105,110,64,115,116,114,105,100,101,45,114,45,119,101,98,46,99,111,109)
   var myEmailString=''
   for (i=0;i<myEmail.length;i++)
   //concatenates the string
   myEmailString+=String.fromCharCode(myEmail[i])
    
	// generates a visible email link
   if (x == 'visible') {
      document.write('<a href="mailto:'+myEmailString+'">'+myEmailString+'</a><br />')
    } 
	//generates a hidden form recipient field
   if (x == 'hidden') {
	  document.write('<input type="hidden" name="recipient" value="'+myEmailString+'" /><br />')
   }
}

// hides mail address & Phone Num from spam bots
function showAddress(){
    //creates an array of char code caracters that represents the phone num 
   // edit array via ascii Table: http://www.asciitable.com/    ("HTML" Char set)
   var myPhone= new Array(32,40,56,48,50,41,38,110,98,115,112,59,50,52,53,45,51,49,53,57)
   var myPhSting=''
   for (i=0;i<myPhone.length;i++)
   //concatenates the string
   myPhSting+=String.fromCharCode(myPhone[i])
    
	// generates a visible Address
   document.write('<br />Stride-R-Web Serices, LLC<br />8 Pierce Rd.<br />North Springfield, VT 05150<br /><div class="flRight">Phone: ' + myPhSting + '</div>Contact: Darrin Norton')
}


//-----------------------------------------------------------------------------------
function refinephone(textbox){
   var str = textbox.value
   str = str.replace(/([ ()-])/g,"");
   var s1 = str.substring(0,3)
   var s2 = str.substring(3)
   s2 = s2.substring(0,3)
   var s3 = str.substring(6)
   var re = new RegExp('/[^0-9]{10}$');
   str = s1+s2+s3;
   if (str.length !=10 && str.length > 0){
	  document.getElementById('myForm').phone.focus()
	  document.getElementById('phoneTip').style.visibility = 'visible'
   }else{
	  if (str.length > 0){
        textbox.value = '(' + s1 + ') ' + s2 + '-' + s3;
	  }
   }
}

function phonemask(textbox){
   var str = textbox.value
   
   for (var i = 0; i <= 3; i++){
	 for (var k = 0; k <= str.length; k++){
       if (k==0){
		   if (str != ''){
             if (str.substring(k, k+1) != '('){
	           str = '(' + str
	         }
		   }
	   }else{
	     if (k==4){
	        if (str.substring(k, k+1) != ')'){
	          str = str + ') '
	        }
	     }else{
            if (k==9){
	           if (str.substring(k, k+1) != '-'){
	              str = str.substring(0,k) + '-' + str.substring(k,str.length);
	           }
	        }else{
			   if (k==5){
				  if (str.substring(k, k+1) != ' '){
	                str = str.substring(0,k) + ' ' + str.substring(k,str.length);
				  }
	           }
		    }
	    }
	  }
	 }
   }
 textbox.value = str
}


function replace(fullString,text,by) {
// Replaces text with by in string
    var strLength = fullString.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return fullString;

    var i = fullString.indexOf(text);
    if ((!i) && (text != fullString.substring(0,txtLength))) return fullString;
    if (i == -1) return fullString;

    var newstr = fullString.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(fullString.substring(i+txtLength,strLength),text,by);

    return newstr;
}

// -----------------------------------------------------------------------------------------
// does final form test and submits form
 function subForm() {
	   // checks for empty values before submitting form
			if ((document.getElementById('myForm').fname.value != "") && (document.getElementById('myForm').lname.value != "") && (document.getElementById('myForm').email.value != "")) {
				var messcontent = document.getElementById('myForm').message.value.replace(/([^A-Za-z])/g,"");
				// removes empty spaces from message field and then tests for content
				if (messcontent != "") {
					//submits form if all required fields are okay.
					document.getElementById('myForm').submit()
				} else {
					// alternate instruction for negative test result
					document.getElementById('myForm').message.focus()
					document.getElementById('fieldsTip').style.visibility = 'visible'
				}
				
			} else {
				// alternate for negative results of testing the other 4 required fields
				document.getElementById('fieldsTip').style.visibility = 'visible'
			}
	}
// -------------------------------------------------------------------------------
// functions to save user data as cookies

  // Creates and initializes a new Date object
  expireDate = new Date
  expireDate.setMonth(expireDate.getMonth()+12)

    // This function sets cookies using name / value pairs passed to it from function saveIt.
function setCookie(name,value){
	
	// Writes the cookie using passed variable values
	document.cookie = name + "=" + value + ";expires=" + expireDate.toGMTString() 

}

// -------------------------------------------------------------------------------
  // This function processes the form field data
function saveIt() {
	if (document.getElementById('myForm').ckRemember.checked){
	    //  Test form field value for empties
	    if (document.getElementById('myForm').fname.value){
		     // calls the function and passes it the field name and value pair
		    setCookie('fname',document.getElementById('myForm').fname.value)
		}
		 //  Test form field value for empties
	   if (document.getElementById('myForm').lname.value){
		     // calls the function and passes it the field name and value pair
		    setCookie('lname',document.getElementById('myForm').lname.value)
	   }
		//  Test form field value for empties
	   if (document.getElementById('myForm').address.value){
		     // calls the function and passes it the field name and value pair
		    setCookie('address',document.getElementById('myForm').address.value)
	   }
	    //  Test form field value for empties
      if (document.getElementById('myForm').city.value){
		     // calls the function and passes it the field name and value pair
		    setCookie('city',document.getElementById('myForm').city.value)
	   }
	    //  Test form field value for empties
	   if (document.getElementById('myForm').state.value){
		     // calls the function and passes it the field name and value pair
		    setCookie('state',document.getElementById('myForm').state.value)
	    }
	  //  //  Test form field value for empties
	   if (document.getElementById('myForm').zip.value){
		     // calls the function and passes it the field name and value pair
		    setCookie('zip',document.getElementById('myForm').zip.value)
	   }
	    //  Test form field value for empties
      if (document.getElementById('myForm').email.value){
		     // calls the function and passes it the field name and value pair
		    setCookie('email',document.getElementById('myForm').email.value)
      }
	    //  Test form field value for empties
	  if (document.getElementById('myForm').phone.value){
		     // calls the function and passes it the field name and value pair
		    setCookie('phone',document.getElementById('myForm').phone.value)
	  }
	} else {
		//deletes any existing cookies
		if (document.cookie != ""){
		   newExpireDate = new Date
		   newExpireDate.setDate(newExpireDate.getDate()-1)
		   
		   arrThisCookie = document.cookie.split("; ")
		   
		   for (i=0;i<arrThisCookie.length;i++) {
			   cookieName =  arrThisCookie[i].split("=")[0]
			   
			   document.cookie = cookieName+"=;expires="+ newExpireDate.toGMTString()
		   }
		}
	}
}

// -----------------------------------------------------------------------------------------------------------------------------

//  This function retrieves any previously stored cookie data and outputs it to the form during page onload
function getEm(){

      // tests for available data
     if (document.cookie!=""){
		  
	      //Tests for null returns
		  if (readCookie('mailSuccess') != null){
			  if (readCookie('mailSuccess') == "senttrue"){
				  document.getElementById('sentTip').style.visibility = 'visible'
			  }else{
				  if (readCookie('mailSuccess') == "sentfalse"){
					 document.getElementById('sentMess').innerHTML="<p><strong class='red'>Message Error:</strong><br />Sorry, the delivery of your message was NOT successfull.</p><p>Please try again ...or use one of the other contact methods provided.</p>"
				     document.getElementById('sentTip').style.visibility = 'visible'
				  }else{
				     document.getElementById('sentTip').style.visibility = 'hidden'
				  }
				  
			  }
			  setCookie('mailSuccess','');
		  }
		  
		  if (readCookie('fname') != null) { 
		      document.getElementById('myForm').ckRemember.checked = true;
			  // calls the readCookie function and returns its value into the form fields
              document.getElementById('myForm').fname.value = readCookie('fname') 
		  } else {
		      // in case of Null, this will load the form as empty fields
		     document.getElementById('myForm').fname.value = ""
		  }
		     
	      
		  if (readCookie('lname') != null) {
			  // calls the readCookie function and returns its value into the form fields
	         document.getElementById('myForm').lname.value = readCookie('lname')		 
		  } else {
		     // in case of Null, this will load the form as empty fields
		     document.getElementById('myForm').lname.value = ""
		  }
		  
		  if (readCookie('address') != null) {
			  // calls the readCookie function and returns its value into the form fields
	         document.getElementById('myForm').address.value = readCookie('address')		 
		  } else {
		     // in case of Null, this will load the form as empty fields
		     document.getElementById('myForm').address.value = ""
		  }
		  
		  if (readCookie('city') != null) {
			  // calls the readCookie function and returns its value into the form fields
	         document.getElementById('myForm').city.value = readCookie('city')		 
		  } else {
		     // in case of Null, this will load the form as empty fields
		     document.getElementById('myForm').city.value = ""
		  }
		  
		  if (readCookie('state') != null) {
			  // calls the readCookie function and returns its value into the form fields
	         document.getElementById('myForm').state.value = readCookie('state')		 
		  } else {
		     // in case of Null, this will load the form as empty fields
		     document.getElementById('myForm').state.value = ""
		  }
		  
		  if (readCookie('zip') != null) {
			  // calls the readCookie function and returns its value into the form fields
	         document.getElementById('myForm').zip.value = readCookie('zip')		 
		  } else {
		     // in case of Null, this will load the form as empty fields
		     document.getElementById('myForm').zip.value = ""
		  }
		  
		  if (readCookie('email') != null) {
			  // calls the readCookie function and returns its value into the form fields
	         document.getElementById('myForm').email.value = readCookie('email')		 
		  } else {
		     // in case of Null, this will load the form as empty fields
		     document.getElementById('myForm').email.value = ""
		  }
		  
		  if (readCookie('phone') != null) {
			  // calls the readCookie function and returns its value into the form fields
	         document.getElementById('myForm').phone.value = readCookie('phone')		 
		  } else {
		     // in case of Null, this will load the form as empty fields
		     document.getElementById('myForm').phone.value = ""
		  }
	}
}

// -------------------------------------------------------------------------------------------------
// This function does the actual cookie reading
function readCookie(cookieName){

    //creates a variable and adds an equal sign to the passed cookiename
	 var cookieNameToFind = cookieName + "="
	
	 // creates an array variable and splits the cookie's name/value pairs
	 var arrCookies = document.cookie.split(';')
	
	// loops through the array and finds the requested cookie by name
     for(var i=0;i < arrCookies.length;i++) {
	
	     // creates a variable and initializes it with a value from the array
		var desiredValue = arrCookies[i]
		
		//Loops through looking for blank preceeding spaces
		 while (desiredValue.charAt(0)==' ') {
		 
		     // Strips any blank preceeding spaces
		    desiredValue = desiredValue.substring(1,desiredValue.length)
			}
		
		// tests if found cookie is the one we seek
	    if (desiredValue.indexOf(cookieNameToFind) == 0) {
		
		    // returns the cookie's value
	         return desiredValue.substring(cookieNameToFind.length,desiredValue.length)
		 }
	 }
	// returns null if our cookie is not found
	 return null

}

var sentMess = false;
function showTip(tip){
	myTip=document.getElementById(tip).style
	if (document.getElementById('sentTip').style.visibility == 'visible'){
	  sentMess = true;
	  document.getElementById('sentTip').style.visibility = 'hidden'	
	}
	if (myTip.visibility="hidden") {
		myTip.visibility="visible"
	} 
}

function hideTip(tip){
	myTip=document.getElementById(tip).style
	if (myTip.visibility="visible") {
		myTip.visibility="hidden"
	} 
	if(sentMess){
		document.getElementById('sentTip').style.visibility = 'visible'	
	}
}


