//var othersselected = false;

function validate() {
  var error_message = "Errors have occured during the process of your form.\n\nPlease make the following corrections:\n\n";
  var error_found = false;
  var error_special_found = false;
  var error_special_to = false;
  var error_field;
  var error_highlight;
  var checked = false;
  var otherpayment = '';
	
  //var item_name = document.payments.item_name.value;
  var item_number = document.payments.item_number.value;
  var other = document.payments.other.value;
  var amount = document.payments.amount.value;
  var first_name = document.payments.first_name.value;
  var last_name = document.payments.last_name.value;
  var address1 = document.payments.address1.value;
  var city = document.payments.city.value;
  var index = document.payments.state.selectedIndex;
  var state = document.payments.state[index].value;
  var zip = document.payments.zip.value;
  var email = document.payments.email.value;
  var night_phone_a = document.payments.night_phone_a.value;
  var night_phone_b = document.payments.night_phone_b.value;
  var night_phone_c = document.payments.night_phone_c.value;
  var day_phone_a = document.payments.day_phone_a.value;
  var day_phone_b = document.payments.day_phone_b.value;
  var day_phone_c = document.payments.day_phone_c.value;
  //var to_emails = document.payments.to_emails.value.split(',');
  
  /* for(var i=0; i < to_emails.length ; i++){
	alert(to_emails[i]);
  } */

/* Required Field Check */
  /////////////////////////////////////////////////////////////////////////////////////////
  //// 				PAYMENT INFORMATION 				       ////
  /////////////////////////////////////////////////////////////////////////////////////////
  for(i=0; i < document.payments.item_name.length ; i++){
  	if(document.payments.item_name[i].checked) {
		checked = true;
		whichone = document.payments.item_name[i].value;
		if(whichone != "Invoice" && whichone != "Quote") {
			document.payments.item_name[i].value = document.payments.other.value;
			otherpayment = document.payments.item_name[i].value;
		}
	}
  }

  if ( !checked ){
	error_message = error_message + "* Payment Type is Required\n";
	error_field = document.payments.item_name[0];
	for(var i=0; i < document.payments.item_name.length ; i++) {
		document.payments.item_name[i].style.backgroundColor = "#FF0000";
		document.payments.item_name[i].style.color = "#000000";
	}
	error_found = true;
  } else if ( document.payments.other.disabled == false && otherpayment == '' ) {
	error_message = error_message + "* Payment Type Other is Required\n";
	error_field = document.payments.other;
	document.payments.other.style.backgroundColor = "#FF0000";
	document.payments.other.style.color = "#000000";
	error_found = true;
  } else {
  	for(var i=0; i < document.payments.item_namelength ; i++) {
		document.payments.item_name[i].style.backgroundColor = "#FFFFFF";
		document.payments.item_name[i].style.color = "#000000";
	}
	document.payments.other.style.backgroundColor = "#FFFFFF";
	document.payments.other.style.color = "#000000";
  }

  if ( (item_number == '') || (item_number.length < 2) ) {
    error_message = error_message + "* Item Number is Required\n";
	error_field = document.payments.item_number;
	document.payments.item_number.style.backgroundColor = "#FF0000";
    error_found = true;
  } else {
  	document.payments.item_number.style.backgroundColor = "#FFFFFF";
  }

  if ( (amount == 0) || (amount == '') ) {
    error_message = error_message + "* Amount field is Required\n";
    error_field = document.payments.amount;
	document.payments.amount.style.backgroundColor = "#FF0000";
    error_found = true;
  } else {
  	document.payments.amount.style.backgroundColor = "#FFFFFF";
  }
  /////////////////////////////////////////////////////////////////////////////////////////
  //// 		           BILLING INFORMATION 					       ////
  /////////////////////////////////////////////////////////////////////////////////////////
  if ( (first_name == '') || (first_name.length < 2) ) {
    error_message = error_message + "* First Name is Required\n";
	error_field = document.payments.first_name;
	document.payments.first_name.style.backgroundColor = "#FF0000";
    error_found = true;
  } else {
  	document.payments.first_name.style.backgroundColor = "#FFFFFF";
  }
  
  if ( (last_name == '') || (last_name.length < 2) ) {
    error_message = error_message + "* Last Name is Required\n";
	error_field = document.payments.last_name;
	document.payments.last_name.style.backgroundColor = "#FF0000";
    error_found = true;
  } else {
  	document.payments.last_name.style.backgroundColor = "#FFFFFF";
  }

  if ( (address1 == '') || (address1.length < 2) ) {
    error_message = error_message + "* Address For is Required\n";
	error_field = document.payments.address1;
	document.payments.address1.style.backgroundColor = "#FF0000";
    error_found = true;
  } else {
  	document.payments.address1.style.backgroundColor = "#FFFFFF";
  }
  
  if ( (city == '') || (city.length < 2) ) {
    error_message = error_message + "* City is Required\n";
	error_field = document.payments.city;
	document.payments.city.style.backgroundColor = "#FF0000";
    error_found = true;
  } else {
  	document.payments.city.style.backgroundColor = "#FFFFFF";
  }
  
  if ( (state == '') || (state.length < 2) ) {
    error_message = error_message + "* State is Required\n";
	alert('IN here');
	error_field = document.payments.state[index];
	document.payments.state.style.backgroundColor = "#FF0000";
    error_found = true;
  } else {
  	document.payments.state.style.backgroundColor = "#FFFFFF";
  }
  
  if ( (zip == '') || (zip.length < 2) ) {
    error_message = error_message + "* Zip Code is Required\n";
	error_field = document.payments.zip;
	document.payments.zip.style.backgroundColor = "#FF0000";
    error_found = true;
  } else {
  	document.payments.zip.style.backgroundColor = "#FFFFFF";
  }
  
  if ( (email == '') || (email.length < 2) ) {
    error_message = error_message + "* Email is Required\n";
	error_field = document.payments.email;
	document.payments.email.style.backgroundColor = "#FF0000";
    error_found = true;
  } else {
  	document.payments.email.style.backgroundColor = "#FFFFFF";
  }
  
  if ( ((night_phone_a == '') || (night_phone_a.length != 3)) ||
  	   ((night_phone_b == '') || (night_phone_b.length != 3)) ||
	   ((night_phone_c == '') || (night_phone_c.length != 4))  ) {
    error_message = error_message + "* Night Phone is Required\n";
	error_field = document.payments.night_phone_a;
	document.payments.night_phone_a.style.backgroundColor = "#FF0000";
	document.payments.night_phone_b.style.backgroundColor = "#FF0000";
	document.payments.night_phone_c.style.backgroundColor = "#FF0000";
    error_found = true;
  } else {
  	document.payments.night_phone_a.style.backgroundColor = "#FFFFFF";
	document.payments.night_phone_b.style.backgroundColor = "#FFFFFF";
	document.payments.night_phone_c.style.backgroundColor = "#FFFFFF";
  }
  
  if ( ((day_phone_a == '') || (day_phone_a.length != 3)) ||
  	   ((day_phone_b == '') || (day_phone_b.length != 3)) ||
	   ((day_phone_c == '') || (day_phone_c.length != 4))  ) {
    error_message = error_message + "* Day Phone is Required\n";
	error_field = document.payments.day_phone_a;
	document.payments.day_phone_a.style.backgroundColor = "#FF0000";
	document.payments.day_phone_b.style.backgroundColor = "#FF0000";
	document.payments.day_phone_c.style.backgroundColor = "#FF0000";
    error_found = true;
  } else {
  	document.payments.day_phone_a.style.backgroundColor = "#FFFFFF";
	document.payments.day_phone_b.style.backgroundColor = "#FFFFFF";
	document.payments.day_phone_c.style.backgroundColor = "#FFFFFF";
  }
  
  
  /*if ( (item_number == '') || (item_number.length < 2) ) {
    error_message = error_message + "* Quote or Invoice number\n* ------- or Small Description is Required\n";
	if(error_found == true) {
		error_field = document.payments.item_name[0];
		document.payments.item_name[0].style.backgroundColor = "#FF0000";
	} else {
    	error_field = document.payments.item_number;
		document.payments.item_number.style.backgroundColor = "#FF0000";
	}
    error_found = true;
  } else {
  	document.payments.item_number.style.backgroundColor = "#FFFFFF";
  }*/
  
 /* if ( ((from_email == '') || (from_email.length < 2)) ) {
    error_message = error_message + "* From E-mails field is Required\n";
	if(error_found == true) {
		error_field = document.payments.from_email;
	} else {
    	error_field = document.payments.from_email;
	}
	document.payments.from_email.style.backgroundColor = "#FF0000";
    error_found = true;
  } else {
  	document.payments.from_email.style.backgroundColor = "#FFFFFF";
  }

  if ( (to_emails == '') || (to_emails.length < 1) ) {
    error_message = error_message + "* To E-mail field is Required\n";
    if(error_found == true) {
		error_field = document.payments.to_emails;
	} else {
    	error_field = document.payments.to_emails;
	}
	document.payments.to_emails.style.backgroundColor = "#FF0000";
    error_found = true;
	error_special_to = true;
  } else {
  	document.payments.to_emails.style.backgroundColor = "#FFFFFF";
  }

  if ( (feedback == '') || (feedback.length < 1) ) {
    error_message = error_message + "* FeedBack field is Required\n";
    if(error_found == true) {
		error_field = document.payments.feedback;
	} else {
    	error_field = document.payments.feedback;
	}
	document.payments.feedback.style.backgroundColor = "#FF0000";
    error_found = true;
  } else {
  	document.payments.feedback.style.backgroundColor = "#FFFFFF";
  }*/

  
/* Required Field Check */

/* Validate Email */
  if(error_found){
  	error_special_message = "\nThe following are special validations:\n";
  } else {
 	error_special_message = "Errors have occured during the process of your form.\n\nPlease make the following corrections:\n\nThe following are special validations:\n";
  }

  
  reg1 = /[a-zA-Z]/; // not valid
  reg2 = /^[0-9]+(\.[0-9]{2})?$/; // valid
  if ( (reg1.test(amount)) || (!reg2.test(amount)) ) {
    error_special_message = error_special_message + "* Amount is not valid\n";
    if(error_found) {
		error_field = document.payments.amount;
	} else {
    	error_field = document.payments.amount;
	}
	document.payments.amount.style.backgroundColor = "#FF0000";
    error_special_found = true;
  } else {
  	document.payments.amount.style.backgroundColor = "#FFFFFF";
  }
  
  reg2 = /[0-9]/; // valid
  if ( (reg1.test(zip)) || (!reg2.test(zip)) ) {
    error_special_message = error_special_message + "* Zip Code is not valid\n";
    if(error_found) {
		error_field = document.payments.zip;
	} else {
    	error_field = document.payments.zip;
	}
	document.payments.zip.style.backgroundColor = "#FF0000";
    error_special_found = true;
  } else {
  	document.payments.zip.style.backgroundColor = "#FFFFFF";
  }
  
 



  reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
  if ( (reg1.test(email)) || (!reg2.test(email)) ) {
    error_special_message = error_special_message + "* E-mail is not valid\n";
    if(error_found) {
		error_field = document.payments.email;
	} else {
    	error_field = document.payments.email;
	}
	document.payments.email.style.backgroundColor = "#FF0000";
    error_special_found = true;
  } else {
  	document.payments.email.style.backgroundColor = "#FFFFFF";
  }
  
 /* for(var i=0; i < to_emails.length ; i++){
	  if ( ( reg1.test(to_emails[i]) || !reg2.test(to_emails[i]) || to_emails[i] == '' ) ) {
		if (error_special_to) {
			error_special_message = error_special_message + "* To E-mail is not valid\n";
		} else {
			if( to_emails[i] != '' ) {
				error_special_message = error_special_message + "* To E-mail # " + (i+1) + " is not valid\n---- See email: " + to_emails[i] + "\n";
			} else {
				//if(i != to_emails.length-1){
				error_special_message = error_special_message + "* To E-mail # " + (i+1) + " is blank, please remove extra commas\n";
			}
		}
		//if( to_emails[i] != '' ) {
		if(error_found || error_special_found) {
			error_field = document.payments.to_emails;
		} else {
			error_field = document.payments.to_emails;
		}
		document.payments.to_emails.style.backgroundColor = "#FF0000";
		error_special_found = true;
		//}
		//document.payments.to_emails.style.backgroundColor = "#FF0000";
		//error_special_found = true;
	  } else {
		document.payments.to_emails.style.backgroundColor = "#FFFFFF";
	  }
  }*/
/* Validate Email */

  if (error_found && !error_special_found) {
    alert(error_message);
    error_field.focus();
    return false;
  } else if (!error_found && error_special_found) {
    alert(error_special_message);
    error_field.focus();
    return false;
  } else if (error_found && error_special_found) {
    alert(error_message+error_special_message);
    error_field.focus();
    return false;
  } else {
    return true;
  }


}/* End Of Validate */

function checkenabled() {
	if(document.payments.other.disabled) {
		document.payments.other.disabled=false;
		//othersselected = true;
	} 
}
function disableother() {
 	if(!document.payments.other.disabled) {
		document.payments.other.disabled=true;
		//othersselected = false;
	}
}
