function formCheck(theForm) {
    var reason="";
    reason += isEmpty(theForm.txtFirstName.value, 'First Name');
    reason += isEmpty(theForm.txtLastName.value, 'Last Name');
    reason += isSameValue(theForm.txtFirstName.value, theForm.txtLastName.value, 'First Name','Last Name');
    reason += checkEmail(theForm.txtEmail.value);
    reason += checkPhone(theForm.txtBusPhone.value);
    reason += checkDropdown(theForm.selCountry.selectedIndex, "Country");
    reason += hasLink(theForm.comments.value, "Comments");
    reason += validateConfirm(theForm.chkUS);

    if (reason != "") {
	alert('You entered invalid entries: \n' + reason);
	return false;
    }
}

function formCheckTraining(theForm) {
    var reason="";
    reason += checkEmail(theForm.txtEmail.value);
    reason += checkPhone(theForm.txtBusPhone.value);
    reason += isEmpty(theForm.txtFirstName.value, 'First Name');
    reason += isEmpty(theForm.txtFirstName.value, 'Last Name');
    reason += isEmpty(theForm.txtCompany.value, 'Company');
    reason += isEmpty(theForm.txtJobTitle.value, 'Job Title');

    if (reason != "") {
	alert('You entered invalid entries: \n' + reason);
	return false;
    }
}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function checkEmail (strng) {
    var error="";
    if (strng == "") {
	error = "You didn't enter an email address.\n";
    }

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) {
	error = "Please enter a valid email address.\n";
    } else {
	//test email for illegal characters
	var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
	    if (strng.match(illegalChars)) {
		error = "The email address contains illegal characters. \n";
	    }
    }
    return error;
}

function checkPhone (strng) {
    var error = "";
    if (strng == "") {
	error = "Please enter a phone number.\n";
	return error;
    }
    //strip out acceptable non-numeric characters
    var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); 
    if (isNaN(parseInt(stripped))) {
	error = "The phone number contains illegal characters. \n";

    }
    if (!(stripped.length < 17)) {
	error = "The phone number is the wrong length. Make sure you included an area code.\n";
    }
    return error;
}

function checkCCNum (string) {
    var error ="";
    if (string =="") {
	error = "Please enter a valid Credit Card number. \n";
    }

    if (isNaN(parseInt(string))) {
	error = "Please enter a valid Credit Card number. \n";
    }
    if (!(string.length == 16)) {
	error = "Please enter a valid 16-digit Credit Card number. \n";
    }
    return error;
}

function checkCVC (string, cardtype) {

    var error ="";

    if (string == "") {
	error = "Please enter a valid CVC.  \n";
	return error;
    }
    if (isNaN(string)) {
	error = "Please enter a valid CVC.  \n";
	return error;
    }
    if (cardtype == "AMEX") {
	if (!(string.length) == 4) {
	    error = "Please enter a valid 4 digit CVC.  \n";
	    return error;
	}
    }
    if (cardtype != "AMEX") {
	if(!(string.length) == 3) {
	    error = "Please enter a valid 3 digit CVC. \n";
	    return error;
	}
	return error;
    }
}

function checkZIP (string) {
    var error = "";
    if (string == "") {
	error= "Please enter a valid ZIP code. \n";
    }
    if (isNaN(parseInt(string))) {
	error= "Please enter a valid ZIP code. \n";
    }
    if(!(string.length) == 5) {
	error = "Please enter a valid ZIP code. \n";
    }

    return error;
}

function checkQty (string) {
    var error = "";

    if (isNaN(parseInt(string)) || string == 0) {
	error= "Please enter a valid quantity. \n";
    }

    return error;
}

function isEmpty(strng, name) {
    var error = "";
    if (strng.length == 0) {
	error = "Please Include your " + name + " \n";
    }
    return error;
}

function checkDropdown(choice, name) {
    var error = "";
    if (choice == 0) {
	error = "Please include your " + name + "\n";
    }
    return error;
}

function validateConfirm(check) {
    var error = "";
    if (check.checked == false) {

	// if we get here then the validation has failed

	var error = '\nPlease check the Customer Agreement\n ';
    }

    return error;
}

function checkProducts(products) {
    var error = "";
    var productSelected = false;
    
    for (i = 0; i < products.length; i++) {
	if (products[i].checked == true) {
	    productSelected = true;
	    break;
	}
    }

    if (!productSelected) {
	error = "Please check one of the products\n";
    }


    return error;
}

function setSpanText(spanID,newText) {
    t = document.createTextNode(newText);
    mySpan = document.getElementById(spanID);
    children = mySpan.childNodes;
    if (children.length) {
	mySpan.replaceChild(t, children[0]);
    } else {
	mySpan.appendChild(t);
    }
}

function makeInt(val) {
    // establish variables
    n = val;
    re = /[0-9]+/;
    x = n.match(re);
    // figure out if value is not a valid number... set to zero
    if (isNaN(n) || (n.length == 0) || (x == null)) {
	n = 0;
    } else {
	// find absolute value if not an integer
	n = parseInt(n);
	if (n < 0) {
	    n = Math.abs(n);
	}
    }
    return n;
}


function hasLink(strng, name) {
    var error = "";
    var lower=strng.toLowerCase();

    if (lower.indexOf("http://") != -1) {
	error = "Links are not allowed in " + name + " \n";
    }

    if (lower.indexOf("https://") != -1) {
	error = "Links are not allowed in " + name + " \n";
    }

    if (lower.indexOf("a href=") != -1) {
	error = "Links are not allowed in " + name + " \n";
    }

    return error;
}


function isSameValue(strng1, strng2, name1, name2) {
    var error = "";
    var lower1=strng1.toLowerCase();
    var lower2=strng2.toLowerCase();

    if ((lower1 == lower2) && (lower1 != "") && (lower2 != "")) {
	error = "Values cannot be the same for " + name1 + " and " + name2 + "\n";
    }

    return error;
}
