
var maxWidth=200;

var maxHeight=200;

var fileTypes=["gif","jpg","jpeg"];

var outImage="previewField";

var defaultPic="images/spacer.gif";



function preview(what){

  var source=what.value;

  var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();

  for (var i=0; i<fileTypes.length; i++) if (fileTypes[i]==ext) break;

  globalPic=new Image();	

  if (i<fileTypes.length) globalPic.src=source;

  else {

    globalPic.src=defaultPic;

    alert("THAT IS NOT A VALID IMAGE\nPlease load an image with an extention of one of the following:\n\n"+fileTypes.join(", "));

  }

  setTimeout("applyChanges()",200);

}

var globalPic;

function applyChanges(){

  var field=document.getElementById(outImage);

  var x=parseInt(globalPic.width);

  var y=parseInt(globalPic.height);

  if (x>maxWidth) {

    y*=maxWidth/x;

    x=maxWidth;

  }

  if (y>maxHeight) {

    x*=maxHeight/y;

    y=maxHeight;

  }

  field.style.display=(x<1 || y<1)?"none":"";

  field.src=globalPic.src;

  field.width=x;

  field.height=y;

}

 

function MM_openBrWindow(theURL,winName,features) { //v2.0

  window.open(theURL,winName,features);

}



function MM_swapImgRestore() { //v3.0

  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;

}



function MM_preloadImages() { //v3.0

  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();

    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)

    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}

}



function MM_findObj(n, d) { //v4.01

  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {

    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}

  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];

  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);

  if(!x && d.getElementById) x=d.getElementById(n); return x;

}



function MM_swapImage() { //v3.0

  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)

   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}

}



function MM_setTextOfLayer(objName,x,newText) { //v4.01

  if ((obj=MM_findObj(objName))!=null) with (obj)

	if (document.layers) { document.write(unescape(newText)); document.close();}

	else innerHTML = unescape(newText);

}





// Declaring valid date character, minimum year and maximum year

var dtCh= "-";

var minYear=1900;

var maxYear=2100;

var nameIndex = 0;

var typeIndex = 1;

var requiredIndex = 2;

var msgIndex = 3;

var jstypeIndex = 4;

var minIndex = 5;

var maxIndex = 6;

var compareToIndex = 7;

var validate = new Array();

var maxHours = 24;

var requiredTxt = 'Missing Required Field:'

var invalidTxt = 'Invalid Value:'

var secondsSinceLoad = 0;

var alertList = new Array();

function addAlert(type, name,subtitle, description,time, redirect){

	var addIndex = alertList.length;

	alertList[addIndex]= new Array();

	alertList[addIndex]['name'] = name;

	alertList[addIndex]['type'] = type;

	alertList[addIndex]['subtitle'] = subtitle;

	alertList[addIndex]['description'] = replaceAll(description, '<br>', '\n');

	alertList[addIndex]['time'] = time;

	alertList[addIndex]['done'] = 0;

	alertList[addIndex]['redirect'] = redirect;

} 

function checkAlerts(){

	secondsSinceLoad += 1;

	var mj = 0;

	var alertmsg = '';

	for(mj = 0 ; mj < alertList.length; mj++){

		if(alertList[mj]['done'] == 0){

			if(alertList[mj]['time'] < secondsSinceLoad && alertList[mj]['time'] > -1 ){

				alertmsg = alertList[mj]['type'] + ":" + alertList[mj]['name'] + "\n" +alertList[mj]['subtitle']+ "\n"+ alertList[mj]['description'] + "\n\n";

				alertList[mj]['done'] = 1;

				if(alertList[mj]['redirect'] == ''){

					alert(alertmsg);

				}else if(confirm(alertmsg)){

					window.location = alertList[mj]['redirect'];

				}

			}

		}

	}

	

	

	setTimeout("checkAlerts()", 1000);



}

function toggleDisabled(id){

		

			if(this.document.getElementById( id).disabled==true){

				this.document.getElementById( id).disabled=false;

			}else{

				this.document.getElementById(  id).disabled=true;

				}

			}

function toggleDisplay(id){

		

			if(this.document.getElementById( id).style.display=='none'){

				this.document.getElementById( id).style.display='inline'

				if(this.document.getElementById(id+"link") != undefined){

					this.document.getElementById(id+"link").style.display='none';

				}

						

			}else{

				this.document.getElementById(  id).style.display='none'

				if(this.document.getElementById(id+"link") != undefined){

					this.document.getElementById(id+"link").style.display='inline';

				}

			}

		}

