﻿//Routine to randomize display of home page banner image
//The JavaScript Source!! http://javascript.internet.com 
//======================================================

//Set up three arrays for the image files to be used (left, middle, right).
var theImagesL = new Array() // do not change this
var theImagesM = new Array() // do not change this
var theImagesR = new Array() // do not change this

//To add more image files, continue with the
//pattern below, adding to the array.
theImagesL[0]="images/banner01_0.gif";
theImagesL[1]="images/banner1_1.jpg";
theImagesL[2]="images/banner1_2.jpg";
theImagesL[3]="images/banner1_3.jpg";
theImagesL[4]="images/banner1_4.jpg";
theImagesL[5]="images/banner1_5.jpg";
theImagesL[6]="images/banner1_6.jpg";
theImagesL[7]="images/banner1_7.jpg";
theImagesL[8]="images/banner1_8.jpg";
theImagesL[9] = "images/banner1_9.jpg";
theImagesL[10] = "images/banner1_10.jpg";
theImagesL[11] = "images/banner1_11.jpg";
theImagesL[12] = "images/banner1_12.jpg";
theImagesL[13] = "images/banner1_13.jpg";
theImagesL[14] = "images/banner1_14.jpg";
theImagesL[15] = "images/banner1_15.jpg";
theImagesL[16] = "images/banner1_15.jpg";
theImagesL[17] = "images/banner1_16.jpg";
theImagesL[18] = "images/banner1_17.jpg";
theImagesL[19] = "images/banner1_19.jpg";
theImagesL[20] = "images/banner1_20.jpg";
theImagesL[21] = "images/banner1_21.jpg";

theImagesM[0]="images/banner02_0.gif";
theImagesM[1]="images/banner2_1.jpg";
theImagesM[2]="images/banner2_2.jpg";
theImagesM[3]="images/banner2_3.jpg";
theImagesM[4]="images/banner2_4.jpg";
theImagesM[5]="images/banner2_5.jpg";
theImagesM[6]="images/banner2_6.jpg";
theImagesM[7]="images/banner2_7.jpg";
theImagesM[8]="images/banner2_8.jpg";
theImagesM[9] = "images/banner2_9.jpg";
theImagesM[10] = "images/banner2_10.jpg";
theImagesM[11] = "images/banner2_11.jpg";
theImagesM[12] = "images/banner2_12.jpg";
theImagesM[13] = "images/banner2_13.jpg";
theImagesM[14] = "images/banner2_14.jpg";
theImagesM[15] = "images/banner2_15.jpg";
theImagesM[16] = "images/banner2_16.jpg";
theImagesM[17] = "images/banner2_17.jpg";
theImagesM[18] = "images/banner2_18.jpg";
theImagesM[19] = "images/banner2_19.jpg";
theImagesM[20] = "images/banner2_20.jpg";
theImagesM[21] = "images/banner2_21.jpg";

theImagesR[0]="images/banner03_0.jpg";
theImagesR[1]="images/banner3_1.jpg";
theImagesR[2]="images/banner3_2.jpg";
theImagesR[3]="images/banner3_3.jpg";
theImagesR[4]="images/banner3_4.jpg";
theImagesR[5]="images/banner3_5.jpg";
theImagesR[6]="images/banner3_6.jpg";
theImagesR[7]="images/banner3_7.jpg";
theImagesR[8]="images/banner3_8.jpg";
theImagesR[9] = "images/banner3_9.jpg";
theImagesR[10] = "images/banner3_10.jpg";
theImagesR[11] = "images/banner3_11.jpg";
theImagesR[12] = "images/banner3_12.jpg";
theImagesR[13] = "images/banner3_13.jpg";
theImagesR[14] = "images/banner3_14.jpg";
theImagesR[15] = "images/banner3_15.jpg";
theImagesR[16] = "images/banner3_16.jpg";
theImagesR[17] = "images/banner3_17.jpg";
theImagesR[18] = "images/banner3_18.jpg";
theImagesR[19] = "images/banner3_19.jpg";
theImagesR[20] = "images/banner3_20.jpg";
theImagesR[21] = "images/banner3_21.jpg";

//do not edit anything below this line
//====================================
var cntL = theImagesL.length;
var cntM = theImagesM.length;
var cntR = theImagesR.length;

/*
//Precache all three image arrays
var preBufferL = new Array()
for (i = 0; i < cntL; i++) {
    preBufferL[i] = new Image()
    preBufferL[i].src = theImagesL[i]
}

var preBufferM = new Array()
for (i = 0; i < cntM; i++) {
    preBufferM[i] = new Image()
    preBufferM[i].src = theImagesM[i]
}

var preBufferR = new Array()
for (i = 0; i < cntR; i++) {
    preBufferR[i] = new Image()
    preBufferR[i].src = theImagesR[i]
}
*/

