// Radio Button Validation
function ValidateEmail( Email )
{
		var atCharPresent = false;
		var dotPresent = false;

		for ( var Idx = 0; Idx < Email.length; Idx++ )
		{
				if ( Email.charAt ( Idx ) == '@' )
						atCharPresent = true;
				if ( Email.charAt ( Idx ) == '.' )
						dotPresent = true;
		}

		if ( !atCharPresent || !dotPresent )
				return false;

		return true;
}
function radioVal(btn) {
	var cnt = -1;
	for (var i=btn.length-1; i > -1; i--) {
		if (btn[i].checked) {cnt = i; i = -1;}
	}
	if (cnt > -1) return btn[cnt].value;
	else return null;
}

function checkArray(form, arrayName) 
{ 
	var retval = new Array();
	for(var i=0; i < form.elements.length; i++) 
	{ 
		var el = form.elements[i]; 
		if(el.type == "checkbox" && el.name == arrayName && el.checked) 
		{ 
			retval.push(el.value); 
		} 
	} 
	return retval; 
}
function IsPhone(sText,Label)
{
   var ValidChars = "0123456789-/+()or ";
   var IsNumber=true;
   var Char;
   if(sText=="") {alert("Please enter your "+Label);return false}
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if(ValidChars.indexOf(Char) == -1) 
      {
         IsNumber = false;
      }
   }
   if(!IsNumber)
   	alert("Your Phone number must contain only numeric characters.");
   return IsNumber;
}
function verifyPhNO(val){
	var url="mobile-verify.php";
	var params="mob="+val+"&step=1";
	var mobileVeriSucc=function(res){
		alert(res);
	}
	getAjaxRes(url,params,mobileVeriSucc);	
}
function trim(value) 
{
	var temp = value;
	var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
	if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
	var obj = / +/g;
	temp = temp.replace(obj, " ");
	if (temp == " ") { temp = ""; }
		return temp;
}

//special character validation
function specialchar_check(v)
{
        var retVal=1;
        var iChars = "!@#$%^&*()+=-_[]\\\';,./{}|\":<>?";
          for (var i = 0; i < v.length; i++) {
                if (iChars.indexOf(v.charAt(i)) != -1)
                retVal=0;
        }
        return retVal;
}

function isAlpha(parm) {
        var lwr = 'abcdefghijklmnopqrstuvwxyz ';
        var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        var val = lwr+upr;
        if (parm == "") return true;
        for (i=0; i<parm.length; i++) {
                if (val.indexOf(parm.charAt(i),0) == -1) return false;
        }
        return true;
}

//url validation
function isUrl(url) {	
	var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
	return RegExp.test(url);
}	

function Spacecheck(val)
{
	var retVal=1;
	var iChars = " ";
	for (var i = 0; i < val.length; i++) {
		if (iChars.indexOf(val.charAt(i)) != -1)
			retVal=0;
	}
	return retVal;
}