function checkAll(form, field, value)

{

	for (i = 0; i < form.elements.length; i++){

		if(form.elements[i].name == field)

			form.elements[i].checked = value;

	}

}



function replaceAll(text, src, rep){

		offset = text.toLowerCase().indexOf(src.toLowerCase());

		while ( offset != -1 ) {                         

		text = text.substring(0, offset) + rep + text.substring(offset + src.length ,text.length);

		offset = text.indexOf( src, offset + rep.length + 1);

		}

		return text;

		}





function addForm(formname){

	validate[formname] = new Array();

}

function addToValidate(formname, name, type,required,  msg){

	if( typeof validate[formname] == 'undefined'){

		addForm(formname);

	}

	validate[formname][validate[formname].length] = new Array(name, type,required, msg);	

}

function addToValidateRange(formname, name, type,required,  msg,min,max){

	addToValidate(formname, name, type,required,  msg);

	validate[formname][validate[formname].length - 1][jstypeIndex] = 'range'

	validate[formname][validate[formname].length - 1][minIndex] = min;

	validate[formname][validate[formname].length - 1][maxIndex] = max;

}



function addToValidateDateBefore(formname, name, type,required,  msg,compareTo){

	addToValidate(formname, name, type,required,  msg);

	validate[formname][validate[formname].length - 1][jstypeIndex] = 'isbefore'

	validate[formname][validate[formname].length - 1][compareToIndex] = compareTo;

}





function removeFromValidate(formname, name){

	for(i = 0; i < validate[formname].length; i++){

		if(validate[formname][i][nameIndex] == name){

			validate[formname].splice(i, 1);

		}

	}

}



function toDecimal(original){

	temp = Math.round(original*100)/100;

	if((original * 100) % 100 == 0)

		return temp + '.00';

	if((original * 10) % 10 == 0)

		return temp + '0';

	return temp

}



function isInteger(s){

	var i;

    for (i = 0; i < s.length; i++){

        // Check that current character is number.

        var c = s.charAt(i);

        if (((c < "0") || (c > "9"))) return false;

    }

    // All characters are numbers.

    return true;

}



function isNumeric(s){

  if(!/^-*[0-9\.]+$/.test(s))

   {

   		return false

   }

   else

   {

   		return true;

   }

}



function stripCharsInBag(s, bag){

	var i;

    var returnString = "";

    // Search through string's characters one by one.

    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++){

        var c = s.charAt(i);

        if (bag.indexOf(c) == -1) returnString += c;

    }

    return returnString;

}



function daysInFebruary (year){

	// February has 29 days in any year evenly divisible by four,

    // EXCEPT for centurial years which are not also divisible by 400.

    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );

}

function DaysArray(n) {

	for (var i = 1; i <= n; i++) {

		this[i] = 31

		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}

		if (i==2) {this[i] = 29}

   }

   return this

}

var date_reg_positions = {'Y': 3,'m': 1,'d': 2};

var date_reg_format = '([0-9]{1,2})-([0-9]{1,2})-([0-9]{4})'

function isDate(dtStr){

	if(dtStr.length== 0){

		return true;

	}

	myregexp = new RegExp(date_reg_format)

	if(!myregexp.test(dtStr))

		return false



return true

}



function getDateObject(dtStr){

		if(dtStr.length== 0){

		return true;

	}

	myregexp = new RegExp(date_reg_format)

	var dt = myregexp.exec(dtStr)

	var yr = dt[date_reg_positions['Y']];

	var mh = dt[date_reg_positions['m']];

	var dy = dt[date_reg_positions['d']];

	var date1 = new Date();

	date1.setFullYear(yr);

	 date1.setMonth(mh);

	 date1.setDate(dy);

	return date1;

}



function isBefore(value1, value2){

	var d1 = getDateObject(value1);

	var d2 = getDateObject(value2);

	return d2 >= d1;

}



