function checkSubmit() { 
  var valid = true;
  emptyChecks = new Array('Name','City','Company','PartNumber','Format','FileType');
  for (fld in emptyChecks) { if (!markEmpty(emptyChecks[fld])) { valid=false; }}
  	if (!markNotPhone('PhoneNumber')) { valid = false; }
  	if (!markNotNumberOrNull('Ext')) { valid = false; }
    if (!markNotState('stateSelect')) { valid = false; }
    if (!markNotEmail('Email', 'EmailConfirm')) { valid = false; }
	if (!valid) { alert('One or more fields contain an invalid value. \n\nPlease modify the "highlighted" fileds and resubmit. '); }
    else { document.getElementById("submit").disabled="disabled"; }
  return valid;
}


/******************
 *  shared functions for error display
 ******************/

function hideerror(fieldelem) {document.getElementById(fieldelem.id).style.background = '#FFFFFF';
	  }
function showerror(fieldelem) {
     	document.getElementById(fieldelem.id).style.background = '#E0F793';
	}

function evalerror(valid,id) {
   	if (valid) { hideerror(document.getElementById(id)) } 
  	else { showerror(document.getElementById(id)) }
  	return valid;
}

function evalerrorcomp(valid,id1,id2) {
   	if (valid) { hideerror(document.getElementById(id1));
   				 hideerror(document.getElementById(id2));
   	}
   	else { showerror(document.getElementById(id1));
  		   showerror(document.getElementById(id2));
   	}
  	return valid;
}
/******************
 *  functions to return true or false if a field is valid
 ********/
function validateNotEmpty(id) {
  thisval = document.getElementById(id).value;
  return (thisval && thisval != "" && thisval.length > 0);
}

function validateLength(id, len) {
  return (document.getElementById(id).value.length == len);
}

function validateMinLength(id, len) {
  return (document.getElementById(id).value.length >= len);
}

function validatePasswords(id1, id2) {
  return ((document.getElementById(id1).value == document.getElementById(id2).value)
      && document.getElementById(id1).value.length > 0);
}

function validateDate(id) {
  var regEx = /^\d{2}-\d{2}-\d{4}$/;

  return (document.getElementById(id).value.search(regEx) != -1);
}

function validateEmail(id1,id2) {
  var regEx = /^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$/;

  return ((document.getElementById(id1).value == document.getElementById(id2).value) 
  		&& document.getElementById(id1).value.search(regEx) != -1);
}

function validateZip (id) {
  var regEx = /^[0-9]*$/;
  
  return (document.getElementById(id).value.search(regEx) != -1 && document.getElementById(id).value.length == 5)
}

function validateName (id) {
  var regEx = /^[a-zA-Z]*$/;
  
  return (document.getElementById(id).value.search(regEx) != -1 && document.getElementById(id).value.length > 0);
}

function validateAddress (id) {
  var regEx = /^[0-9][a-zA-Z0-9. ]*$/;

  return (document.getElementById(id).value.search(regEx) != -1 && document.getElementById(id).value.length > 0)
}