function adsvalidate(){
	var frm = document.PostAdForm;
	var subcat_id = document.PostAdForm.SubCategory.options[document.PostAdForm.SubCategory.selectedIndex].value;
	var cat_id = document.PostAdForm.Category.options[document.PostAdForm.Category.selectedIndex].value;
	var city_id = document.PostAdForm.City.options[document.PostAdForm.City.selectedIndex].value;

        if (trim(document.PostAdForm.name.value) == ""){
                alert ("Please enter the name");
                document.PostAdForm.name.focus( );
                return false;
        }
        var leng=document.PostAdForm.name.value.length;
        if(leng<2){
                alert("Your name must be atleast be 2 characters long..");
                document.PostAdForm.name.focus();
                return false;
        }

	if(specialchar_check(frm.name.value) == 0)
	{
		alert("Please enter valid name. Special characters are not allowed");
                document.PostAdForm.name.focus();
                return false;
	}

        if(isAlpha(frm.name.value) == false){
		alert("Please enter valid name.");
                document.PostAdForm.name.focus();
                return false;
        }


	if (trim(document.PostAdForm.email.value) == ""){
		alert ("Please enter your e-mail address");
		document.PostAdForm.email.focus( );
		return false;
	}
	if ( !ValidateEmail( document.PostAdForm.email.value ) ){
		alert( "Invalid E-mail " + document.PostAdForm.email.value );
		document.PostAdForm.email.focus( );
		return false;
	}	
	if (trim(document.PostAdForm.password.value) == ""){
		alert ("Please enter password");
		document.PostAdForm.password.value="";
		document.PostAdForm.password.focus( );
		return false;
	}
	var pass = trim(document.PostAdForm.password.value);
	var len=document.PostAdForm.password.value.length;
	if(pass.length<4){
		alert("Your password must be atleast be 4 characters long.");
		document.PostAdForm.password.focus();
		return false;
	}
	if(Spacecheck(pass) == 0)
	{
		alert("Your password must not contain spaces");
		return false;
	}
	if (trim(document.PostAdForm.confirm_password.value) == ""){
		alert ("Please enter confirm password");
		document.PostAdForm.confirm_password.focus( );
		return false;
	}
	if(document.PostAdForm.confirm_password.value!=document.PostAdForm.password.value){
		alert("Password and Confirm password does not match")
		document.PostAdForm.confirm_password.focus( );
		return false;
	}
	var web = trim(document.PostAdForm.Website.value);
	if(web != '' && !isUrl(web))
	{
		alert("Please enter valid Website address");
		document.PostAdForm.Website.focus( );
		return false;
	}
	var ph = trim(document.PostAdForm.Telephone.value);
	var mob = trim(document.PostAdForm.Mobile.value);
	if (ph == "" && mob == ""){
		alert ("Please enter the Telephone/Mobile Number");
		document.PostAdForm.Telephone.focus( );
		return false;
	}
	else
	{
		if(ph != '' && ph.length < 5)
		{
			alert("Your Phone number must contain minimum of 5 numbers.");
			return false;
		}
		if(ph != '' && !IsPhone(ph,"phone number."))
		{
			document.PostAdForm.Telephone.focus( );
			return false;
		}

		
		if(isNaN(mob)||mob.indexOf(" ")!=-1)
		{
			alert("Your Mobile number must contain only numeric values");
			document.PostAdForm.Mobile.focus( );
			return false;
		}
		if (mob.length>10 || mob.length <10)
		{
			alert("Your Mobile number must contain 10 characters");
			document.PostAdForm.Mobile.focus( );
			return false;
		}
		var mstart = mob.charAt(0);
		if(mstart!=9 && mstart!=8 && mstart!=7)
		{
			alert("Please enter a valid mobile number");
			document.PostAdForm.Mobile.focus( );
			return false
		
		}	
	}

	if (document.PostAdForm.Category.selectedIndex == 0 ){
		alert( "Please select Category");
		document.PostAdForm.Category.focus( );
		return false;
	}
	if (document.PostAdForm.SubCategory.selectedIndex == 0 ){
		alert( "Please select Sub Category");
		document.PostAdForm.SubCategory.focus( );
		return false;
	}	

	if((cat_id==23) && (subcat_id == 1 || subcat_id == 2 || subcat_id == 6 || subcat_id == 7)) {
		if( document.PostAdForm.rstype.selectedIndex == 0 ) {
			alert( "Please select type");
			document.PostAdForm.rstype.focus( );
			return false;
		}
		if(subcat_id == 1 || subcat_id == 2){
			var rtype=document.PostAdForm.rstype.options[document.PostAdForm.rstype.selectedIndex].value;
			if(rtype == 3 || rtype == 4 || rtype == 5 || rtype == 7) {
				if(document.PostAdForm.bedroom.selectedIndex == 0) {
					alert( "Please select bedroom");
					document.PostAdForm.bedroom.focus( );
					return false;
				}
			}
		}
	}
	if((cat_id==23) && (subcat_id == 6 || subcat_id == 7)) {

		var itemsChecked = checkArray(document.PostAdForm, "accepted[]");
			if(itemsChecked.length == 0) {
				alert('Please select accepted');
				document.PostAdForm.Accepted[0].focus();
				return false;
			}
		var btn2 = radioVal(document.PostAdForm.parking);
			if (btn2 == null) {
				alert('Please select Parking');
				document.PostAdForm.parking[0].focus( );
				return false;
			}
	}
	if((cat_id==23) && (subcat_id == 5 || subcat_id == 6 || subcat_id == 7)) {
		var btn = radioVal(document.PostAdForm.rsadtype);
			if (btn == null) {
				alert('Please select adtype');
				document.PostAdForm.rsadtype[0].focus( );
				return false;
			}
	}
	if((cat_id==1) || (cat_id==2) || (cat_id==5) || (cat_id==8) || (cat_id==9) || (cat_id==11) || (cat_id==12) || (cat_id==16) || (cat_id==22) || (cat_id==24) || (cat_id==25) || (cat_id==27) && (subcat_id == 1 || subcat_id == 2 || subcat_id == 3 || subcat_id == 4 || subcat_id == 5 || subcat_id == 6 || subcat_id == 7 || subcat_id == 8 || subcat_id == 9)) {
		var btn = radioVal(document.PostAdForm.ad_type);
			if (btn == null) {
				alert('Please select adtype');
				document.PostAdForm.ad_type[0].focus( );
				return false;
			}
	}
	if(cat_id==3) {
		if(subcat_id == 2) {
			if( document.PostAdForm.vmake.selectedIndex == 0 ) {
				alert( "Please select make");
				document.PostAdForm.vmake.focus( );
				return false;
			}
			if( document.PostAdForm.model.selectedIndex == 0 ) {
				alert( "Please select model");
				document.PostAdForm.model.focus( );
				return false;
			}
		}
		if(subcat_id == 1 || subcat_id == 2) {
			var btn = radioVal(document.PostAdForm.adtype);
				if (btn == null) {
					alert('Please select adtype');
					document.PostAdForm.adtype[0].focus( );
					return false;
			}
		}
	}
	if((cat_id==18) && (subcat_id == 1 || subcat_id == 2)) {
		if( document.PostAdForm.jobtype.selectedIndex == 0 ) {
			alert( "Please select job type");
			document.PostAdForm.jobtype.focus( );
			return false;
		}

		if( document.PostAdForm.jobs_field.selectedIndex == 0 ) {
			alert( "Please select job field");
			document.PostAdForm.jobs_field.focus( );
			return false;
		}
		var fcnt=0;
		for(var j=0; j<document.PostAdForm.jobs_field.options.length; j++)
		{
			if(document.PostAdForm.jobs_field.options[j].selected){
				fcnt++;
			}
		}
		if( fcnt > 3 )
		{
			alert( "You can select maximum of 3 options");
			return false;
		}
		if( document.PostAdForm.jobs_role.selectedIndex == 0 ) {
			alert( "Please select job role");
			document.PostAdForm.jobs_role.focus( );
			return false;
		}
		var rcnt=0;
		for(var j=0; j<document.PostAdForm.jobs_role.options.length; j++)
		{
			if(document.PostAdForm.jobs_role.options[j].selected){
				rcnt++;
			}
		}
		if( rcnt > 3 )
		{
			alert( "You can select maximum of 3 options");
			return false;
		}
	}
        if (trim(document.PostAdForm.AdTitle.value) == "" ){
                alert( "Please enter the Title of the Classified Ad." );
                document.PostAdForm.AdTitle.focus( );
                return false;
        }

	if (trim(document.PostAdForm.AdDesc.value) == "" ){
		alert( "Please enter the Description of the Classified Ad." );
		document.PostAdForm.AdDesc.focus( );
		return false;
	}	
	else
	{
		var myString = document.PostAdForm.AdDesc.value;
		var mySplitResult = myString.split(" ");
		var i = mySplitResult.length;		
		for(i = 0; i < mySplitResult.length; i++)
		{			
			if(mySplitResult[i].length >= 40)
			{
				alert("Please enter valid Ad Description");				
				document.PostAdForm.AdDesc.focus();
				return false;
			}
		}

		if(myString.length<50)
		{
			alert ("The Description for the Classified Ad must be atleast 50 characters");
			document.PostAdForm.AdDesc.focus();
			return false;
		}

		//var phone= /((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}/
		var phone=/([0-9]{7})/
		var reg1 = new RegExp(phone);
		var r1 = reg1.test(myString); 
		var email=/\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))/
		var reg2 = new RegExp(email);
		var r2 = reg2.test(myString);

		if(r1)
		{
			alert('Your description should not contain phone number');
			document.PostAdForm.AdDesc.focus();
			return false;
		}

		if(r2){
			alert("Your description should not contain email address");
			document.PostAdForm.AdDesc.focus();
			return false;
		}
		
	}
	
	if ( document.PostAdForm.Validdays.selectedIndex == 0 ){
			alert( "Please select the Ad Duration");
			document.PostAdForm.Validdays.focus();
			return false;
	}
	
	if(document.PostAdForm.ResidingState.value == 0){
		alert("Please select the state");
		document.PostAdForm.ResidingState.focus();
		return false;
	}
	if((document.PostAdForm.City.value==0) || (document.PostAdForm.City.value=="")){
		alert("Please select the city");
		document.PostAdForm.City.focus();
		return false;
	}
	if((cat_id==23) && (subcat_id == 1 || subcat_id == 2 || subcat_id == 6 || subcat_id == 7)) {
			if( document.PostAdForm.locality.selectedIndex == 0 ) {
				alert( "Please select locality");
				document.PostAdForm.locality.focus( );
				return false;
			}
	}
	if (trim(document.PostAdForm.secCode.value) == "" ){
			alert( "Please enter the Security Code" );
			document.PostAdForm.secCode.focus( );
			return false;
	}	
	if(!document.PostAdForm.Terms.checked)
	{
		alert( "Please select Terms & Conditions");
		document.PostAdForm.Terms.focus( );
		return false;
	}	
	return true;
}