var submitcount=0;

function highlightTextField(field) {
      field.style.borderColor = 'red';
      field.style.borderStyle = 'solid';
      field.style.borderWidth = '2px;';
}

function removeHighlightTextField(field) {
      field.style.borderColor = '';
      field.style.borderStyle = '';
      field.style.borderWidth = '';
}

function highlightSelectField(field) {
      field.style.backgroundColor = 'red';
      field.style.color = 'black';
}

function removeHighlightSelectField(field) {
      field.style.backgroundColor = '';
      field.style.color = '';
}

function validateForm(form) {
   var ErrorMsg = '';
   var FirstErrorField = '';

   if(form.SiteName.value == '') {
      ErrorMsg = ErrorMsg + '- Site Name\n';
      highlightTextField(form.SiteName);
      if(FirstErrorField == '') {
         FirstErrorField = 'form.SiteName';
      }
   }else removeHighlightTextField(form.SiteName);

   if(!isEmail(form.Email.value)) {
      ErrorMsg = ErrorMsg + '- A Valid Email Address\n';
      highlightTextField(form.Email);
      if(FirstErrorField == '') {
         FirstErrorField = 'form.Email';
      }
   }else removeHighlightTextField(form.Email);

   if(form.RecipURL.value == 'http://' || form.RecipURL.value == '') {
      ErrorMsg = ErrorMsg + '- URL Where Link is Located\n';
      highlightTextField(form.RecipURL);
      if(FirstErrorField == '') {
         FirstErrorField = 'form.RecipURL';
      }
   }else removeHighlightTextField(form.RecipURL);
   
   if(form.SiteURL.value == 'http://' || form.SiteURL.value == '') {
      ErrorMsg = ErrorMsg + '- Your Site URL\n';
      highlightTextField(form.SiteURL);
      if(FirstErrorField == '') {
         FirstErrorField = 'form.SiteURL';
      }
   }else removeHighlightTextField(form.SiteURL);
   
   if(form.random.value == '') {
      ErrorMsg = ErrorMsg + '- Input Security Code\n';
      highlightTextField(form.random);
      if(FirstErrorField == '') {
         FirstErrorField = 'form.random';
      }
   }else removeHighlightTextField(form.random); 
   
   if(form.Description.value == '') {
      ErrorMsg = ErrorMsg + '- Description\n';
      highlightTextField(form.Description);
      if(FirstErrorField == '') {
         FirstErrorField = 'form.Description';
      }
   }else removeHighlightTextField(form.Description);  
   
   if(form.Category.selectedIndex == 0) {
      ErrorMsg = ErrorMsg + '- Category\n';
      highlightSelectField(form.Category);
      if(FirstErrorField == '') {
         FirstErrorField = 'form.Category';
      }
   }else removeHighlightSelectField(form.Category);      
   if(ErrorMsg != '') {
      eval(FirstErrorField + '.focus();');
      alert('Please complete ALL the following fields to continue:\n' + ErrorMsg);
      return false;
   } else {
         return true;
   }
}

// validates email address
function isEmail(email) {
    invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;";

    // Check for null
    if (email == "") {
        return false;
    }

    // Check for invalid characters as defined above
    for (i=0; i<invalidChars.length; i++) {
        badChar = invalidChars.charAt(i);
        if (email.indexOf(badChar,0) > -1) {
            return false;
        }
    }
    lengthOfEmail = email.length;
    if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {
        return false;
    }
    Pos = email.indexOf("@",1);
    if (email.charAt(Pos + 1) == ".") {
        return false;
    }
    while ((Pos < lengthOfEmail) && ( Pos != -1)) {
        Pos = email.indexOf(".",Pos);
        if (email.charAt(Pos + 1) == ".") {
            return false;
        }
        if (Pos != -1) {
            Pos++;
        }
    }

    // There must be at least one @ symbol
    atPos = email.indexOf("@",1);
    if (atPos == -1) {
        return false;
    }

    // But only ONE @ symbol
    if (email.indexOf("@",atPos+1) != -1) {
        return false;
    }

    // Also check for at least one period after the @ symbol
    periodPos = email.indexOf(".",atPos);
    if (periodPos == -1) {
        return false;
    }
    if (periodPos+3 > email.length) {
        return false;
    }
    return true;
}