
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 dtCh= "/";
	
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf("/");
	
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strDay=dtStr.substring(0,pos1);
	var strMonth=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	
	var minYear=1900;
	var maxYear=2100;
	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){
		
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
	
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		
		return false;
	}
return true;
}

function echeck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		  
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		  
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		   
		    return false;
		 }

 		 return true;					
	}


//*******************************************************************
function uzXmlHttp(){

var xmlhttp = false;

try{

xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

}catch(e){

try{

xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

}catch(e){

xmlhttp = false;

}

}
//*******************************************************************
if(!xmlhttp && document.createElement){
xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
//*******************************************************************
function result8(){
var name = document.reg.chat.checked;
var result8;
var url = 'includes/active.php?name='+name; 
xmlhttp = uzXmlHttp();
xmlhttp.open("GET", url, false);
xmlhttp.send(null); 
//result = xmlhttp.responseText;
 var resultarea= document.getElementById('result8');
   resultarea.innerHTML = xmlhttp.responseText;
}

function validate(){
		var username 		= document.reg.username.value;		//--0
		var password 		= document.reg.password.value;		//--1
		var re_pass 		= document.reg.re_pass.value;			//--2
		var firstname		= document.reg.firstname.value;		//--3
		var lastname 		= document.reg.lastname.value;		//--4
		var day 			= document.reg.day.value;					//--5
		var month 			= document.reg.month.value;			//--6
		var year 			= document.reg.year.value;					//--7
		var country 		= document.reg.country.value;			//--8
		var lang 			= document.reg.lang.value;					//--9
		var emailID 		= document.reg.email.value;				//--11
		var con 			= document.reg.chat.checked;				//--12
	    var cout			= document.reg.counter.value;

submitOK = "true";
error = " ";

if((username.length) < 4 ){
	error ="* Username Minimum of 4 characters in length\n";
	submitOK = "false";
}
if((password.length) < 4 ){
	error += "* Password Minimum of 4 characters in length\n";
	submitOK = "false";	
	
}else if(password != re_pass){
	error += "* Both password Incorrect\n";
	submitOK = "false";	
}

if((firstname.length) < 2 ){
	error +="* Firstname Minimum of 2 characters in length\n";
	submitOK = "false";
}

if((lastname.length) < 2 ){
	error +="* Lastname Minimum of 2 characters in length\n";
	submitOK = "false";
}

if(day == '0' ){
	error +="* Please select Date of birth\n";
	submitOK = "false";
}
if(month == '0' ){
	error +="* Please select month of birth\n";
	submitOK = "false";
}
if(year == '0' ){
	error +="* Please select year of birth\n";
	submitOK = "false";
}
if(country == '0' ){
	error +="* Please select Nationality\n";
	submitOK = "false";
}
if(lang == '0' ){
	error +="* Please select Language to Prefer\n";
	submitOK = "false";
}
if ((emailID==null)||(emailID=="")){
		
	
		error +="* Please Enter Correct Email Address\n";
		submitOK = "false";
	}
	else if(echeck(emailID)==false){
		emailID.value="";
		
		error +="* Please Enter Correct Email Address\n";
		submitOK = "false";
	}
if(con){
			var chat_name			= document.reg.chat_name.value;
			if((chat_name.length) < 4 )
				{
					error +="* Display name Minimum of 4 characters in length\n";
					submitOK = "false";
				}
		}
if(cout){

	for(var i=1; i<cout; i++){
	
		var addhi					= document.getElementById("addhide"+i).value;
	
if(addhi=='1'){
var addhidenfield			= document.getElementById("additional"+i).value;


if((addhidenfield.length)<2)
				{
					error +="* additional name "+i+" Minimum of 2 characters in length\n";
					submitOK = "false";
				}

  }
 }
}
if(submitOK == "true" ){
	return (true);
}else{	
	alert(error);	
	return (false);
	}
}