//Get index setting for last banner displayed, if any
var sLastIndex = '';
var cookiename = 'banner';
sLastIndex = Get_Cookie(cookiename);
//alert(sLastIndex)
if (sLastIndex) {
    var iLastIndex = sLastIndex.split('|');
} else {
    var iLastIndex = new Array(2);
    iLastIndex[0] = "-1";
    iLastIndex[1] = "-1";
    iLastIndex[2] = "-1";
}
//alert(sLastIndex + "\n" + iLastIndex[0] + ", " + iLastIndex[1] + ", " + iLastIndex[2] ) ;

//Get random index of left, middle and right banner to display now; if same as last index, retry until different;
var whichImageL = Math.round(Math.random() * (cntL - 1));
if ( parseInt(iLastIndex[0]) > -1) {
    while (whichImageL == parseInt(iLastIndex[0])) {
        whichImageL = Math.round(Math.random() * (cntL - 1));
    }
}

var whichImageM = Math.round(Math.random() * (cntM - 1));
if (parseInt(iLastIndex[1]) > -1) {
    while (whichImageM == parseInt(iLastIndex[1])) {
        whichImageM = Math.round(Math.random() * (cntM - 1));
    }
}

var whichImageR = Math.round(Math.random() * (cntR - 1));
if (parseInt(iLastIndex[2]) > -1) {
    while (whichImageR == parseInt(iLastIndex[2])) {
        whichImageR = Math.round(Math.random() * (cntR - 1));
    }
}
//alert('whichImageL: ' + whichImageL + '\nwhichImageM: ' + whichImageM + '\nwhichImageR: ' + whichImageR);

//save index of banners to be displayed
Set_Cookie(cookiename, whichImageL + "|" + whichImageM + "|" + whichImageR, '', '/', '', ''); 

//Write image tag based on computed image index;
function showBanner() {
    var pfx = '<td style="background-color: #685530">';
    var sfx = '</td>';
    
    var stmp = pfx + '<img src="' + theImagesL[whichImageL] + '" width="280" height="186" alt="">' + sfx;
    stmp += pfx + '<img src="' + theImagesM[whichImageM] + '" width="200" height="186" alt="" hspace="1">' + sfx;
    stmp += pfx + '<img src="' + theImagesR[whichImageR] + '" width="255" height="186" alt="">' + sfx;
    
    document.write(stmp);
}


/***************************************************************************
Script Name: Javascript Cookie Script
Author: Public Domain, with some modifications
Script Source URI: http://techpatterns.com/downloads/javascript_cookies.php
Version 1.1.1
Last Update: 4 October 2007

Changes:
1.1.1 fixes a problem with Get_Cookie that did not correctly handle case
where cookie is initialized but it has no "=" and thus no value, the 
Get_Cookie function generates a NULL exception. This was pointed out by olivier, thanks

1.1.0 fixes a problem with Get_Cookie that did not correctly handle
cases where multiple cookies might test as the same, like: site1, site

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
*****************************************************************************/

// this fixes an issue with the old method, ambiguous values 
// with this test document.cookie.indexOf( name + "=" );

// To use, simple do: Get_Cookie('cookie_name'); 
// replace cookie_name with the real cookie name, '' are required
function Get_Cookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');


        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}

/*
only the first 2 parameters are required, the cookie name, the cookie
value. Cookie time is in milliseconds, so the below expires will make the 
number you pass in the Set_Cookie function call the number of days the cookie
lasts, if you want it to be hours or minutes, just get rid of 24 and 60.

Generally you don't need to worry about domain, path or secure for most applications
so unless you need that, leave those parameters blank in the function call.
*/
function Set_Cookie(name, value, expires, path, domain, secure) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());
    // if the expires variable is set, make the correct expires time, the
    // current script below will set it for x number of days, to make it
    // for hours, delete * 24, for minutes, delete * 60 * 24
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    //alert( 'today ' + today.toGMTString() );// this is for testing purpose only
    var expires_date = new Date(today.getTime() + (expires));
    //alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

    document.cookie = name + "=" + escape(value) +
		((expires) ? ";expires=" + expires_date.toGMTString() : "") + //expires.toGMTString()
		((path) ? ";path=" + path : "") +
		((domain) ? ";domain=" + domain : "") +
		((secure) ? ";secure" : "");
}