function isValidEmail(emailStr){

	if(emailStr.length== 0){

		return true;

	}

	if(!/^\w+([\.\-\+]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(emailStr))

		return false

	return true	

}



function isValidPhone(phoneStr){

	if(phoneStr.length== 0){

		return true;

	}

	if(!/^[0-9\-\(\)]+$/.test(phoneStr))

		return false

	return true	

}

function isFloat(floatStr){

	if(floatStr.length== 0){

		return true;

	}

	if(!/^[0-9\.]+$/.test(floatStr))

		return false

	return true	

}

var time_reg_format = "[0-9]{1,2}\:[0-9]{2}";

function isTime(timeStr){

	time_reg_format = time_reg_format.replace('([ap]m)', '');

	time_reg_format = time_reg_format.replace('([AP]M)', '');

	if(timeStr.length== 0){

		return true;

	}

	//we now support multiple time formats

	myregexp = new RegExp(time_reg_format)

	if(!myregexp.test(timeStr))

		return false



return true

}



function inRange(value, min, max){

	return value >= min && value <= max;

}





function trim(s) {

	if(typeof(s) == 'undefined')  

		return s;

	while (s.substring(0,1) == " ") {

		s = s.substring(1, s.length);

	}

	while (s.substring(s.length-1, s.length) == ' ') {

		s = s.substring(0,s.length-1);

	}



	return s;

}





function check_form(formname){

	return validate_form(formname, '');

}







function validate_form(formname, startsWith){

	if ( typeof (formname) == 'undefined')

	{

		return false;

	}

	if ( typeof (validate[formname]) == 'undefined')

	{

		return true;

	}

	var form = "document." + formname;

	var isError = false;

	var errorMsg = "";

	for(var i = 0; i < validate[formname].length; i++){

			if(validate[formname][i][nameIndex].indexOf(startsWith) == 0){

			if(typeof eval(form + "." + validate[formname][i][nameIndex] ) != 'undefined'){

			var bail = false;

			if(validate[formname][i][requiredIndex]){

				if(typeof eval(form + "." + validate[formname][i][nameIndex]) == 'undefined' || trim(eval(form + "." + validate[formname][i][nameIndex] + ".value")) == ""){

					errorMsg += '\n' + requiredTxt +' ' + validate[formname][i][msgIndex];

					isError = true;

					bail = true;

				}

			}

			if(!bail){

			

			switch(validate[formname][i][typeIndex]){

				case 'email':

							if(!isValidEmail(trim(eval(form + "." + validate[formname][i][nameIndex] + ".value")))){

								isError = true;

								errorMsg += "\n "+ invalidTxt + " " +	validate[formname][i][msgIndex];

							}

							 break;

				case 'time':

							if( !isTime(trim(eval(form+"." + validate[formname][i][nameIndex] + ".value")))){

								isError = true;

								errorMsg += "\n "+ invalidTxt + " " +	validate[formname][i][msgIndex];

							} break;

				case 'date': if(!isDate(trim(eval(form + "." + validate[formname][i][nameIndex] + ".value")))){

								isError = true;

								errorMsg += "\n "+ invalidTxt + " " +	validate[formname][i][msgIndex];

							}  break;

				case 'alpha': break;

				case 'alphanumeric': break;

				case 'int':if(!isInteger(trim(eval(form + "." + validate[formname][i][nameIndex] + ".value")))){

								isError = true;

								errorMsg += "\n "+ invalidTxt + " " + 	validate[formname][i][msgIndex];

							} break;

				case 'float':if(!isFloat(trim(eval(form + "." + validate[formname][i][nameIndex] + ".value")))){

							isError = true;

							errorMsg += "\n "+ invalidTxt + " " + 	validate[formname][i][msgIndex];

						} break;

				

				

			

				}

				if(typeof validate[formname][i][jstypeIndex]  != 'undefined' && !isError){

					switch(validate[formname][i][jstypeIndex]){

					case 'range':

							if(!inRange(trim(eval(form + "." + validate[formname][i][nameIndex] + ".value")), validate[formname][i][minIndex], validate[formname][i][maxIndex])){

								isError = true;

								errorMsg += "\n " + validate[formname][i][msgIndex] + " value " + eval(form + "." + validate[formname][i][nameIndex] + ".value") + " is not within the valid range (" +validate[formname][i][minIndex] + " - " + validate[formname][i][maxIndex] +  ") " 	;

							}

							break;

					case 'isbefore':

							compareTo = form + "." + validate[formname][i][compareToIndex];

							if(	typeof compareTo != 'undefined'){

								date1 = trim(eval(form + "." + validate[formname][i][nameIndex] + ".value"));

								date2 = trim(eval(compareTo + '.value'));

								if(!isBefore(date1,date2)){ 		

									isError = true;

									errorMsg += "\n " + validate[formname][i][msgIndex] + "(" + date1 + ") is not before " + date2;

								}

							}

					}

				}

		}

			

	}}}

	if (isError == true) {

				alert(errorMsg);

				

				return false;

			}

		return true;

	

}





/**

 * Displays an error message if an element of a form hasn't been completed and

 * should be

 *

 * @param   object   the form

 * @param   string   the name of the form field to put the focus on

 *

 * @return  boolean  whether the form field is empty or not

 */

function emptyFormElements(theForm, theFieldName)

{

    var isEmpty  = 1;

    var theField = theForm.elements[theFieldName];

    // Whether the replace function (js1.2) is supported or not

    var isRegExp = (typeof(theField.value.replace) != 'undefined');



    if (!isRegExp) {

        isEmpty      = (theField.value == '') ? 1 : 0;

    } else {

        var space_re = new RegExp('\\s+');

        isEmpty      = (theField.value.replace(space_re, '') == '') ? 1 : 0;

    }

    if (isEmpty) {

        theForm.reset();

        theField.select();

        alert(errorMsg0);

        theField.focus();

        return false;

    }



    return true;

} // end of the 'emptyFormElements()' function





/**

 * Ensures a value submitted in a form is numeric and is in a range

 *

 * @param   object   the form

 * @param   string   the name of the form field to check

 * @param   integer  the minimum authorized value

 * @param   integer  the maximum authorized value

 *

 * @return  boolean  whether a valid number has been submitted or not

 */

function checkFormElementInRange(theForm, theFieldName, min, max)

{

    var theField         = theForm.elements[theFieldName];

    var val              = parseInt(theField.value);



    if (typeof(min) == 'undefined') {

        min = 0;

    }

    if (typeof(max) == 'undefined') {

        max = Number.MAX_VALUE;

    }



    // It's not a number

    if (isNaN(val)) {

        theField.select();

        alert(errorMsg1);

        theField.focus();

        return false;

    }

    // It's a number but it is not between min and max

    else if (val < min || val > max) {

        theField.select();

        alert(val + errorMsg2);

        theField.focus();

        return false;

    }

    // It's a valid number

    else {

        theField.value = val;

    }



    return true;

} // end of the 'checkFormElementInRange()' function





/**

  * Checks/unchecks all options of a <select> element

  *

  * @param   string   the form name

  * @param   string   the element name

  * @param   boolean  whether to check or to uncheck the element

  *

  * @return  boolean  always true

  */

function setSelectOptions(the_form, the_select, do_check)

{

    var selectObject = document.forms[the_form].elements[the_select];

    var selectCount  = selectObject.length;



    for (var i = 0; i < selectCount; i++) {

        selectObject.options[i].selected = do_check;

    } // end for



    return true;

} // end of the 'setSelectOptions()' function

// DROPDOWN

$(document).ready(function() {
  $('ul#homenav > li').hover(function() {
    $('ul:first', this).show();
  },
  function() {
    $('ul:first', this).hide();
  });

  $('ul#nav li li').hover(function() {
    $('ul:first', this).each(function() {
      $(this).css('top', $(this).parent().position().top );
      $(this).css('left', $(this).parent().position().left + $(this).parent().width() );
      $(this).show();
    });
  },
  function() {
    $('ul:first', this).hide();
  });
});

$(document).ready(function() {
  $('ul#secnav > li').hover(function() {
    $('ul:first', this).show();
  },
  function() {
    $('ul:first', this).hide();
  });

  $('ul#nav li li').hover(function() {
    $('ul:first', this).each(function() {
      $(this).css('top', $(this).parent().position().top );
      $(this).css('left', $(this).parent().position().left + $(this).parent().width() );
      $(this).show();
    });
  },
  function() {
    $('ul:first', this).hide();
  });
});

// PRODGALL

$(function() {
    $('.prodgallphoto').cycle({ 
        timeout: 0, 
        speed:   300,
        startingSlide: 0 
    });
	
    $('#slide1').click(function() { 
        $('.prodgallphoto').cycle(0); 
        return false; 
    });
   
   $('#slide2').click(function() { 
        $('.prodgallphoto').cycle(1); 
        return false; 
    });
   
    $('#slide3').click(function() { 
       $('.prodgallphoto').cycle(2); 
       return false; 
    });
	
    $('#slide4').click(function() { 
       $('.prodgallphoto').cycle(3); 
       return false; 
    });

    $('#slide5').click(function() { 
       $('.prodgallphoto').cycle(4); 
       return false; 
    });

    $('#slide6').click(function() { 
       $('.prodgallphoto').cycle(5); 
       return false; 
    });
});