function validateAddress2 (id) {
  var regEx = /^[a-zA-Z0-9.# ]*$/;

  return (document.getElementById(id).value.search(regEx) != -1 && document.getElementById(id).value.length >= 0)
}

function validateCity (id) {
  var regEx = /^[a-zA-Z][a-zA-Z ]*$/;
  

  return (document.getElementById(id).value.search(regEx) != -1 && document.getElementById(id).value.length > 0)
}

function validateState (id) {
 var regEx = /^[a-zA-Z][a-zA-Z ]*$/;
 return (document.getElementById(id).value.search(regEx) != -1 && document.getElementById(id).value.length > 0)
  //if (id.match(/stateSelect$/) && document.getElementById(id.match(/(.*)stateSelect$/)+"country")=="US") {
   // return (validateNotEmpty(id));
 // }
 // else return (true);

}

function validateCountry (id) {
  var regEx = /^[a-zA-Z][a-zA-Z ]*$/;

  return (document.getElementById(id).value.search(regEx) != -1 && document.getElementById(id).value.length > 0)
}

function validateInteger(id) {
  return (parseInt(document.getElementById(id).value) == document.getElementById(id).value-0);
}

function validateIntegerOrNull(id) {
	thisval = document.getElementById(id).value
	
  return (parseInt(document.getElementById(id).value) == document.getElementById(id).value-0) || (thisval.length == 0);
}
function validatePhone(id) {
  var regEx = /^[0-9]{3}-[0-9]{3}-[0-9]{4}$/;
  var regEx2 = /^[0-9]{3}.[0-9]{3}.[0-9]{4}$/;
  var regEx3 = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/;
  
  return	(document.getElementById(id).value.search(regEx) != -1) ||
   			(document.getElementById(id).value.search(regEx2) != -1) ||
   			(document.getElementById(id).value.search(regEx3) != -1);
}

function validateCreditCard (id, type) {
  var ccType = document.getElementById(type);
  
  if (ccType.options[ccType.selectedIndex].value == "Amex") {
  var regEx = "^3[47][0-9]{13}$";

  return (document.getElementById(id).value.search(regEx) != -1 && mod10(document.getElementById(id).value));
  } else if (ccType.options[ccType.selectedIndex].value == "Discover") {
    var regEx = "^6011[0-9]{12}$";

    return (document.getElementById(id).value.search(regEx) != -1 && mod10(document.getElementById(id).value));
    
  } else if (ccType.options[ccType.selectedIndex].value == "MasterCard") {
     var regEx = "^5[1-5][0-9]{14}$";
     console.debug(document.getElementById(id).value);
     console.debug( mod10(document.getElementById(id).value));
     console.debug(document.getElementById(id).value.search(regEx) != -1);
     return (document.getElementById(id).value.search(regEx) != -1 && mod10(document.getElementById(id).value));
    
  } else if (ccType.options[ccType.selectedIndex].value == "Visa") {
    var regEx = "^4[0-9]{15}$";

    return (document.getElementById(id).value.search(regEx) != -1 && mod10(document.getElementById(id).value));
  } else {

    return true;
  }
}

function validateCCV (id, type) { 
  var ccType = document.getElementById(type);
  if (document.getElementById(id).value != ""){
  if (ccType.options[ccType.selectedIndex].value == "Amex") {
    var regEx = "^[0-9]{4}$";
    return (document.getElementById(id).value.search(regEx) != -1);
  } else {
    var regEx = "^[0-9]{3}$";
   return (document.getElementById(id).value.search(regEx) != -1);
  }}
  else {return true; }
}

function validateExp (month, year) {
  var cardYear = document.getElementById(year);
  var cardMonth = document.getElementById(month);

  var expYear = cardYear.options[cardYear.selectedIndex].value;
  var expMonth = (cardMonth.options[cardMonth.selectedIndex].value - 1);

  var cardExp = new Date();
  cardExp.setFullYear(expYear,expMonth,1);

  var today = new Date();
  today.setDate(1);
  
  if (cardExp>=today) {
	return true;
  }	
  else {
	return false;
  }   
}

function validateRadioGroup(radioGroup) {
  for (j = 0; j < radioGroup.length; j++) {
    if (radioGroup[j].checked) { return true; }
  }
  
  return false;
}
/*
*Special functions for validation calculation
*/

function mod10( cardNumber ) { // LUHN Formula for validation of credit card numbers.
	var ar = new Array( cardNumber.length );
	var i = 0,sum = 0;


    	for( i = 0; i < cardNumber.length; ++i ) {
    		ar[i] = parseInt(cardNumber.charAt(i));
    	}
    	for( i = ar.length -2; i >= 0; i-=2 ) { // you have to start from the right, and work back.
    		ar[i] *= 2;							 // every second digit starting with the right most (check digit)
    		if( ar[i] > 9 ) { ar[i]-=9; }			 // will be doubled, and summed with the skipped digits.
    	}										 // if the double digit is > 9, ADD those individual digits together 


        	for( i = 0; i < ar.length; ++i ) {
        		sum += ar[i];						 // if the sum is divisible by 10 mod10 succeeds
        	}
        	return (((sum%10)==0)?true:false);	 	
}




/******************
 *  functions to mark marks by required fields
 *  marks must be contained in an element with id: id + '_asterisk'
 ********/
function markEmpty(id) {
  var valid = validateNotEmpty(id);
  return (evalerror(valid,id));
}

function markNotNumber(id) {
  var valid = validateInteger(id);
  return (evalerror(valid,id));
}

function markNotNumberOrNull(id) {
  var valid = validateIntegerOrNull(id);
  return (evalerror(valid,id));
}

function markNotPhone(id) {
  var valid = validatePhone(id);
  return (evalerror(valid,id));
}

function markNotEmail(id1,id2) {
  var valid = validateEmail(id1,id2);
  return (evalerrorcomp(valid,id1,id2));
}

function markExp(month, year) {
  var valid = validateExp(month, year);
  return (evalerror(valid,year));
}

function markNotName(id) {
  var valid = validateName(id)
  return (evalerror(valid,id1,id2));
}

function markNotCC(id, type) {
  var valid = validateCreditCard(id, type);
  return (evalerror(valid,id));
}

function markNotCCV(id, type) {
  var valid = validateCCV(id, type);
  return (evalerror(valid,id));
}

function markNotZip(id) {
  var valid = validateZip(id);
  return (evalerror(valid,id));
}

function markNotAddress(id) {
  var valid = validateAddress(id);
  return (evalerror(valid,id));
}

function markNotAddress2(id) {
  var valid = validateAddress2(id);
  return (evalerror(valid,id));
}

function markNotCity(id) {
  var valid = validateCity(id);
  return (evalerror(valid,id));
}

function markNotState(id) {
  var valid = validateState(id);
  return (evalerror(valid,id));
}

function markNotCountry(id) {
  var valid = validateCountry(id);
  return (evalerror(valid,id));
}

function markInvalidPasswords(id1, id2) {
  var valid = validatePasswords(id1, id2);
  if (valid) { hideerror(document.getElementById(id1)); hideerror(document.getElementById(id2)); } 
  else { showerror(document.getElementById(id1)); showerror(document.getElementById(id2)); }
  return valid;
}

function markNotEmptyNotNumber(id) {
  //mark the field if it's not empty AND it is not an integer
  var valid = !validateNotEmpty(id) || validateInteger(id);
  return (evalerror(valid,id));
}

function markNotEmptyNotDate(id) {
  var valid = !validateNotEmpty(id) || validateDate(id);
  return (evalerror(valid,id));
}

function markUnansweredQuestion(radioGroup,questionId) {
  var valid = validateRadioGroup(radioGroup);
  return (evalerror(valid,id));
}

function markEmptyTextGroup(prefix,labelId,numEntries) {
  for (i = 0; i < numEntries; i++) {
    if (!validateNotEmpty(prefix + i)) { showerror(document.getElementById(labelId)); return false; }
  }
  hideerror(document.getElementById(labelId));
  return true;
}

function markNotEmptyInvalidPasswords(id1, id2) {
  var valid = validatePasswords(id1, id2) ||
    (!validateNotEmpty(id1) && !validateNotEmpty(id2));
  if (valid) { hideerror(document.getElementById(id1)); hideerror(document.getElementById(id2)); } 
  else { showerror(document.getElementById(id1)); showerror(document.getElementById(id2)); }
  return valid;
}
