// JavaScript Document
//include for form validation
var digits = "0123456789";
var phoneNumberDelimiters = "()-. ";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 10;

function validate(form, a)
{
	//a = required fields array
	//form = form to check
	var err = "";
	var sys = "";
	var top = 0;
	var x = form.elements;
	for(var i = 0; i < x.length; i++)
	{//for each form element
		if(x[i])
		{//if the form element is defined
			if(x[i].disabled||x[i].disabled=="disabled")
			{//enable all form fields for submit
				x[i].disabled=false;
			}
			for(j = 0; j < a.length; j++)
			{//go through each required field id in the array a
				if(x[i].id == a[j])
				{//see if this form element is listed in the array a
					if(!(hasvalue(x[i])))
					{//make sure it has a nonzero value, otherwise, report an error
						err = "Please enter information for the highlighted required fields.\n";
						highlight(x[i], 1);
						if(top == 0)
						{
							x[i].focus();
							top = 1;
						}
					}
					else
					{
						highlight(x[i], 0);
					}
				}
			}
		}
	}
	
	return err;
}

function hasvalue(el)
{//check el for value by form type
	if(el.type == "text")
	{
		if(el.value.length > 0)
		{
			return true;
		}
	}
	if(el.type == "select-one")
	{
		if(el.selectedIndex > 0)
		{
			return true;
		}
	}
	if(el.type =="password")
	{
		if(el.value.length > 0)
		{
			return true;
		}
	}
	if(el.type == "checkbox")
	{
		if(el.checked)
		{
			return true;
		}
	}
	if(el.type == "radio")
	{
		if(el.checked)
		{
			return true;
		}
	}
	
	return false;
}

function getvalue(el)
{//returns string of value held in form element
//if not a recognized type, return ""
	if(el.type == "text")
	{
		if(el.value.length > 0)
		{
			return el.value;
		}
	}
	if(el.type == "select-one")
	{
		if(el.selectedIndex > 0)
		{
			return el.options[el.selectedIndex].value;
		}
	}
	if(el.type =="password")
	{
		if(el.value.length > 0)
		{
			return el.value;
		}
	}
	if(el.type == "checkbox")
	{
		if(el.checked)
		{
			return el.value;
		}
	}
	if(el.type == "radio")
	{
		if(el.checked)
		{
			return el.value;
		}
	}
	
	return "";

}

function highlight(obj, state)
{
	var c = '#FFFFCC';
	if(obj.style)
	{
		if(state)
		{
			obj.style.borderColor=c;
			obj.style.backgroundColor=c;
		}
		else
		{
			obj.style.borderColor='';
			obj.style.backgroundColor='';
		}
	}
}

function toggleControl(aControls, state)
{
	for(var i in aControls)
	{
		if(document.getElementById(aControls[i]))
		{
			if(state == 0) //disable
			{
				document.getElementById(aControls[i]).disabled = true;
			}
			else //enable
			{
				document.getElementById(aControls[i]).disabled = false;				
			}
		}
	}
}

function clearvalue(el)
{//clears value in control.  doesn't return anything.
	if(el.type == "text")
	{
		el.value = "";
	}
	if(el.type == "select-one")
	{
		el.selectedIndex = 0;
	}
	if(el.type =="password")
	{
		el.value = "";
	}
	if(el.type == "checkbox")
	{
		el.checked = false;
	}
	if(el.type == "radio")
	{
		el.checked = false;
	}
	
}

function getel(string)
{
	if(document.getElementById(string))
	{
		return document.getElementById(string);	
	}
	else
	{
		return "";	
	}
}

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 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++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkPhone(strPhone){
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function checkZip(strZip, len)
{
	s=stripCharsInBag(strZip, validWorldPhoneChars).substr(0, len);
	var zipRegExp = /(^\d{5}$)|(^\D{1}\d{1}\D{1}\d{1}\D{1}\d{1}$)/;
	return (zipRegExp.test(s) && s.length == len);
}

function validatePhone(phone){
	
	if ((phone.value==null)||(phone.value=="")){
		alert("Please enter a phone number")
		phone.focus()
		return false
	}
	if (checkPhone(phone.value)==false){
		alert("Please enter a phone number in the form (xxx) xxx-xxxx")
		phone.value=""
		phone.focus()
		return false
	}
	phone.value = stripCharsInBag(phone.value, validWorldPhoneChars).substr(0, minDigitsInIPhoneNumber) ;
	return true
}
function validateZip(z, c)
{//first obj will be zip, second will be country
//only set up now to handle validating simple US and CA zips
	var len;
	if((getvalue(z).length == 0) && (getvalue(c).length == 0))
	{
		return true;
	}
	else
	{
		if(getvalue(c) == "US")
		{
			len = 5;
		}
		else if (getvalue(c) == "CA")
		{
			len = 6;
		}
		else
		{
			return true;
		}
		
		if (getvalue(z).length == 0){
			alert("Please enter a zip code");
			z.focus();
			return false;
		}
		if (checkZip(getvalue(z), len)==false){
			alert("Please enter a valid " + len + " digit zip or select a new country.");
			clearvalue(z);
			z.focus();
			return false;
		}
		z.value = stripCharsInBag(getvalue(z), validWorldPhoneChars).substr(0, len);
		return true;
	}
}

function toggleDiv(id, type)
{
	var target = document.getElementById(type + id);
	var targetimg = document.getElementById(type+'img'+id);

	if (autohide == 1)
	{//hide all divs first
		var maxid = document.getElementById('max'+type);
		var img, others;
		if(maxid && maxid.value)
		{
			for(i=0;i<maxid.value;i++)
			{
				if(id != i)
				{
					others = document.getElementById(type+i);
					img = document.getElementById(type+'img'+i);
					if(others)
					{
						//alert('others' + i);
						others.style.visibility = 'hidden';	
						others.style.display = 'none';
						img.src = '/images/plus.gif';
					}					
				}
			}
		}
	}
	//now toggle the vis of the target div
	
	if(target)
	{
		//alert('target')
		target.style.visibility = (target.style.visibility != 'hidden' ? 'hidden' : 'visible' );	
		target.style.display = (target.style.display != 'none' ? 'none' : 'block' );	
	}
	if(targetimg)
	{
		targetimg.src = (targetimg.src.indexOf('plus') < 0 ? '/images/plus.gif' : '/images/minus.gif' );
	}
};
function refreshCaptchaImage(){
	img = document.getElementById("captchaImage");
	img.src="/captchaimage.asp?rand=" + Math.random();
	txt = document.getElementById("strCaptcha");
	txt.value = '';
	
};
function AllowFormPass(num,formDest) {
	var captchaForm = "captchaForm";
	if (num == 0) {
		enableDisable(true,captchaForm);
		enableDisable(false,formDest);
		setTimeout(function(){showHide(captchaForm+"Div","hide")},3000);
		setTimeout(function(){showHide(formDest+"Div","show")},3000);
	} else {
		refreshCaptchaImage();
	}
};
function showHide(id,type){
	var mydiv = document.getElementById(id);
	if(mydiv){
		if(type=="show"){
			mydiv.style.display="block";
		} else { 
			mydiv.style.display="none";
		}
	}
};
function SetCaptchaPassword(val) {
	var myField = document.getElementById("CaptchaCode");
	myField.value = val;
};
function enableDisable(flag,formname){
	//flag = true if disabled, false if enabled
	var f = eval('document.'+formname);
	var len=f.elements.length; 
	for(x=0;x<len;x++) { 
		f.elements[x].disabled=flag; 
	} 
};
