function validRegField(name, value)
{
    var i;

    if (value.length < 2) return false;

    if (name == 'firstName' || name == 'lastName') {
        if (! value.match(/^([^~@#$%^&*+={}\[\]\\\|;<>?1234567890]*(&#?[0-9]+;)?)*$/)) return false;

        if (value.replace(/[- .]|^[Dd][Rr][. ]/g, '').length == 2 &&
            value != 'aa') return true;
        
        if (! value.match(/[aeiouy&\x80-\xFF]/i)) return false;
    }
    else if (name == 'city' && value.length < 3) return false;

    if (value.match(/\b(foo|foobar)\b|^spam|spam$/i)) return false;

    if (value.match(/^a?[sdfghjkl;]{3}|asd[sdfg]|[sdfg]asd|asdasd/i) &&
        value.substr(0, 1) != value.substr(0, 1).toUpperCase()) return false;

    if (value.match(/^(.)\1+$/)) return false;

    for (i = 1; i < value.length; i++) {
        if (value.charCodeAt(i) != value.charCodeAt(0) + i) break;
    }

    if (i == value.length && value != value.toUpperCase()) return false;

    return true;
}

function checkSignupForm()
{
    var goodemailstr = /^[^@]+@[^@.]+\.[^@]*\w\w$/

    var whois = document.getElementsByName('whois[]');

    if (document.forms[0].email.value == "") {
        alert("Please enter your email address");
        document.forms[0].email.focus();
        return false;
    }
    else if (! goodemailstr.test(document.forms[0].email.value)) {
        alert("Please enter a valid email address");
        document.forms[0].email.focus();
        return false;
    }

    if (document.forms[0].confirmemail.value == "") {
        alert("Please confirm your email address");
        document.forms[0].confirmemail.focus();
        return false;
    }
    else if (document.forms[0].confirmemail.value !=
             document.forms[0].email.value) {
        alert("Email addresses must match");
        document.forms[0].confirmemail.focus();
        return false;
    }

    if (document.forms[0].firstName.value == "") {
        alert("Please enter your first name");
        document.forms[0].firstName.focus();
        return false;
    }

    if (document.forms[0].lastName.value == "") {
        alert("Please enter your last name");
        document.forms[0].lastName.focus();
        return false;
    }

    if (document.forms[0].title.value == "") {
        alert("Please enter your job title");
        document.forms[0].title.focus();
        return false;
    }

    if (document.forms[0].company.value == "") {
        alert("Please enter your company name");
        document.forms[0].company.focus();
        return false;
    }

    if (document.forms[0].city.value == "") {
        alert("Please enter your city");
        document.forms[0].city.focus();
        return false;
    }

    if (document.forms[0].country.value == "") {
        alert("Please select your country");
        document.forms[0].country.focus();
        return false;
    }

    if (document.forms[0].phone.value == "") {
        alert("Please enter your phone number");
        document.forms[0].phone.focus();
        return false;
    }

    if (document.forms[0].job.value == "") {
        alert("Please define your occupation");
        document.forms[0].job.focus();
        return false;
    }

    if (whois[0].checked == false && whois[1].checked == false &&
        whois[2].checked == false) {
        alert("Please tell us if you are an IP supplier or consumer");
        return false;
    }

    if ((document.forms[0].represents[0].checked == false)
        && (document.forms[0].represents[1].checked == false)
        && (document.forms[0].represents[2].checked == false)) {
        alert("Please tell us who you represent");
        document.forms[0].represents[0].focus();
        return false;
    }

    if (document.forms[0].market_segment.value == "") {
        alert("Please tell us the primary end product at your location");
        document.forms[0].market_segment.focus();
        return false;
    }

    if (document.forms[0].revenue.value == "") {
        alert("Please tell us your approximate company revenues");
        document.forms[0].revenue.focus();
        return false;
    }

    if (document.forms[0].referral.value == "" &&
        document.forms[0].ref_other.value == "") {
        alert("Please tell us how you heard about us");
        document.forms[0].referral.focus();
        return false;
    }

    if (document.forms[0].edit && document.forms[0].edit.value) return true;

    if (! validRegField('firstName', document.forms[0].firstName.value)) {
        alert("Please enter your full first name");
        document.forms[0].firstName.focus();
        return false;
    }

    if (! validRegField('lastName', document.forms[0].lastName.value)) {
        alert("Please enter your full last name");
        document.forms[0].lastName.focus();
        return false;
    }

    if (! validRegField('title', document.forms[0].title.value)) {
        alert("Please enter your full job title");
        document.forms[0].title.focus();
        return false;
    }

    if (! validRegField('company', document.forms[0].company.value)) {
        alert("Please enter your full company or institution name");
        document.forms[0].company.focus();
        return false;
    }

    if (! validRegField('city', document.forms[0].city.value)) {
        alert("Please enter your full city name");
        document.forms[0].city.focus();
        return false;
    }
}

function checkAllFields(ref) 
{
    // check all checkboxes in column
    var chkAll = document.getElementById('checkAll');
    var checks = document.getElementsByName('del[]');
    var removeButton = document.getElementById('removeChecked');
    var boxLength = checks.length;
    var allChecked = false;
    var totalChecked = 0;

    if (ref == 1) {
        if (chkAll.checked == true) {
            for (i = 0; i < boxLength; i++) {
                checks[i].checked = true;
            }
        }
        else {
            for (i = 0; i < boxLength; i++) {
                checks[i].checked = false;
            }
        }
    }
    else {
        for (i = 0; i < boxLength; i++) {
            if (checks[i].checked == true) {
                allChecked = true;
                continue;
            }
            else {
               allChecked = false;
               break;
            }
        }
        if (allChecked == true) {
            chkAll.checked = true;
        }
        else {
            chkAll.checked = false;
        }
    }

    for (j = 0; j < boxLength; j++) {
        if (checks[j].checked == true) {
            totalChecked++;
        }
    }

    //removeButton.value = "Remove ["+totalChecked+"] Selected";
}

/**
 * email validation script for support forms
 */

function echeck(str)
{
    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)
    if (str.indexOf(at) == -1){
        alert("Invalid E-mail Address")
        return false
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 ||
        str.indexOf(at) == lstr) {
        alert("Invalid E-mail Address")
        return false
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 ||
        str.indexOf(dot) == lstr) {
        alert("Invalid E-mail Address")
        return false
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        alert("Invalid E-mail Address")
        return false
    }

    if (str.substring(lat - 1, lat) == dot ||
        str.substring(lat + 1, lat + 2) == dot) {
        alert("Invalid E-mail Address")
        return false
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        alert("Invalid E-mail Address")
        return false
    }

    if (str.indexOf(" ") != -1) {
        alert("Invalid E-mail Address")
        return false
    }

    return true
}

function ValidateSupportForm()
{
    var emailID = document.emailForm.email
    var nameID = document.emailForm.name
    var companyID = document.emailForm.company

    if ((emailID.value == null) || (emailID.value == "")) {
        alert("Please Enter your Email Address")
        emailID.focus()
        return false
    }

    if (echeck(emailID.value) == false) {
        emailID.value = ""
        emailID.focus()
        return false
    }

    if (notEmpty(nameID) == false) {
        nameID.focus()
        return false
    }

    if (notEmpty(companyID) == false) {
        companyID.focus()
        return false
    }

    return true
}

function notEmpty(elem)
{
    var str = elem.value;
    if (str.length == 0) {
        alert("You must fill in all fields");
        return false;
    }
    else {
        return true;
    }
}

function ValidateIPForm()
{
    var goodemailstr = /^[^@]+@[^@.]+\.[^@]*\w\w$/

    var shortDesc = document.editipform.shortDescript
    var node = document.editipform.node
    var nodechoice = document.editipform.nodechoice
    var foundry = document.editipform.foundry
    var cat1 = document.editipform.category_1	
    var overview = document.editipform.overview
    var maturity = document.editipform.maturity	
    var gates = document.editipform.gates
    var height = document.editipform.height
    var width = document.editipform.width
    var power = document.editipform.power
    var leakage = document.editipform.leakage
    var constPower = document.editipform.constPower
    var constLeakagePower = document.editipform.constLeakagePower
    var portable = false
    var markets = false
    var email = document.editipform.sales_contact_email
    
    //new code for checking HTML links in the data -- added 102808 by Josh Pederson
    
    var deliv0 = document.editipform.deliverables_0
    var deliv1 = document.editipform.deliverables_1
    var deliv2 = document.editipform.deliverables_2
    var deliv3 = document.editipform.deliverables_3
    var deliv4 = document.editipform.deliverables_4
    
    var feature0 = document.editipform.features_0
    var feature1 = document.editipform.features_1
    var feature2 = document.editipform.features_2
    var feature3 = document.editipform.features_3
    var feature4 = document.editipform.features_4
    var feature5 = document.editipform.features_5
    var feature6 = document.editipform.features_6
    var feature7 = document.editipform.features_7
    var feature8 = document.editipform.features_8
    var feature9 = document.editipform.features_9
    
    var myRegExp = /href=|href =/;
    var matchPos1 = overview.value.search(myRegExp);
    var matchPos2 = shortDesc.value.search(myRegExp);
    
    var matchPos3 = deliv0.value.search(myRegExp);
    var matchPos4 = deliv1.value.search(myRegExp);
    var matchPos5 = deliv2.value.search(myRegExp);
    var matchPos6 = deliv3.value.search(myRegExp);
    var matchPos7 = deliv4.value.search(myRegExp);
    
    var matchPos8 = feature0.value.search(myRegExp);
    var matchPos9 = feature1.value.search(myRegExp);
    var matchPos10 = feature2.value.search(myRegExp);
    var matchPos11 = feature3.value.search(myRegExp);
    var matchPos12 = feature4.value.search(myRegExp);
    var matchPos13 = feature5.value.search(myRegExp);
    var matchPos14 = feature6.value.search(myRegExp);
    var matchPos15 = feature7.value.search(myRegExp);
    var matchPos16 = feature8.value.search(myRegExp);
    var matchPos17 = feature9.value.search(myRegExp);
    
    if (matchPos1 != -1 || matchPos2 != -1 || matchPos3 != -1 || matchPos4 != -1 || matchPos5 != -1 || matchPos6 != -1 || matchPos7 != -1 || matchPos8 != -1 || matchPos9 != -1 || matchPos10 != -1 || matchPos11 != -1 || matchPos12 != -1 || matchPos13 != -1 || matchPos14 != -1 || matchPos15 != -1 || matchPos16 != -1 || matchPos17 != -1) {
	alert("HTML links are not allowed in the IP details --  <a href=''>");
        return false;
    }   
    
    //  End new code 102808



    
    if (notEmptyIP(shortDesc) == false) {
        shortDesc.focus()
        return false	
    }

    if (notEmptyIP(node) == false) {
        node.focus()
        return false	
    }

    if(notEmptyIP(cat1) == false) {
        cat1.focus()
        return false	
    }	

    if (notEmptyIP(overview) == false) {
        overview.focus()
        return false	
    }		

    if (notEmptyIP(maturity) == false) {
        maturity.focus()
        return false	
    }			

    for (i = 0; i < document.editipform.elements.length; i++) {
        if (document.editipform.elements[i].name == 'portability[]' &&
            document.editipform.elements[i].checked == true) portable = true;

        if (document.editipform.elements[i].name == 'markets[]')
            markets = document.editipform.elements[i];
    }

    if (portable == false) {
        alert("You must check at least one box to indicate portability");
        return false;
    }

    if (notEmptyIP(markets) == false) {
        markets.focus()
        return false	
    }			

    if ((node.value == '32nm' || node.value == '45nm' || node.value == '65nm' ||
         node.value == '90nm') &&
        (foundry.value == 'Chartered' || foundry.value == 'IBM' ||
         foundry.value == 'Samsung')) {
        var msg = foundry.value + " " + node.value + 
                  " IP should be set to Common Platform"
        foundry.value = 'Common Platform'
        foundry.focus()
        alert(msg)
        return false
    }

    if (node.value == 'all' && foundry.value != 'all') {
        foundry.value = 'all'
        foundry.focus()
        alert('If node is set to \'all\', foundry must be set to \'all\'')
        return false
    }

    if (! node.value.match(/^To Be Determined$|^[0-9]+ *nm$|^all$|^other$/) ||
        (nodechoice.value.length != 0 &&
         ! nodechoice.value.match(/^[0-9]+ *nm$/))) {
        nodechoice.focus()
        alert('Node must be a number followed by \'nm\'')
        return false
    }

    if (gates.value.length != 0 && gates.value != 0 && 
        (gates.value < 100 || gates.value > 10000000)) {
        gates.focus()
        alert('Gate count out of range');
        return false
    }

    if (height.value.length != 0 && height.value != 0 &&
        height.value < 1) {
        height.focus()
        alert('Height out of range');
	return false
    }

    if (width.value.length != 0 && width.value != 0 &&
        width.value < 1) {
        width.focus()
        alert('Width out of range');
        return false
    }

    if (power.value.length != 0 && power.value != 0 &&
        constPower.value.length != 0 && constPower.value != 0) {
        power.focus()
        alert('Power and Constant Power can not both be set');
        return false
    }

    if (leakage.value.length != 0 && leakage.value != 0 &&
        constLeakagePower.value.length != 0 && constLeakagePower.value != 0) {
        leakage.focus()
        alert('Leakage and Constant Leakage Power can not both be set');
        return false
    }

    if (email.value.length != 0 && ! goodemailstr.test(email.value)) {
	email.focus()
	alert('Please enter a valid email address');
	return false
    }

    return true
 }
 
function notEmptyIP(elem)
{
    var str = elem.value;
	
    if (str.length == 0) {
        alert("You must fill in all required fields");
        return false;
    }
    else {
        return true;
    }
}

function last_choice(selection) 
{
    return selection.selectedIndex == selection.length - 1;
}

function activate(field) 
{
    field.disabled = false;
    if (document.styleSheets) field.style.visibility = 'visible';
    field.focus();
}

function process_choice(selection, textfield) 
{
    if (last_choice(selection)) {
        activate(textfield);
    }
    else {
        textfield.disabled = true;    
        if (document.styleSheets) textfield.style.visibility = 'hidden';
        textfield.value = ''; 
    }
}

function check_choice() 
{
    if (! last_choice(document.editipform.node)) {
        document.editipform.choicetext.blur();
        alert('Please check your menu selection first');
        document.editipform.node.focus(); 
    }
}
