function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function attachEventListener(target,eventType,functionRef,capture){
	if(target==null){return}
	if (typeof(target.attachEvent) != "undefined") {
		//Man, I hate IE
		var functionString=eventType+functionRef;
		target["e"+functionString]=functionRef;
		target[functionString]=function(event){
			if(typeof event=='undefined'){
				event=window.event;
			}
			target["e"+functionString](event);
		};
		target.attachEvent("on"+eventType,target[functionString]);
	} else if (typeof target.addEventListener != "undefined") {
		target.addEventListener(eventType,functionRef,capture);
	}
}

String.prototype.ucFirst = function () {
   return this.substr(0,1).toUpperCase() + this.substr(1,this.length);
}

function cancelEvent(event){
	if (event.cancelable){
		event.preventDefault();// DOM style
	}
	event.returnValue=false;//IE
}

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_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 validateNumeric(formObj, ObjName, len, prompt) {

        if (len == '*') {
                var regex = /^\d+$/;
                if (!regex.test(formObj.value)) {

                    alert(prompt)
                    formObj.focus()
                    return false
                        }
                }
        else {
                numReg = "^\\d{"+parseInt(len)+",}$"
                var regex = new RegExp(numReg);
                if (!regex.test(formObj.value)) {

                    alert(prompt)
                    formObj.focus()
                    return false

                        }
                }
        return true;
}

function validate_phone(formObj)
{

if (formObj.value == "") {

                alert("Please enter a valid Phone number plus Area Code. (xxx-xxx-xxxx)");
                formObj.focus()
                return false;
   }

        phoneReg = "^(?:[\(-][0-9]{3}[\)-]|[0-9]{3})[-. ]?[0-9]{3}[-. ]?[0-9]{4}$";
        var regex = new RegExp(phoneReg);
        if (!regex.test(formObj.value)) {

                alert("Please enter a valid Phone number plus Area Code. (xxx-xxx-xxxx)");
                formObj.focus()
                return false;
                }

        return true;

}

function validate_email(theControl)
{

        if ( theControl.value == "" | theControl.value.length <= 0 )
        {

   alert("I'm sorry. This email address must be filled in correctly to continue. Please"
   +" check the prefix and '@' sign.");
   theControl.focus()
                return false;
        }

        var reEmail = /^.+\@.+\..+$/
        var holderValue;
        var thisValue = theControl.value;

        // Check for e-mail addresses from ISPs and other sources that have been consistently
        // entered incorrectly.  If detected, correct the situation.
        if
                (
                        (thisValue.substring(thisValue.length - 4, thisValue.length).toLowerCase()) == '@aol' ||
                        (thisValue.substring(thisValue.length - 4, thisValue.length).toLowerCase()) == '@msn' ||
                        (thisValue.substring(thisValue.length - 6, thisValue.length).toLowerCase()) == '@yahoo' ||
                        (thisValue.substring(thisValue.length - 6, thisValue.length).toLowerCase()) == '@lycos' ||
                        (thisValue.substring(thisValue.length - 7, thisValue.length).toLowerCase()) == '@excite' ||
                        (thisValue.substring(thisValue.length - 10, thisValue.length).toLowerCase()) == '@altavista' ||
                        (thisValue.substring(thisValue.length - 11, thisValue.length).toLowerCase()) == '@compuserve' ||
                        (thisValue.substring(thisValue.length - 8, thisValue.length).toLowerCase()) == '@prodigy' ||
                        (thisValue.substring(thisValue.length - 8, thisValue.length).toLowerCase()) == '@hotmail' ||
                        (thisValue.substring(thisValue.length - 9, thisValue.length).toLowerCase()) == '@netscape'
                )
                {
                        holderValue = thisValue.concat('.com');
                        thisValue = holderValue;
                        theControl.value = thisValue;
                }
        if
                (
                        (thisValue.substring(thisValue.length - 5, thisValue.length).toLowerCase()) == '@home'
                )
                {
                        holderValue = thisValue.concat('.net');
                        thisValue = holderValue;
                        theControl.value = thisValue;
                }

        // Now check the actual value of the e-mail address for validity.
        var flagFirstCheck = (theControl.value.length < 6) ||
                (thisValue.indexOf('@') == -1) ||
                (thisValue.indexOf('.') == -1) ||
                (thisValue.indexOf('@',(thisValue.indexOf('@')+1)) != -1) ||
                ((thisValue.indexOf('.')+1) == thisValue.length) ||
                ((thisValue.indexOf('@')+1) == thisValue.length)
        var flagSecondCheck = reEmail.test(thisValue)
	        var flagThirdCheck = thisValue.search( /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/ );

        if ( flagFirstCheck || !flagSecondCheck || 0 != flagThirdCheck )
        {

   alert("I'm sorry. This email address seems to be incorrect. Please"
   +" check the prefix and '@' sign.");
   theControl.focus()

                return false;
        }
        else {

                return true;
        }
}

function valbutton(control,prompt, focus) {
// validate radiobuttons
var myOption = -1;
for (i=control.length-1; i > -1; i--) {
if (control[i].checked) {
myOption = i; i = -1;
}
}
if (myOption == -1) {
	alert(prompt);
	return false;
	}
}

function FEmptyControl(control, prompt, focus) {
  if (control.value=="") {
    alert(prompt)
    control.focus()
    return false }
  return true }

function check_survey_form(form) {

	var checked=false;

	if (form.how.selectedIndex < 1) { 
		alert('Select How you found JensenMarineDirect.com');
		form.how.focus();
		return false; }

	if ( (form.why1.checked) || (form.why2.checked) || (form.why3.checked) || (form.why4.checked) || (form.why5.checked) || (form.why6.checked) ) 
	{ checked=true }

	if (!checked ) { 
		alert('Select Why you choose JensenMarineDirect.com (at least one)');
		form.why1.focus();
		return false;
	}

	if (!valbutton(form.wouldpurchaseagain, "Please select \"Consider making another purchase\" option.")) return false;
	// if (!valbutton(form.service_rating, "Please select \"sales and service rating\" option.")) return false;

	return true; 
}