// JavaScript Document
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };
function verifyFields() {
	for (var i = 0; i < document.tickets.length; i++) {
		var elem = document.tickets[i];
		// Check text fields
		if ((!elem.value || elem.selectedIndex == 0) && (elem.className == 'required' || elem.className == 'reqemail' || elem.className == 'reqdate' || elem.className == 'confirmemail')) {
			alert("All fields marked with * are required.");
			elem.focus();
			return false;
		}
		// Check name of variable - if (elem.name.indexOf('email') >= 0) {
		// Check email field for validity if filled in
		if (elem.className.indexOf('email') >= 0) {
			elem.value = elem.value.trim();
			if (elem.value && !checkEmail(elem.value)) {
				alert("The email address format does not appear valid.");
				elem.focus();
				return false;
			}
		}
		// Check that the email field matches the confirmemail field
		if (elem.className == 'confirmemail') {
			elem.value = elem.value.trim();
			var origemail = document.getElementById('ccEmail').value.trim();
			if (elem.value != origemail) {
				alert("The email address and confirmation email address do not match.");
				elem.focus();
				return false;
			}
		}		
		// Check date field for validity if filled in
		if (elem.className.indexOf('date') >= 0) {
			elem.value = elem.value.trim();
			if (elem.value && !checkDate(elem.value)) {
				//alert("The date entered does not appear valid. It must be in mm/dd/yyyy format");
				elem.focus();
				return false;
			}
		}			
		// Check radio button questions
		if ( (elem.type == "radio" && elem.className == 'required') ||
			 (elem.type == "radio" && elem.className == 'reqmethod' && document.getElementById('paymentinfo').style.display=='') ) { //specific for payment method
			checked = false;
			for (var j = 0; j < document.tickets.length; j++) {
				if (elem.name == document.tickets[j].name && document.tickets[j].checked) {
					checked = true;
				}
			}
			if (!checked) {
				alert("All fields marked with * are required.");
				document.tickets[i].focus();
				return false;
			}
		}
		// Check checkbox questions
		if (elem.type == "checkbox" && elem.className == 'required') {
			var idx = elem.name.lastIndexOf('_');
			checked = false;
			for (var j = 0; j < document.tickets.length; j++) {
				var elem2 = document.tickets[j];
				if (elem.name.substring( 0, idx ) == elem2.name.substring( 0, idx ) && elem2.checked) {
					checked = true;
				}
			}
			if (!checked) {
				alert("All fields marked with * are required.");
				elem.focus();
				return false;
			}
		}
		
		//specific credit card validations
		if (elem.className == 'reqcc' && document.getElementById('paymentinfo').style.display=='') {
			//check if credit card transaction
			var paymethod_value="";
			if(typeof document.tickets.paymethod.length == "undefined")
				paymethod_value=document.tickets.paymethod.value;
			else {
				for (var j = 0; j < document.tickets.paymethod.length; j++) {
					if(document.tickets.paymethod[j].checked) 
						paymethod_value = document.tickets.paymethod[j].value;
				}
			}
			//alert(paymethod_value);
			if(paymethod_value=="cc") {
				if (elem.name=="ccType") {
					var checked = false;
					for (j = 0; j < document.tickets.ccType.length; j++) {
						if (document.tickets.ccType[j].checked) {
							checked = true;
						}
					}
					if (!checked) {
						alert("All credit card fields marked with * are required when paying by credit card.");
						elem.focus();
						return false;
					}
				}
				if (elem.name!="ccType") {			
					if (!elem.value || elem.selectedIndex == 0) {
						alert("All credit card fields marked with * are required when paying by credit card.");
						elem.focus();
						return false;
					}
				}
			}
		}	
	}
	return true;
}
function checkEmail(email) {
	if (window.RegExp) {
		var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
		var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,6}|[0-9]{1,3})(\\]?)$";
		var reg1 = new RegExp(reg1str);
		var reg2 = new RegExp(reg2str);
		if (!reg1.test(email) && reg2.test(email)) {
			return true;
		}
		return false;
	} else {
		if(email.indexOf("@") >= 0)
			return true;
		return false;
	}
}
//date validation related functions
function checkDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
	return true
}
function isInteger(s){
	var i;
	for (i = 0; i < s.length; i++){   
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}	
 function toggle_email(email) {
	if (window.XMLHttpRequest) {
		http = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		http = new ActiveXObject("Microsoft.XMLHTTP");
	}
	handle = document.getElementById(email);
	var url = 'check_Domain.php?';
	if(handle.value.length > 0) {
		var fullurl = url + 'do=check_domain_exists&email=' + encodeURIComponent(handle.value);
		http.open("GET", fullurl, true);
		http.send(null);
		http.onreadystatechange = statechange_username;
	}else{
		document.getElementById('bad_email').innerHTML = '';
	}
}

function statechange_username() {
	if (http.readyState == 4) {
	  if(http.status  == 200) {
		var xmlObj = http.responseXML;	
		var html = xmlObj.getElementsByTagName('result').item(0).firstChild.data;
		if(html!="EMAIL FINE") document.getElementById('bad_email').innerHTML = html;
		else document.getElementById('bad_email').innerHTML = '';
	  } else {
	  	document.getElementById('bad_email').innerHTML = "Error 200";
	  }
	}
}
function stripCharsInBag(s, bag){
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}	
function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}	
//END date validation related functions
