// JavaScript Document

function validate_required(field)
{
	with (field)
	{
		if (value==null||value=="")
		{
			return false;
		} else {
			field.focus();
			return true;
		}
	}
}
function validate_expression(field, expr)
{
	var ex = '';
	switch (expr) {
		case 'alpha':
			ex = /^[a-zA-Z ]+$/;
			break;
		case 'number':
			ex  = /^[0-9]+$/;
			break;
		case 'alphanum':		
			ex = /^[0-9a-zA-Z ]+$/;
			break;
		case 'email':
			ex = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-Z0-9]{2,4}$/;	
			break;
		default: // alpha
			ex = /^[a-zA-Z ]+$/;
			break;
	}

	var val = field.value.replace(/ /,'');
	if (val.match(ex)) {
		return true;	
	} else {
		field.focus();
		return false;
	}
}

function compare_values(field_a, field_b) {
	if (field_a.value == field_b.value) {
		return true;
	} else {
		field_a.focus();
		return false;
	}
}

function checkContactForm(form)
{
	var pass = true;
	var msg = '';
	
	if(!validate_required(form.name)) {
		pass = false;
		msg = msg + '  Your Name\n';
		form.name.focus();
	}
	if(!validate_required(form.email) || !validate_expression(form.email,'email')) {
		pass = false;
		msg = msg + '  Your Email Address\n';
		form.email.focus();
	}
    idx = form.distributor.selectedIndex;
    if(idx == 0) {
    	pass = false;
        msg = msg + '  Select a Distributor\n';
    }
    
	if(msg.length > 0) {
		alert('Please complete or correct the following:\n' + msg);
	}
	return pass;
}

function checkDrAl(form)
{
	var pass = true;
	var msg = '';
	
	if(!validate_required(form.name)) {
		pass = false;
		msg = msg + '  Your Name\n';
		form.name.focus();
	}
	if(!validate_required(form.email) || !validate_expression(form.email,'email')) {
		pass = false;
		msg = msg + '  Your Email Address\n';
		form.email.focus();
	}
	if(!validate_required(form.describeIssue)) {
		pass = false;
		msg = msg + '  Your Question for Dr. Al\n';
		form.describeIssue.focus();
	}
	if(msg.length > 0) {
		alert('Please complete or correct the following:\n' + msg);
	}
	return pass;
}

function confirm_delete(obj_name, id, page) {
	if (confirm("Are you sure you want to delete " + obj_name + "?")) {
		window.location.href = page + "?id=" + id + "&action=remove";
	}
}


function swapClass(elem, val) {
	document.getElementById(elem).className = val;
}
/* BEGIN GALLERY SCROLL */
function setImage(elem, pos) {
	elem.src = './assets/images/thumb/' + imgs[pos] + '/';
	elem.alt = caps[pos];
}
function setHref(elem, pos) {
	elem.href = './gallery/' + refs[pos] + '.html';
}
function scrollBack() {
	var start = document.getElementById('imgStart');
	//var end = document.getElementById('imgEnd');
	var img0 = document.getElementById('imgScroll0');
	var img1 = document.getElementById('imgScroll1');
	var img2 = document.getElementById('imgScroll2');
	var lnk0 = document.getElementById('lnkScroll0');
	var lnk1 = document.getElementById('lnkScroll1');
	var lnk2 = document.getElementById('lnkScroll2');
	var pos = start.value;

	if(pos == 0)
		pos = imgs.length;
	pos--;
	setImage(img0, pos);
	setHref(lnk0, pos);
	start.value = pos;
	pos++;

	if(pos >= (imgs.length))
		pos = 0;
	setImage(img1, pos);
	setHref(lnk1, pos);
	pos++;

	if(pos >= (imgs.length))
		pos = 0;	
	setImage(img2, pos);
	setHref(lnk2, pos);

}
function scrollAhead() {
	var start = document.getElementById('imgStart');
	//var end = document.getElementById('imgEnd');
	var img0 = document.getElementById('imgScroll0');
	var img1 = document.getElementById('imgScroll1');
	var img2 = document.getElementById('imgScroll2');
	var lnk0 = document.getElementById('lnkScroll0');
	var lnk1 = document.getElementById('lnkScroll1');
	var lnk2 = document.getElementById('lnkScroll2');
	var pos = start.value;
	if(pos >= imgs.length-1)
		pos = -1;
	pos++;

	setImage(img0, pos);
	setHref(lnk0, pos);
	start.value = pos;
	pos++;

	if(pos >= imgs.length-1)
		pos = 0;
	setImage(img1, pos);
	setHref(lnk1, pos);
	pos++;

	if(pos >= imgs.length-1)
		pos = 0;	
	setImage(img2, pos);
	setHref(lnk2, pos);
}
/* END GALLERY SCROLL */


