$(document).ready(function() {
	tips = $( ".validateTips" );
	function updateTips( t ) {
		tips
			.text( t )
			.addClass( "ui-state-highlight" );
		setTimeout(function() {
			tips.removeClass( "ui-state-highlight", 1500 );
		}, 500 );
	}
	function checkLength( o, n, min, max ) {
		//alert(n);
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass( "ui-state-error" );
			updateTips( "Length of " + n + " must be between " +
				min + " and " + max + "." );
			return false;
		} else {
			o.removeClass( "ui-state-error" );
			return true;
		}
	}
	function checkRegexp( o, regexp, n ) {
		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass( "ui-state-error" );
			updateTips( n );
			return false;
		} else {
			o.removeClass( "ui-state-error" );
			return true;
		}
	}
	jQuery.fn.ForceNumericOnly =
	function()
	{
		return this.each(function()
		{
			$(this).keydown(function(e)
			{
				var key = e.charCode || e.keyCode || 0;
				// allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
				return (
					key == 8 || 
					key == 9 ||
					key == 46 ||
					(key >= 37 && key <= 40) ||
					(key >= 48 && key <= 57) ||
					(key >= 96 && key <= 105));
			})
		})
	};
	$("#PostMobileTxt").ForceNumericOnly();
	$("#PostPriceTxt").ForceNumericOnly();
	$("#PostAgeYearTxt").ForceNumericOnly();
	$("#PostAgeMonthTxt").ForceNumericOnly();
	$("#RegrMobileTxt").ForceNumericOnly();
	$("#RegrpageMobileTxt").ForceNumericOnly();
	$("#login-form,#loginpage-form").submit(function(e) {
		e.preventDefault();
		var bValid = true;
		var prefix;
		if ($(this).attr('id') == 'login-form')
			prefix='Lgin';
		else 
			prefix='Lginpage';
		var Login_name= $("#"+prefix+"NameTxt");
		var password= $("#"+prefix+"PwdTxt");
		var uid = Login_name.val();
		var pwd = password.val();

		bValid = bValid && checkLength( Login_name, "Username", 3, 15 );
		bValid = bValid && checkLength( password, "Password", 5, 15 );

		bValid = bValid && checkRegexp( Login_name, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
		bValid = bValid && checkRegexp( password, /^([0-9a-zA-Z])+$/, "Password field only allow : a-z 0-9" );
		
		if (bValid)
		{
			$( this ).dialog( "close" );
			viewmaskpaneldemo();
			$.post(BaseURL+"n24needs/login/Authenticate", { uid:uid, pwd:pwd}, function (data) {
				if (data.substring(0,5) == "Error")
				{
					alert(data);
				}
				else {
					eval('var sessarry={'+data+'}');
						$("a#signout").click(function(e) {
							e.preventDefault();
							SignOut(); 
						});
						$( "a#showcitydiv,a#showcitydiv1" )
							.click(function() {
								$( "#citydiv-form" ).dialog( "open" );
						});
					$('#PostNameTxt').val(sessarry['USERNAME']);
					$('#PostEmailTxt').val(sessarry['USEREMAIL']);
					$('#PostMobileTxt').val(sessarry['USERMOBILE']);
					$('#detUserEmailTxt').val(sessarry['USEREMAIL']);
					$('#detUserMobileTxt').val(sessarry['USERMOBILE']);
					$('#detUserNameTxt').val(sessarry['USERNAME']);
					$('#PostCityTxt').val(sessarry['USERCITY']);
					$('#PostCityTxtHdn').val(sessarry['USERCITYID']);
					//if (location.href.lastIndexOf('LoginPage')>0)
						document.location.href= 'http://'+sessarry['USERCITY']+'.24needs.com';
				}
			});
			
		}
	});
	$("#register-form,#Registerpage-form").submit(function(e) {
		e.preventDefault();
		var bValid = true;
		var prefix;
		var address="";
		if ($(this).attr('id') == 'register-form')
			prefix='Regr';
		else {
			address = $("#"+prefix+"addrTxtArea").val();
			prefix='Regrpage';

		}
		var regname = $("#"+prefix+"NameTxt"),
			reguname = $("#"+prefix+"UserNameTxt"),
			regcityname = $("#"+prefix+"CityNameTxt"),
			regmobile = $("#"+prefix+"MobileTxt"),
			regemail = $("#"+prefix+"EmailTxt"),
			regconfemail = $("#"+prefix+"CnfmEmailTxt"),
			regaddress = $("#"+prefix+"addrTxtArea"),
			regpwd = $("#"+prefix+"PwdTxt"),
			regcnfpwd = $("#"+prefix+"CnfmPwdTxt");
		bValid = bValid && checkLength( regname, "Name", 2, 25 );
		bValid = bValid && checkLength( reguname, "username", 3, 15 );
		bValid = bValid && checkLength( regcityname, "city", 3, 15 );
		bValid = bValid && checkLength( regemail, "email", 6, 80 );
		if (bValid && (regemail.val() != regconfemail.val()))
		{
			regconfemail.addClass( "ui-state-error" );
			updateTips( "Confirm E-mail doesn't match with E-mail" );
			hidemaskpaneldemo();
			return false;
		}
		bValid = bValid && checkLength( regpwd, "password", 5, 15 );
		if (bValid && (regpwd.val() != regcnfpwd.val()))
		{
			regcnfpwd.addClass( "ui-state-error" );
			updateTips( "Confirm Password doesn't match with Password" );
			hidemaskpaneldemo();
			return false;
		}

		bValid = bValid && checkRegexp( regname, /^[a-z]([a-z ])+$/i, "Name may consist of alphabets and space and begins with a letter." );
		bValid = bValid && checkRegexp( reguname, /^[a-z]([0-9a-z_])+$/i, "Username may consist of a-z, 0-9, underscores, begin with a letter." );
		bValid = bValid && checkRegexp( regcityname, /^[a-z]([a-z])+$/i, "City may consist of only alphabets." );
		bValid = bValid && checkRegexp( regemail, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" );
		bValid = bValid && checkRegexp( regpwd, /^([0-9a-zA-Z~!@#\$%\^&\*\?<>|_\-\=\+])+$/, "Password field does not allow illegal characters." );

		if (bValid)
		{
			viewmaskpaneldemo();
			//alert('before register ajax');
			$.post(BaseURL+"n24needs/login/Register", { address: address, uid: $("#"+prefix+"UserNameTxt").val(), pwd: $("#"+prefix+"PwdTxt").val(), cnfpwd: $("#"+prefix+"CnfmPwdTxt").val(), name: $("#"+prefix+"NameTxt").val(), email: $("#"+prefix+"EmailTxt").val(), cnfemail: $("#"+prefix+"CnfmEmailTxt").val(), city: $("#"+prefix+"CityNameTxt").val(), mobile: $("#"+prefix+"MobileTxt").val() }, function (data) {
				alert(data);
				if (data == "Account registered. Please check your email for details on how to activate it.")
				document.location.href= BaseURL;
				hidemaskpaneldemo();
			});
			if ($(this).attr('id') == 'register-form')
				$( this ).dialog( "close" );
			
		}
		else
			hidemaskpaneldemo();

	});
	$("a#signout").click(function(e) {
		e.preventDefault();
		SignOut();
	});
	$("#forgotpwd-form").submit(function(e) {
		e.preventDefault();
			var userid = $("#FPUserIdTxt").val();
			var email = $("#FPEmailTxt").val();
			if (userid== "" && email == "" )
			{
				alert("Please enter UserID or Email");
			} else {
				$( this ).dialog( "close" );
				$.post(BaseURL+"n24needs/login/ForgotPassword", { userid: userid , email: email }, function (data) {
					if (data.substring(0,6) == "userid")
					{
						alert(data);
					}
					else {
						alert(data);
					}
				});
			}
	});
	$("#resetpwd-form").submit(function(e) {
		e.preventDefault();
			var oldpwd = $("#ResetoldpassTXT").val();
			var newpwd = $("#ResetnewpassTXT").val();
			var cnfpwd = $("#ResetconfpassTXT").val();
			if (oldpwd == "" || newpwd == "" || cnfpwd == "")
			{
				alert("Please enter complete data")
				return
			}
			else if (newpwd != cnfpwd)
			{
				alert("New Password and confirm passwords are not matching")
				return
			}else {
				$.post(BaseURL+"n24needs/login/ResetPassword", { oldpwd: oldpwd , newpwd: newpwd, cnfpwd: cnfpwd }, function (data) {
					if (data.substring(0,3) == "old")
					{
						alert(data);
						$("#ResetoldpassTXT").val()=oldpwd;
						$("#ResetnewpassTXT").val()=newpwd;
						$("#ResetconfpassTXT").val()=cnfpwd;
					}
					else {
						alert(data);
						$("#ResetoldpassTXT").val()='';
						$("#ResetnewpassTXT").val()='';
						$("#ResetconfpassTXT").val()='';
					}
				});
			}
	});
	$('#PostSubCatSlct').change(function() {
		ShowPostDiv('PostSubCatSlct');
	});
	$('#art_CategorySlct').change(function() {
		if (this.options[this.selectedIndex].value!=0) {
			getCat(this,'art_SubCatSlct');
		} else {$('#art_CategorySlct').html('<option value=\"0\">Select Category First</option>');} 
	});
	$('#PostCategorySlct').change(function() {
		if ($('input[name=PostUserTypeRadBtn]:radio').is(':checked')) {
			if (this.options[this.selectedIndex].value!=0) {
				getCat(this,'PostSubCatSlct');
				ShowPostDiv('PostCategorySlct');
			} else {$('#PostSubCatSlct').html('<option value=\"0\">Select Category First</option>');} 
		} 
		else {alert('Please select Provider or Seeker.'); $("#PostCategorySlct option:first-child").attr("selected","selected"); return false;}
	});
	$('input[name=PostUserTypeRadBtn]:radio').click(function() {
		if ($('#PostNameTxt').val() == "")
		{
			alert('Please enter your Name'); $("input[name=PostUserTypeRadBtn]:radio").attr("checked", false); return false;
		}
		if ($('#PostEmailTxt').val() == "")
		{
			alert('Please enter your E-mail Id'); $("input[name=PostUserTypeRadBtn]:radio").attr("checked", false); return false;
		}
	});
	$('input[name=AdTypeRadBtn]:radio').click(function() {
		var urlarray=document.location.href.split('/');
		var urlarraycnt=urlarray.length;
		var url="";
		var i=0;
		var currval=$(this).val();
		var oldval='';
		while ( i<urlarraycnt )
		{
			if (i==0)
			url=urlarray[i];
			else { 
				url=url+"/"+urlarray[i];
				if (urlarray[i]=='search')
				{
					url=url+"/"+currval;
					i++;
					oldval=urlarray[i];
				}
			}
			i++;
			if (i==9)
			i++;
		}
		if (oldval!=currval)
		{
			document.location.href=url;
		}
	});
	$('#PostItemTxt').keypress(function() {
	  if ($('#PostSubCatSlct option:selected').val()=='0' || $('#PostSubCatSlct option:selected').val()=='-1')
	  {
		  alert('Please select Sub Category');
		  $('#PostSubCatSlct').focus();
		  return false;
	  }
	});
	$(function() {

	$( "#dialog:ui-dialog" ).dialog( "destroy" );
	var name = $( "#LginNameTxt" ),
		password = $( "#LginPwdTxt" ),
		loginFields = $( [] ).add( name ).add( password ),
		tips = $( ".validateTips" );

		$( "#login-form" ).dialog({
			autoOpen: false,
			height: 180,
			width: 300,
			modal: true,
			close: function() {
				loginFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
		$( "#signinlnk" )
			.click(function() {
				$( "#login-form" ).dialog( "open" );
		});
		var frgtpwd_userid =  $( "#FPUserIdTxt" ),
		frgtpwd_email = $( "#FPEmailTxt" ),
		frgtpwdFields = $( [] ).add( frgtpwd_userid ).add( frgtpwd_email ),
		tips = $( ".validateTips" );
		$( "#forgotpwd-form" ).dialog({
			autoOpen: false,
			height: 200,
			width: 400,
			modal: true,
			close: function() {
				frgtpwdFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
		$( "#forgotpwdlnk,#forgotpwdlnk1" )
			.click(function() {
				$( "#login-form" ).dialog( "close" );
				$( "#forgotpwd-form" ).dialog( "open" );
		});
		var regname = $("#RegrNameTxt"),
			reguname = $("#RegrUserNameTxt"),
			regcityname = $("#RegrCityNameTxt"),
			regmobile = $("#RegrMobileNameTxt"),
			regemail = $("#RegrEmailTxt"),
			regconfemail = $("#RegrCnfmEmailTxt"),
			regpwd = $("#RegrPwdTxt"),
			regcnfpwd = $("#RegrCnfmPwdTxt"),
			regFields = $( [] ).add( regname ).add( reguname ).add( regcityname ).add( regmobile ).add( regemail ).add( regconfemail ).add( regpwd ).add( regcnfpwd ),
			tips = $( ".validateTips" );
		$( "#register-form" ).dialog({
			autoOpen: false,
			height: 335,
			width: 350,
			modal: true,
			close: function() {
				regFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
		$( "#citydiv-form" ).dialog({
			autoOpen: false,
			height: 400,
			width: 600,
			modal: true
		});
		$( "#maskdiv-form" ).dialog({
			autoOpen: false,
			height: 70,
			width: 120,
			modal: true
		});

		$( "#registerlnk,#registerlnk1" )
			.click(function() {
				$( "#login-form" ).dialog( "close" );
				$( "#RegrCityNameTxt" ).autocomplete({
					source: BaseURL+"n24needs/autocomplete/MainCityTxt/",
					minLength: 2,
					select: function( event, ui ) { null;}
				});
				$( "#register-form" ).dialog( "open" );
		});
		$( "a#showcitydiv,a#showcitydiv1" )
			.click(function() {
				$( "#citydiv-form" ).dialog( "open" );
		});
		$( "#details-form" ).dialog({
			autoOpen: false,
			height: 220,
			width: 370,
			modal: true

		});
		$( "#pics-form" ).dialog({
			autoOpen: false,
			width: 550,
			modal: true
		});
		$('#pics-form').dialog(
			  "resize", "auto"
		);
		$( ".fontresultlinedetailex" )
			.click(function() {
				$( "#express-form" ).dialog( "open" );
		});
		$( "#express-form" ).dialog({
			autoOpen: false,
			height: 150,
			width: 300,
			modal: true

		});
		$( ".fontresultlinedetailsd" )
			.click(function() {
				$( "#details-form" ).dialog( "open" );
		});

 });
});
function SignOut() {
	viewmaskpaneldemo();
	$.post(BaseURL+"n24needs/login/SignOut", {}, function (data) {
		alert("You are Signed out");
			$('#MainLoginDiv').html(data);
			$('#PostNameTxt').val('');
			$('#PostEmailTxt').val('');
			$('#PostMobileTxt').val('');
			$('#detUserEmailTxt').val('');
			$('#detUserMobileTxt').val('');
			$('#detUserNameTxt').val('');
			/*if (location.href.lastIndexOf('myaccount')>0)
			{
				window.open('n24needs','_self');
			} else if (location.href.lastIndexOf('post')>0)*/
			document.location.href= BaseURL;
			/*$( "#signinlnk" )
				.click(function() {
					$( "#login-form" ).dialog( "open" );
			});
			$( "#registerlnk" )
				.click(function() {
					$( "#RegrCityNameTxt" ).autocomplete({
						source: BaseURL+"n24needs/autocomplete/MainCityTxt/",
						minLength: 2,
						select: function( event, ui ) { null;}
					});
					$( "#register-form" ).dialog( "open" );
			});*/
	});
}
function search() {
	var srchtype = $('#SearchCategorySlct').val(),keywordstr;
	if ($('#MainItemTxt').val() == "") keywordstr='All';
	else keywordstr = $('#MainItemTxt').val();
	document.location.href= BaseURL+'n24needs/search/A/'+srchtype+'/-1/'+keywordstr;
}

function ShowPostDiv(obj) {
	$( "#PostCityTxt" ).autocomplete({
		source: BaseURL+"n24needs/autocomplete/MainCityTxt/",
		minLength: 2,
		select: function( event, ui ) { null;}
	});
	if ($('#'+obj+' option:selected').val()=='1' && obj=='PostCategorySlct')
	{
		$( "#REpostpropcityTxt" ).autocomplete({
			source: BaseURL+"n24needs/autocomplete/MainCityTxt/",
			minLength: 2,
			select: function( event, ui ) { null;}
		});
		$( "#REpostlocationTxt" ).autocomplete({
			source: BaseURL+"n24needs/autocomplete/MainLocationTxt/",
			minLength: 2,
			select: function( event, ui ) { null;}
		});
		$('#cars,#bikes,#itemdiv').hide();
		$('#RealEstatediv').show();
	}
	else if ($('#'+obj+' option:selected').val()=='11' && obj=='PostSubCatSlct')
	{
		$('#RealEstatediv,#bikes,#itemdiv').hide();
		$('#cars').show();
	}
	else if ($('#'+obj+' option:selected').val()=='12' && obj=='PostSubCatSlct')
	{
		$('#RealEstatediv,#cars,#itemdiv').hide();
		$('#bikes').show();
	}
	else if ($('#PostCategorySlct option:selected').val()=='3' || $('#PostCategorySlct option:selected').val()=='4' || $('#PostCategorySlct option:selected').val()=='5' ||
		$('#PostCategorySlct option:selected').val()=='8' || $('#PostCategorySlct option:selected').val()=='9' || $('#PostCategorySlct option:selected').val()=='10')
	{
		$('#RealEstatediv,#cars,#bikes,#itemdiv').hide();
	}
	else if ($('#PostCategorySlct option:selected').val()!='1')
	{
		$('#RealEstatediv,#cars,#bikes').hide();
		$('#itemdiv').show();
	}

}
function post(){
	document.location.href= BaseURL+'n24needs/post';
}

function postconfirm(id){
	null;
	document.location.href= BaseURL+'n24needs/postconfirm/'+id;
}

function DirectPostItem()
{
		var ItemDesc=tinyMCE.getInstanceById("PostItemDescTxtArea").getBody().innerHTML;
		
		var Negotiable="Y";
		var Name=$("#PostNameTxt").val(),
		Email=$("#PostEmailTxt").val();
		var Mobile=$("#PostMobileTxt").val(),
		Provider=$('input:radio[name=PostUserTypeRadBtn]:checked').val();
		var City=$("#PostCityTxt").val(),
		CityName=$("select[name=PostCityTxt] option:selected").text(),		
		Category=$('#PostCategorySlct').val(),
		SubCategory=$('#PostSubCatSlct').val();
		var ItemName=$("#PostItemTxt").val();

		var ItemAlerts= $('input[name=AlertsCheck]:checkbox').is(':checked') ? "Y" : "N";
		var ItemPublish= $('input[name=PublishCheck]:checkbox').is(':checked') ? "Y" : "N";
		var PostID = $("#PostID").val();
		if (Category=='1')
		{
			var PropAddress=$("#REpostaddressTxt").val(),
			LocationID=$("#REpostlocationTxtHdn").val(),
			LocationName=$("#REpostlocationTxt").val(),
			PropCityID=$("#REpostpropcityTxtHdn").val(),
			PropCityName=$("#REpostpropcityTxt").val(),
			Pin=$("#REpostpinTxt").val(),
			NoBedroom=$("#REpostnobedroomTxt").val(),
			NoBathroom=$("#REpostnobathroomTxt").val(),
			Area=$("#REpostareaTxt").val(),
			AreaUOM=$("#Areameasure").val(),
			//Ageyear=$("#REpostAgeYearTxt").val(),
			//Agemonth=$("#REpostAgeMonthTxt").val(),
			Possyear=$("#REpostpossyearTxt").val(),
			Possmonth=$("#REpostpossmonthTxt").val(),
			Ownership=$("#REpostownershipTxt").val(),
			RefineMonths=0,
			//PropType=$("#REPropPriceTxt").val(),
			//REproptag=$("#REpostproptagTxt").val(),
			PropType=$("#REProppostTypeTxt").val();
			$.post(BaseURL+"n24needs/post/R", { PropAddress:PropAddress,
												PropCityID:PropCityID,
												PropCityName:PropCityName,
												LocationID:LocationID,
												LocationName:LocationName,
												Pin:Pin,
												NoBedroom:NoBedroom,
												NoBathroom:NoBathroom,
												Area:Area,
												AreaUOM:AreaUOM,
												Possyear:Possyear,
												Possmonth:Possmonth,
												Ownership:Ownership ,
												PropType:PropType,
												RefineMonths:RefineMonths,
												PostID:PostID}, function (data) {
																				var NewItem=$('input:radio[name=PostNewRadBtn]:checked').val(),
																				AgeYear=$("#REpostAgeYearTxt").val(),
																				AgeMonth=$("#REpostAgeMonthTxt").val(),
																				Price=$("#PostPriceTxt").val(),
																				Negotiable= $('input[name=Negotiable]:checkbox').is(':checked') ? "Y" : "N";
																				var City=$("#PostCityTxt").val(),
																				CityName=$("select[name=PostCityTxt] option:selected").text();		
																				$.post(BaseURL+"n24needs/post/Y", { Name:Name,
																													Email:Email,
																													Mobile:Mobile,
																													Provider:Provider,
																													City:City,
																													CityName:CityName,
																													Category:Category,
																													SubCategory:SubCategory,
																													ItemName:ItemName,
																													ItemDesc:ItemDesc,
																													ItemAlerts:ItemAlerts ,
																													ItemPublish:ItemPublish,
																													NewItem:NewItem,
																													AgeYear:AgeYear,
																													AgeMonth:AgeMonth,
																													Price:Price,
																													Negotiable:Negotiable,
																													PostID:PostID}, function (data) {postconfirm(PostID);});	});		
		}
		else if (SubCategory=='11' || SubCategory=='12')
		{
			var autotype = (SubCategory=='11') ? 'car' : 'bike' ;
			var carmakeslct =$("#"+autotype+"make").val(),
			carmodelslct=$("#"+autotype+"model").val(),
			caryearslct =$("#"+autotype+"year").val(),
			carfuelslct =$("#"+autotype+"fuel").val(),
			carexpiry =$("#"+autotype+"expiry").val(),
			carmileage=$("#"+autotype+"mileage").val(),
			carcolor=$("#"+autotype+"color").val(),
			kmsdone=$("#kmsdone").val(),
			nowners=$("#nowners").val(),
			carowner=$('input:radio[name='+autotype+'owner]:checked').val(),
			carinsurance=$('input:radio[name='+autotype+'insurance]:checked').val();
			$.post(BaseURL+"n24needs/post/A", { carmakeslct:carmakeslct,
												carmodelslct:carmodelslct,
												caryearslct:caryearslct,
												carfuelslct:carfuelslct,
												carexpiry:carexpiry,
												carmileage:carmileage,
												carcolor:carcolor,
												kmsdone:kmsdone,
												nowners:nowners,
												carowner:carowner,
												carinsurance:carinsurance ,
												PostID:PostID}, function (data) {
																				var NewItem=$('input:radio[name=PostNewRadBtn]:checked').val(),
																				AgeYear=$("#PostAgeYearTxt").val(),
																				AgeMonth=$("#PostAgeMonthTxt").val(),
																				Price=$("#PostPriceTxt").val(),
																				Negotiable= $('input[name=Negotiable]:checkbox').is(':checked') ? "Y" : "N";
																				var City=$("#PostCityTxt").val(),
																				CityName=$("select[name=PostCityTxt] option:selected").text();		
																				$.post(BaseURL+"n24needs/post/Y", { Name:Name,
																													Email:Email,
																													Mobile:Mobile,
																													Provider:Provider,
																													City:City,
																													CityName:CityName,
																													Category:Category,
																													SubCategory:SubCategory,
																													ItemName:ItemName,
																													ItemDesc:ItemDesc,
																													ItemAlerts:ItemAlerts ,
																													ItemPublish:ItemPublish,
																													NewItem:NewItem,
																													AgeYear:AgeYear,
																													AgeMonth:AgeMonth,
																													Price:Price,
																													Negotiable:Negotiable,
																													PostID:PostID}, function (data) {postconfirm(PostID);});	});		
				
		}
		else if (Category!='1')
		{
			var NewItem=$('input:radio[name=PostNewRadBtn]:checked').val(),
			AgeYear=$("#PostAgeYearTxt").val(),
			AgeMonth=$("#PostAgeMonthTxt").val(),
			Price=$("#PostPriceTxt").val(),
			Negotiable= $('input[name=Negotiable]:checkbox').is(':checked') ? "Y" : "N";
			var City=$("#PostCityTxt").val(),
			CityName=$("select[name=PostCityTxt] option:selected").text();		
			$.post(BaseURL+"n24needs/post/Y", { Name:Name,
												Email:Email,
												Mobile:Mobile,
												Provider:Provider,
												City:City,
												CityName:CityName,
												Category:Category,
												SubCategory:SubCategory,
												ItemName:ItemName,
												ItemDesc:ItemDesc,
												ItemAlerts:ItemAlerts ,
												ItemPublish:ItemPublish,
												NewItem:NewItem,
												AgeYear:AgeYear,
												AgeMonth:AgeMonth,
												Price:Price,
												Negotiable:Negotiable,
												PostID:PostID}, function (data) {postconfirm(PostID);});		
		}
}
function viewmaskpaneldemo()
{
	$( "#maskdiv-form" ).dialog( "open" );
}
function hidemaskpaneldemo()
{
	$( "#maskdiv-form" ).dialog( "close" );
}

function getCat(obj,target) {		
		var contextId=obj.value;
		var name=obj.name;
		if (name != 'art_CategorySlct' )
		{
			name='cat';
		}
		viewmaskpaneldemo();
		$.post(BaseURL+"n24needs/ajax/"+name+"/"+target, { cont:contextId}, function (data) {
				$('#'+target).html(data);
		        hidemaskpaneldemo();
		});
}
	
function getModel(obj,VehicleType,target) {		
	var VehicleMake=obj.value;
	if (VehicleMake=='Others')
	{
		document.getElementById(target+"div").innerHTML="<input type=text name="+target+" id="+target+" >";
	}
	else {
		$.post(BaseURL+"n24needs/ajax/carmodel/"+target, { cont:VehicleType, cont2:VehicleMake}, function (data) {
			$('#'+target).html(data);
		});		
	}
}

function ReDirect2Search(type,idx,id,name){
			$("#MainCityTxt").val(name);
			$("#MainCityTxtHdn").val(id);
			$("#PostCityTxt").val(name);
			$("#PostCityTxtHdn").val(id);
			$("strong#HomeCity").html("("+name+")");
			$("#citydiv-form").dialog( "close" );
			//alert(name);
		/*	if (name == 'allcities')
				window.open('http://www.24needs.com/','_self');
			else
				window.open('http://'+name.toLowerCase()+'.24needs.com/','_self');  */
			
}

function set_sess_var(name,value,getset) {
	$.get(BaseURL+"n24needs/session/"+name+"/"+value+"/"+getset, { }, function (data) {
	null;
	});
}
function limiter(keydown) {
	var x=tinyMCE.getContent();
    var regex=/<[^>]*>/gi;
    outputstr=x.replace(regex,"");
    if(outputstr.length > 5000){
      alert("The length of your message is "+outputstr.length +" the max num of characters \n allowed for this text Area is 5000");
      }
}
function clearall(){
	$("#PostNameTxt").val("");
	$("#PostEmailTxt").val("");
	$("#PostMobileTxt").val("");
	$("#PostItemTxt").val("");
}

function clearall_art(){
	$("#art_NameTxt").val("");
	$("#art_EmailTxt").val("");
	$("#art_ItemTxt").val("");

}
function check_captcha_and_post(action,obj) {
	var recaptcha_challenge_field=$('#recaptcha_challenge_field').val(),
	recaptcha_response_field =$('#recaptcha_response_field').val();
	viewmaskpaneldemo();
if (action=='Register')
			{
	$.post(BaseURL+"n24needs/captcharedirect/recaptcha/verify-recaptcha", { challenge:recaptcha_challenge_field, response:recaptcha_response_field}, function (data) {
		if(data.indexOf("Passed")!=-1){	
			if (action=='Register')
			{
				$("#"+obj).submit();
				//alert('d');
			}
		}
		else {
			Recaptcha.reload();
			errMsg = "Your reCaptcha answer was not valid. Please re-enter.";	
            alert(errMsg);
			hidemaskpaneldemo();
			return false;
		}
	})
	}
	else{
		var Name=$("#PostNameTxt").val(),
		Email=$("#PostEmailTxt").val();
		Mobile=$("#PostMobileTxt").val();

		if (Name=="")
		{
			alert("Please enter your name to post an Item");
			hidemaskpaneldemo();
			return false;
		}
		if (Email=="")
		{
			alert("Please enter your E-Mail Id to post an Item and for better tracking");
			hidemaskpaneldemo();
			return false;
		}
		if (Mobile !="")
		{
			if (!(isNumeric(Mobile)))
			{
			alert("Please Numerics only for mobile");
			hidemaskpaneldemo();
			return false;
			}
		}

		if(!(validateEmail(Email)))
		{
			alert("Please enter Proper Email ID");
			hidemaskpaneldemo();
			return false;

		}  
		if (!$('input[name=PostUserTypeRadBtn]:radio').is(':checked'))
		{
			alert("Please select provider or seeker");
			hidemaskpaneldemo();
			return false;
		}
		var Category=$('#PostCategorySlct').val(),
		SubCategory=$('#PostSubCatSlct').val();
		if (Category=='0')
		{
			alert('Please select the proper Category');
			hidemaskpaneldemo();
			return false;
		}
		if (SubCategory=='0')
		{
			alert('Please select the proper Sub-Category');
			hidemaskpaneldemo();
			return false;
		}
		var ItemName=$("#PostItemTxt").val();
		if (ItemName=='')
		{
			alert('Please enter the title of the post');
			hidemaskpaneldemo();
			return false;
		}

		if ($("#MultiFile1_wrap_labels").children().length > 0)
		{
			alert('Upload Image First');
			hidemaskpaneldemo();
			return false;
		}
		DirectPostItem();
	};
}
function SendDetails(name,postid) {		
		$( '#details-form' ).dialog( 'close' );
		var name=$("#detPostUserNameTxt").val();
		var mobile=$("#detMobileTxt").html();
		var email=$("#detEmailTxt").html();
		var postid=$("#detPostIdTxt").val();
		
		var seekermobile=$("#detUserMobileTxt").val();
		var seekeremail=$("#detUserEmailTxt").val();
		var seekername=$("#detUserNameTxt").val();
		
		if (seekermobile== "" && seekeremail == "" )
		{
			alert("Please enter Email or Mobile");
			$( '#details-form' ).dialog( 'open' );
		}
		else {
			viewmaskpaneldemo();
			$.post(BaseURL+"n24needs/commute/senddetails/"+seekeremail, { name:name,mobile:mobile,email:email,postid:postid,seekermobile:seekermobile,seekername:seekername }, function (data) {
				alert(data);
                                hidemaskpaneldemo();
			});
			
		}
}
function Express(name,postid) {		
	$( '#express-form' ).dialog( 'close' );

	var name=$("#detPostUserNameTxt").val();
	var mobile=$("#detMobileTxt").html();
	var email=$("#detEmailTxt").html();
	var postid=$("#detPostIdTxt").val();
	
	var seekermobile=$("#SeekerMobileTxt").val();
	var seekeremail=$("#SeekerMailTxt").val();
	
	if (seekermobile== "" && seekeremail == "" )
	{
		alert("Please enter Email or Mobile");
		$( '#express-form' ).dialog( 'open' );
	}
	else {
			viewmaskpaneldemo();
			$.post(BaseURL+"n24needs/commute/express/"+seekeremail, { name:name,mobile:mobile,email:email,postid:postid,seekermobile:seekermobile }, function (data) {
				alert(data);
hidemaskpaneldemo();
			});
			
	}
}
function ShowPic(postid,center){
	if (typeof center == "undefined") {
		center = false;
	}
	$.get(BaseURL+"n24needs/ShowPics/"+postid, { }, function (data) {
		if (data=="No Pics Found")
		{
			$( '#pics-form' ).dialog( 'close' );
		}
		else {
			$("#pics-form").html(data);
			$('#pics-form').dialog( 'open' );
		}
	});
}
function checkfeedback() {
	if ($('#FBNameTxt').val().replace(/ /g,'')=='' && $('#FBEmailTxt').val().replace(/ /g,'')=='') {
		alert('Please enter atleast Name or Email'); return false;
	}
	else if ($('#FBMessageTxtArea').val().replace(/ /g,'')==''){
		alert('Please enter feedback message to submit'); return false;
	}
	else return true;
}



function DirectPostArticle()
{
		var ArticleDesc=tinyMCE.getInstanceById("art_ItemDescTxtArea").getBody().innerHTML;
		
		var ArticleName=$("#art_ItemTxt").val(),
		Email=$("#art_EmailTxt").val();
		//var Mobile=$("#PostMobileTxt").val(),
		var City=$("#art_CityTxt").val(),
		CityName=$("select[name=art_CityTxt] option:selected").text(),		
		Category=$('#art_CategorySlct').val(),
		SubCategory=$('#art_SubCatSlct').val();
		var ArticleName=$("#art_ItemTxt").val(),ArticleVidLnk=$("#art_VideoLink").val();
                if ($("#MultiFile1_wrap_labels").children().length > 0)
		{
			alert('Upload Image First');
			hidemaskpaneldemo();
			return false;
		}
		var ArticleID = $("#ArticleID").val();
			$.post(BaseURL+"cnews/Articlepost/Y", { ArticleID:ArticleID,
													ArticleName:ArticleName,
												CategoryId:Category,
												SubcatID:SubCategory,
												Content:ArticleDesc,
												CityID:City,
												CityName:CityName, 
                                                                                                ArticleVidLnk:ArticleVidLnk 
												}, function (data) {postconfirm(ArticleID);});		
}

function validateEmail(elementValue){  
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
   return emailPattern.test(elementValue);  
 } 

 function isNumeric(value) {
  if (value != null && !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
  return true;
}
