// JavaScript Document

/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// 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 (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 ValidateForm(){
	var dt=document.form1.dob
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }
 
 // Validation of email
 
 function disp_alert()
{
if (document.form1.title.value=="")
{
	alert("Please Select Your Title");
	document.form1.title.focus();
	return false;
}
if (document.form1.fname.value=="")
{
	alert("Please Enter Your First Name");
	document.form1.fname.focus();
	return false;
}
if(document.form1.fname.value!="")
{
	var str_value=document.form1.fname.value;
	var str_len=document.form1.fname.value.length;
	for (var i=0;i<str_len;i++)
	{
		var a=str_value.charAt(i);
		if(a==0||a==1||a==2||a==3||a==4||a==5||a==6||a==7||a==8||a==9)
		{
			alert("Please Enter Characters Only");
			document.form1.fname.focus();
			return false;
			break;
		}
	}
}
if(document.form1.mname.value!="")
{
	var str_value=document.form1.mname.value;
	var str_len=document.form1.mname.value.length;
	for (var i=0;i<str_len;i++)
	{
		var a=str_value.charAt(i);
		if(a==0||a==1||a==2||a==3||a==4||a==5||a==6||a==7||a==8||a==9)
		{
			alert("Please Enter Characters Only");
			document.form1.mname.focus();
			return false;
			break;
		}
	}
}
if(document.form1.lname.value=="")
{
	alert("Please Enter Your Last Name");
	document.form1.lname.focus();
	return false;
}

if(document.form1.lname.value!="")
{
	var str_value=document.form1.lname.value;
	var str_len=document.form1.lname.value.length;
	for (var i=0;i<str_len;i++)
	{
		var a=str_value.charAt(i);
		if(a==0||a==1||a==2||a==3||a==4||a==5||a==6||a==7||a==8||a==9)
		{
			alert("Please Enter Characters Only");
			document.form1.lname.focus();
			return false;
			break;
		}
	}
}
if(document.form1.add1.value=="")
{
	alert("Please Enter Your Address Line 1");
	document.form1.add1.focus();
	return false;
}
if(document.form1.city.value=="")
{
	alert("Please Enter Your City");
	document.form1.city.focus();
	return false;
}
if(document.form1.postcode.value=="")
{
	alert("Please Enter Your Post code / Zip Code");
	document.form1.postcode.focus();
	return false;
}
if(document.form1.country.value=="")
{
	alert("Please Enter Your Country");
	document.form1.country.focus();
	return false;
}
if (document.form1.email.value=="")
{
alert("Please Enter Your Email");
document.form1.email.focus();
return false;
}
else if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(form1.email.value))
{
}
else
{
alert("Invalid E-mail Address! Please re-enter.");
document.form1.email.focus();
return (false)
}

		if(document.form1.dob.value=="")
{
	alert("Please Enter Your Date Of Birth");
	document.form1.dob.focus();
	return false;	
}
else
{
	var ret=ValidateForm();
	
	if(!ret)
	{
		return false;
	}
}
if(document.form1.telno.value=="")
		{
			alert("Please Enter Your Phone Number")
			document.form1.telno.focus();
			return false;
		}
		if(document.form1.telno.value!="")
		{
			var str_value=document.form1.telno.value;
			var str_len=document.form1.telno.value.length;
			var str_comp="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
			for (var i=0;i<str_len;i++)
			{
				var a=str_value.charAt(i);
				if(str_comp.indexOf(a)!=-1)
				{
					alert("Please Enter a vaild Telephone number");
					document.form1.telno.focus();
					return false;
					break;
				}
			}
		}
		if((document.form1.telno.value!="")&&(document.form1.telno.value.length<=9))
		{
			alert("Please Enter a vaild Telephone number");
			document.form1.telno.focus();
			return false;
		}
if(document.form1.domainlike.value=="")
{
	alert("Please Enter Your Domain Choice");
	document.form1.domainlike.focus();
	return false	
}

return true;
}


function CheckSubmit()
{
	var check = disp_alert();
	if(check)	
	{
		document.form1.submit();	
		return true;
	}
	else
	{
		return false;
	}
}
