// Radio Button Validation
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 adsvalidate(){

	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 (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 (document.PostAdForm.password.value == ""){
		alert ("Please enter password");
		document.PostAdForm.password.value="";
		document.PostAdForm.password.focus( );
		return false;
	}
	var len=document.PostAdForm.password.value.length;
	if(len<4){
		alert("Your password must be atleast be 4 characters long.");
		document.PostAdForm.password.focus();
		return false;
	}
	if (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;
	}
	if ( document.PostAdForm.AdTitle.value == "" ){
		alert( "Please enter the Title of the Classified Ad." );
		document.PostAdForm.AdTitle.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==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 ( document.PostAdForm.AdDesc.value == "" ){
		alert( "Please enter the Description of the Classified Ad." );
		document.PostAdForm.AdDesc.focus( );
		return false;
	}
	var desc=document.PostAdForm.AdDesc.value;
	if(desc.length<50){
		alert ("The Description for the Classied Ad must be atleast 50 characters");
		document.PostAdForm.AdDesc.focus();
		return false;
	}
	if (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 (document.PostAdForm.Telephone.value == ""){
		alert ("Please enter the Telephone Number");
		document.PostAdForm.Telephone.focus( );
		return false;
	}
	if (!IsPhone(document.PostAdForm.Telephone.value,"phone number.")) {
		document.PostAdForm.Telephone.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 ( document.PostAdForm.Validdays.selectedIndex == 0 ){
		alert( "Please select the Ad Duration");
		document.PostAdForm.Validdays.focus( );
		return false;
	}
return true;
}