// this deletes the cookie when called
function Delete_Cookie(name, path, domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
			((path) ? ";path=" + path : "") +
			((domain) ? ";domain=" + domain : "") +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

//Routine to populate PDF/Word file on fileviewer.htm page
//========================================================
function displayFile() {
    var path = unescape(gup('path'));
    
    var ht = gup('ht');
    ht = (ht.length > 0) ? ht : "600";
    
    var wd = gup('wd');
    wd = (wd.length > 0) ? wd : "775";

    var title = unescape(gup('title'));
    if (title.length > 0) {
        document.write('<div class="header2">' + title + '</div><br/><br/><br/>');
    } else {
        document.write('<br/><br/><br/><br/>');
    }

    if (path.length > 0) {
        var stmp = '<embed wmode="transparent" height="' + ht + '" width="' + wd + '" src="' + path + '" />'
        document.write(stmp);
    } else {
        document.write('');
    }
}

//Function to retrieve value for passed-in URL parameter
function gup(name) {
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regexS = "[\\?&]" + name + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
        return "";
    else
        return results[1];
}

//Routine to populate search box on searchresults.htm with last query
//===================================================================
function getQueryString() {
    var url = top.location.href;
    var param = "&q=";
    var queryHash = url.substring(url.indexOf(param) + param.length, url.length);
    var posAmp = queryHash.indexOf("&");
    var posPnd = queryHash.indexOf("#");
    if (posAmp > 0) {
        var query = unescape(queryHash.substring(0, posAmp));
    } else if (posPnd > 0) {
        var query = unescape(queryHash.substring(0, posPnd));
    } else {
        var query = unescape(queryHash);
    }
    //Replace any "+" with a space
    query = query.replace(/\+/g, " ");
    
    //Set input 'q' in form 'GooogleSearchBox'
    document.getElementById("GooogleSearchBox").q.value = query;
    //alert("query: " + query);
}

//Routine to validate Contact Us form and submit if ok
//=====================================================
function checkContactUs() {
    var fm = document.forms["ContactUs"];
    var fName = fm.FirstName;
    var lName = fm.LastName;
    var eMail = fm.eMail;
    var phone = fm.Phone;
    var sMsg = "";
    
    //Validate first name
    if (fName.value == "") {
        sMsg = "Please enter your first name.";
        alert(sMsg);
        fName.focus();
        return false;
    }

    //Validate last name
    if (lName.value == "") {
        sMsg = "Please enter your last name.";
        alert(sMsg);
        lName.focus();
        return false;
    }

    //Validate that phone entered is numeric after removing any parentheses, dashes, spaces, and dots.
    if (phone.value.length > 0) {
        var IsNumber = true;
        var ValidChars = "0123456789.-() ";
        var Char;
        var strng = phone.value;
        //var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters        
        
        for (i = 0; i < strng.length && IsNumber == true; i++) {
            Char = strng.charAt(i);
            if (ValidChars.indexOf(Char) == -1) {
                IsNumber = false;
            }
        }
        
        if (!(IsNumber)){
            alert("The phone number contains illegal characters. Please re-enter.");
            return false;
        } else if (strng.length < 10) {
            alert("The phone number need to be at least 10 digits. Please re-enter.");
            return false;
        }
    }
    
    //Validate that e-mail format is valid.
    if (eMail.value.length > 0) {
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        if (reg.test(eMail.value) == false) {
            alert('The e-mail address entered is invalid. Please re-enter.');
            return false;
        }
    }
    
    //Validate that either e-mail or phone entered
    if (eMail.value == "" && phone.value == "") {
        sMsg = "Please enter either an e-mail address or phone number so we can contact you. ";
        alert(sMsg);
        eMail.focus();
        return false;
    }

    //Validate that at least one checkbox is checked.
    var noChk = true;  //assume no box checked
    for (var i = 1; i < 7; i++) {
        var cbox = document.getElementById("Checkbox" + i);
        if (cbox && cbox.checked) {
            noChk = false;
            break;
        }
    }
    if (noChk) {
        sMsg = "Please check at least one box so we can properly direct your inquiry.";
        alert(sMsg);
        fm.Checkbox1.focus();
        return false;
    }

    //Validate Comments non-null for checkbox 1 and 2
    if ((fm.Checkbox1.checked || fm.Checkbox2.checked) && fm.comments.value == "") {
        sMsg = "Please enter comments based on your checkbox selection.";
        alert(sMsg);
        fm.comments.focus();
        return false;
    }
    //Submit if we got this far
    fm.submit();
}
