// JavaScript Document

function updateLocations(form) {
	resetLocations(form);

	var idx = form.country.selectedIndex;
	var val = form.country.options[idx].value;
	var country1 = new Array();
	country1[0] = 'Choose Location';
	country1[1] = 'Minnesota';
	try {
		var locs = eval('country' + val);
		for(i in locs)
		{
			var opt = document.createElement('option');
			opt.text = locs[i];
			opt.value = locs[i];
			try {
				form.location.add(opt, null); // standards compliant; doesn't work in IE
			}
			catch(ex) {
				form.location.add(opt); // IE only
			}
		}
		form.location.style.display = 'block';
	} 
	catch(ex) {
	
	}
	//form.country.disabled = true;
}
function resetLocations(form) {
	// clean out existing options
	for (x = form.location.length - 1; x>=0; x--) {
		form.location.remove(x);
	}
	form.location.style.display = 'none';
	form.country.disabled = false;
}


// number formatting function
// copyright Stephen Chapman 24th March 2006, 22nd August 2008
// permission to use this function is granted provided
// that this copyright notice is retained intact
function formatNumber(num,dec,thou,pnt,curr1,curr2,n1,n2) {
	var x = Math.round(num * Math.pow(10,dec));
	if (x >= 0) 
		n1=n2='';
	var y = (''+Math.abs(x)).split('');
	var z = y.length - dec; 
	if (z<0) 
		z--; 
	for(var i = z; i < 0; i++) 
		y.unshift('0'); 
	if (z<0) 
		z = 1; 
	y.splice(z, 0, pnt); 
	if(y[0] == pnt) 
		y.unshift('0'); 
	while (z > 3) {
		z-=3; 
		y.splice(z,0,thou);
	}
	var r = curr1+n1+y.join('')+n2+curr2;
	return r;
}
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 'float':
			ex = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
			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 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 detect_ie() {
	var ver = 0;
	var msg = '';
	var ua = navigator.userAgent;
	var av = navigator.appVersion;
	if (ua != null)
	{
		if (ua.indexOf("MSIE") != -1)
		{
			if (av.indexOf("MSIE")!=-1){
				var temp=av.split("MSIE");
				ver=parseFloat(temp[1]);
			}
		}
	}
	return ver;
}

function gotoUri(uri) {
	window.location = uri;
}
function gotoAnimalPage(form) {
	var ddl = form.animal;
	var idx = form.animal.selectedIndex;
	var val = form.animal.options[idx].value;
	window.location = './facts/animals/' + val + '.html';
}


function checkContactForm()
{
	var pass = true;
	var msg = '';
	var name = document.getElementById('contactname');
	var email = document.getElementById('contactemail');
	var phone = document.getElementById('contactphone');
	
	if(!validate_required(name)) {
		pass = false;
		msg = msg + '  Your Name\n';
		document.getElementById('contactname').focus();
	}
	if(!validate_required(email) || !validate_expression(email,'email')) {
		pass = false;
		msg = msg + '  Your Email Address\n';
		document.getElementById('contactemail').focus();
	}	
	if(msg.length > 0) {
		alert('Please complete or correct the following:\n' + msg);
	}
	return pass;
}

function checkSignup(form) {
	if(!validate_required(form.email) || !validate_expression(form.email, 'email')) {
		alert('Please enter a valid email address');
		return false;
	}
	return true;
}
function toggleTestimonial(elem) {
	var el = document.getElementById('testimonial' + elem);
	if(el.style.display == 'block')
		el.style.display = 'none';
	else
		el.style.display = 'block';
	return false;
}
function toggleTrivia(elem, answer) {
	if(document.getElementById(elem + '_' + answer).className == '') {
		document.getElementById(elem + '_' + answer).className = 'on';
		document.getElementById(elem + '_answer').style.display = 'block';
	} else {
		document.getElementById(elem + '_' + answer).className = '';
		document.getElementById(elem + '_answer').style.display = 'none';
	}
	return false;
}


/* 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 DROP-DOWN MENU */
function loadMenu() {
   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;
           }
       }
   }
}
function loadCritters() {
	var ids = new Array("critterList");
	//alert('loadCritters');
	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;
		   }
	   }
	}
}
/* END DROP-DOWN MENU */

/* BEGIN GALLERY FUNCTIONS */
function updatePhoto(id, caption) {
	document.getElementById('photoMain').src = './assets/std/' + id + '/';
	document.getElementById('photoMain').alt = caption;
	document.getElementById('caption').innerHTML = caption;
	document.getElementById('fullImage').src = './assets/lg/' + id + '/';
	document.getElementById('fullImage').alt = caption;
	document.getElementById('fullCaption').innerHTML = caption;
}
function showFullImage() {
	document.getElementById('fullImageUberContainer').style.display = 'block';
}
function hideFullImage() {
	document.getElementById('fullImageUberContainer').style.display = 'none';
}
/* END GALLERY FUNCTIONS */

/* HOME PAGE SCROLL ANIMATION */
var delay=10000
var hoffset=-10
var inc=1
var ns4=document.layers
var ie5ns6=document.getElementById||document.all

function updatemsg(){
	crossobj.style.left = "-370px"	
	crossobj.style.display = 'block';
	crossobj.style.left=crossobj.offsetWidth*(-1)+hoffset+"px"
	start=setInterval("animatein()",100)
}

function animatein(){
	if (parseInt(crossobj.style.left) < 0) {		
		crossobj.style.left=parseInt(crossobj.style.left)+10+"px"
	}
}
/* END HOME PAGE SCROLL ANIMATION */

/* BEGIN CLICK TO CALL FUNCTIONS */
function clickToCall(ctcId, ctcKey) {
	var phone = document.getElementById('area').value + document.getElementById('pre').value + document.getElementById('suff').value;
    
    ctcUrl = "http://secure.ifbyphone.com/clickto_status.php?click_id=" + ctcId + "&phone_to_call=" + phone + "&key=332e589fc8488352eb0754c4d6ee4a958a7388fc&click_key=" + ctcKey;
   
	window.open(ctcUrl, 'Clickto' , 'width=200,height=200,toolbar=no,location=no, menubar=no, scrollbars=no, copyhistory=no,resizable=no');
}
/* END CLICK TO CALL FUNCTIONS */

window.onload = function () {
	loadMenu();
    if(doCritters) {
		loadCritters();
	}
	if(doMarquee) {
		crossobj=document.getElementById? document.getElementById("flyin") : document.all.flyin;
		if(crossobj) {
			updatemsg();
		}
	}
}

