// JavaScript Document
/**
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

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 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
}

function isDate(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 ((month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid date.")
		return false
	}
return true
}
// Email Validation Javascript

function echeck(addr,man,db) {
if (addr == '' && man) {
   if (db) alert('email address is mandatory');
   return false;
}
if (addr == '') return true;
var invalidChars = '\/\'\\ ";:?!()[]\{\}^|';
for (i=0; i<invalidChars.length; i++) {
   if (addr.indexOf(invalidChars.charAt(i),0) > -1) {
      if (db) alert('email address contains invalid characters');
      return false;
   }
}
for (i=0; i<addr.length; i++) {
   if (addr.charCodeAt(i)>127) {
      if (db) alert("email address contains non ascii characters.");
      return false;
   }
}

var atPos = addr.indexOf('@',0);
if (atPos == -1) {
   if (db) alert('email address must contain an @');
   return false;
}
if (atPos == 0) {
   if (db) alert('email address must not start with @');
   return false;
}
if (addr.indexOf('@', atPos + 1) > - 1) {
   if (db) alert('email address must contain only one @');
   return false;
}
if (addr.indexOf('.', atPos) == -1) {
   if (db) alert('email address must contain a dot in the domain name');
   return false;
}
if (addr.indexOf('@.',0) != -1) {
   if (db) alert('dot must not immediately follow @ in email address');
   return false;
}
if (addr.indexOf('.@',0) != -1){
   if (db) alert('dot must not immediately precede @ in email address');
   return false;
}
if (addr.indexOf('..',0) != -1) {
   if (db) alert('two dots must not be adjacent in email address');
   return false;
}
var suffix = addr.substring(addr.lastIndexOf('.')+1);
if (suffix.length != 2 && suffix != 'com' && suffix != 'net' && suffix != 'org' && suffix != 'edu' && suffix != 'int' && suffix != 'mil' && suffix != 'gov' & suffix != 'arpa' && suffix != 'biz' && suffix != 'aero' && suffix != 'name' && suffix != 'coop' && suffix != 'info' && suffix != 'pro' && suffix != 'museum') {
   if (db) alert('invalid primary domain in email address');
   return false;
}
return true;
}


// This is the main routine
function ValidateForm(){
	var day=document.form1.Day.selectedIndex
	var month=document.form1.Month.selectedIndex
	var year=document.form1.Year.selectedIndex
		var bridename=document.form1.Bridename.value
		var groomname=document.form1.Groomname.value
		var email=document.form1.Email.value
	var email2=document.form1.Email2.value
	var line1=document.form1.Line1.value.length
	var line2=document.form1.Line2.value.length
	var line3=document.form1.Line3.value.length
	var line4=document.form1.Line4.value.length

	if ((day==0)||(month==0)||(year==0)){
	alert("Please enter a date")
	return false
    }

	
	
	year=2009-year
	
	dt=month+dtCh+day+dtCh+year
	
	
	if (isDate(dt)==false){
		return false
	}
	
			if ((bridename==null)||(bridename=="")){
		alert("Please enter bride's name")
		return false
	}
		if ((groomname==null)||(groomname=="")){
		alert("Please enter groom's name")
		return false
	}
	
		if ((day==0)||(month==0)||(year==0)){
		alert("Please enter a date")
		return false
    }


		if ((email==null)||(email=="")){
		alert("Please enter your Email address")
		return false
	}

		if (echeck(email,1,1)==false){
		return false
	}
		if (echeck(email2,0,1)==false){
		return false
	}

		if (line1>29){
		alert("Line 1 is too long - needs to be less than 30 characters, including spaces")
		return false
	}
		if (line2>29){
		alert("Line 2 is too long - needs to be less than 30 characters, including spaces")
		return false
	}
		if (line3>29){
		alert("Line 3 is too long - needs to be less than 30 characters, including spaces")
		return false
	}
		if (line4>29){
		alert("Line 4 is too long - needs to be less than 30 characters, including spaces")
		return false
	}

	
    return true
 }

