// Print function
function printit(){
    if (window.print) {
        window.print() ;
    } else {
        alert('Please use the print feature of your browser.');
    }
} 

// popups
function popUp(objLink, h, w) {
	window.open(objLink.href, "popup", "width="+w+",height="+h+",scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no");
}


//Empty form fields

function clearText(objField){
    if (objField.defaultValue==objField.value)
    objField.value = ""
}
function resetText(objField){
    if (objField.value=="")
    objField.value = objField.defaultValue
}


/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
*/
function getElementsByClassName(className, tag, elm){
    var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
    var tag = tag || "*";
    var elm = elm || document;
    var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
    var returnElements = [];
    var current;
    var length = elements.length;
    for(var i=0; i<length; i++){
        current = elements[i];
        if(testClass.test(current.className)){
            returnElements.push(current);
        }    
    }
    return returnElements;
}

/*  hide and show content on click   */
function hideshow(objLink)
{
    var objHSBlock = objLink.parentNode.parentNode;
    
    if (objHSBlock.className == "hsblock")
    {
        objHSBlock.className = "hsblock hsopen";
    } else {
        objHSBlock.className = "hsblock";
    }
    
}
/*  hide or show all blocks on click   */
function hideshowAll(strHideShow)
{
    var objHSBlocks = getElementsByClassName("hsblock", "li", document.getElementById("maincontent"));
    var strHSClasses;
    
    if (strHideShow == "show")
    {
        strHSClasses = "hsblock hsopen";
    } else {
        strHSClasses = "hsblock";
    }
    
    for (var i=0;i<objHSBlocks.length;i++) {
        objHSBlocks[i].className = strHSClasses;
    }
    
}

/* standard form validation */
function showContent(theLink)
{
	$(theLink).parent().addClass("selected");
}

/* standard form validation */
function validate(theForm)
    {
    var returnFlag;
    var frmLength;
    var firstBlank;
    var strEmailAddress;
    var strMessage;
        
	var arrReqFields = getElementsByClassName("required", "*", document.getElementById(theForm.id));
    
    strMessage = "Please complete all required fields.";
    firstBlank = -1;
    
    // loop through fields to clear out default values
    frmLength = arrReqFields.length;
    
    for (i=0; i<frmLength;i++){ 
        if(arrReqFields[i].value == "")
        {
            //arrReqFields[i].className = "required warning";
			$(arrReqFields[i]).addClass("warning");
            returnFlag = false;
            if(firstBlank == -1) {
                firstBlank = i;
            }
        }
		else if(arrReqFields[i].className.lastIndexOf("state") > -1) {
			if (arrReqFields[i].value.length < 2) {
				$(arrReqFields[i]).addClass("warning");
                returnFlag = false;
                if(firstBlank == -1) {
                    firstBlank = i;
                    strMessage = "Please enter a valid state";                    
                }
			} else {
				$(arrReqFields[i]).removeClass("warning");
			}
			
		}
		else if(arrReqFields[i].className.lastIndexOf("zipcode") > -1) {
			if (arrReqFields[i].value.length < 5) {
				$(arrReqFields[i]).addClass("warning");
                returnFlag = false;
                if(firstBlank == -1) {
                    firstBlank = i;
                    strMessage = "Please enter a valid zip code";                    
                }
			} else {
				$(arrReqFields[i]).removeClass("warning");
			}
		}
        else if(arrReqFields[i].className.lastIndexOf("email") > -1) {
            strEmailAddress = arrReqFields[i].value;
            intAtPosition = strEmailAddress.indexOf("@");
            intLastDotPosition = strEmailAddress.lastIndexOf(".");
            if((strEmailAddress=="") || (intAtPosition == 0) || (intAtPosition == 1) || (intLastDotPosition == -1) || (intAtPosition > intLastDotPosition) || (intLastDotPosition+1 == strEmailAddress.length)){
                //arrReqFields[i].className = "required warning";
				$(arrReqFields[i]).addClass("warning");
                returnFlag = false;
                if(firstBlank == -1) {
                    firstBlank = i;
                    strMessage = "Please enter a valid email address";
                    
                }
            } else {
				$(arrReqFields[i]).removeClass("warning");
			}
        }
        else {
            //arrReqFields[i].className = "required";
			$(arrReqFields[i]).removeClass("warning");
        }
    }
        
	var arrReqSelect = getElementsByClassName("requiredselect", "*", document.getElementById(theForm.id));
    // loop through fields to clear out default values
    frmLength = arrReqSelect.length;    
    for (i=0; i<frmLength;i++){     
         if(arrReqSelect[i].value == "") {
             if((arrReqSelect[i].options[arrReqSelect[i].selectedIndex].text == "") || arrReqSelect[i].options[arrReqSelect[i].selectedIndex].text.indexOf("--") >= 0) {
                //arrReqSelect[i].className = "requiredselect warning";
				$(arrReqSelect[i]).addClass("warning");
                if(firstBlank == -1) {
                    firstBlank = 0;
                }
                returnFlag = false;
             } else {
                 //arrReqSelect[i].className = "requiredselect";
				 $(arrReqFields[i]).removeClass("warning");
             }
         }    
    }

	//select groups of required radio buttons
	$('.reqgroup').each(function(){
		//make sure at least one is selected
		if( !$(':radio:checked', this).length > 0){
			$(this).addClass("reqgroupwarning");
		  //write a message
			returnFlag = false;
			if(firstBlank == -1) {
				//firstBlank = 0;
				strMessage = "Please complete all required fields.";                    
			}
		} else {
			$(this).removeClass("reqgroupwarning");	
		}
	});
	
    if(returnFlag == false)
        {
            alert(strMessage);
			if(firstBlank != -1) {
            	arrReqFields[firstBlank].focus()
			}
        }
        
    return returnFlag;
    }
	
 