/* BEGIN PRELOAD IMAGES FUNCTIONS */
function MM_swapImgRestore()
{
  var i;
  if(document.MM_sr) for(i=0; i < document.MM_sr.length; i++)
    if(document.MM_sr[i] && document.MM_sr[i].oSrc)
	  document.MM_sr[i].src = document.MM_sr[i].oSrc;
}

function MM_preloadImages()
{
  var i,j;

  if(!document.MM_p) document.MM_p = new Array();

  for(i = 0, j = document.MM_p.length; i < MM_preloadImages.arguments.length; i++) if(MM_preloadImages.arguments[i].indexOf("#") != 0)
  {
    document.MM_p[j] = new Image;
    document.MM_p[j].src = MM_preloadImages.arguments[i];
    j++;
  }
}

function MM_swapImage(o, m)
{
  var x = document.getElementById(o);
  document.MM_sr = new Array;
  if(x)
  {
    document.MM_sr[0] = x;
    x.oSrc = x.src;
    x.src = m;
  }
}

function MM_swapImages(o, m)
{
  var x,i,j;
  document.MM_sr = new Array;
  if(o.length != m.length) return;
  for(i = 0, j = 0; i < o.length; i++)
  {
    x = document.getElementById(o[i]);
    if(x)
    {
      document.MM_sr[j] = x;
      x.oSrc = x.src;
      x.src = m[i];
	  j++;
    }
  }
}
/* END PRELOAD IMAGES FUNCTIONS */

/* BEGIN TESTIMONIAL ROTATOR */
var current = 0; // start counter value
var interval = 15; // number of seconds to rotate
var rotItems = new Array();
var rotSrcs = new Array();
rotItems[0] = 'Welcome to our web site. I want to thank you for visiting and say how appreciative we are that you want to learn about us and our products. You, all people, and the care of our environment mean everything to us, it is why we do what we do! There is a quote I would like to share with you: \"The Earth is special, it is beautiful, it is delicate. The People, the mountains, the waters, the forests, the skies, the deserts, the prairies - all life and all things are miracles. They deserve respect, they deserve appreciation, they deserve to be taken care of.\"  Please enjoy our site.';
rotSrcs[0] = 'Dan - President of NGI';

function rotator() {
	document.getElementById('testimonial').innerHTML = rotItems[current];
	document.getElementById('source').innerHTML = rotSrcs[current];
	current = (current==(rotItems.length-1)) ? 0 : current + 1; // increment or reset
	setTimeout("rotator()", interval*1000);
}
/* END TESTIMONIAL ROTATOR*/

/* BEGIN MENU LOAD */
window.onload = function()
{
	// load menu
   var ids = new Array("nav");

   for(var i = 0; i < ids.length; i++)
   {
       var sfEls = document.getElementById(ids[i]).getElementsByTagName("LI");
       for (var i = 0; i < sfEls.length; i++) {
           sfEls[i].onmouseover = function() {
               if(!this.oldClassName)
                   this.oldClassName = this.className.replace(new RegExp("\\bsfhover\\b"), "");
               this.className += " sfhover";
           }
           sfEls[i].onmouseout = sfEls[i].onclick = function() {
               this.className = this.oldClassName;
           }
       }
   }
   // do Rotator
	if(document.getElementById('testimonial')) {
	   rotator();
	}
}
/* END MENU LOAD */


function gotoDistributor() {
	if(document.getElementById('ddlDistributor')) {
		var idx = document.getElementById('ddlDistributor').selectedIndex;
        var val = document.getElementById('ddlDistributor').options[idx].value;
//        alert(val);
        if(val == 'Other') {
        	window.location = '/contactus.html';
        } else if(parseInt(val)) {
			window.location = './products/distributors.html#' + parseInt(val);
		}
	}
}