var d = document;

var w = window;

function postform_cedvalidate(area1,max_words1,elementid)
{
	area	=	d.getElementById(area1);
	max_words	=	max_words1;
	var v	= area.value;
	if( v.length > max_words ) {
		area.value	= v.substr(0, max_words);
	}
	d.getElementById(elementid).innerHTML = (max_words - area.value.length);
	setTimeout( function() { postform_validate(area1,max_words1,elementid); }, 289 );
}

function postform_validateno(elmid)
{
	var No=document.getElementById(elmid);
	var strRegex= /^[0-9]*$/; 
	var number=No.value;
	if ((!(No.value).match(strRegex)))
	{								
		alert("please fill correct "+elmid+". It must contain only digits");
		No.value="";
		No.focus();
	}
}

function authorise_grind(grind_id,aftermsg)
{
	req	=	ajax_init(false);
	if( ! req ) { return; }
	var thislink	= d.getElementById("grindapprove_"+grind_id);
	req.onreadystatechange	= function() {
		if( req.readyState != 4  ) { return; }
		if( req.responseText != "OK" ) { return; }
		thislink.style.cursor	= "pointer";
		grinds_synchronize();
	}
	req.open("POST", siteurl+"ajax/grindapprove/r:"+Math.round(Math.random()*1000), true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send("grind_id="+encodeURIComponent(grind_id));
	thislink.style.cursor	= "wait";
}

function grinds_synchronize()
{
	var req = ajax_init(false);
	if( ! req ) { return; }
	var dv	= d.getElementById("grouplist");
	if( ! dv ) { return; }	
	var url	= w.location.href.toString();
	if( ! url ) { return; }
	if( url.substr(0, siteurl.length) == siteurl ) {
		url	= url.substr(siteurl.length);
		url	= siteurl+url+"/from:ajax/r:"+Math.round(Math.random()*1000);
	}
	else {
		url	= url.replace(/^http(s)?\:\/\//, "");
		url	= url.substr(url.indexOf("/"));
		url	= siteurl+url+"/from:ajax/r:"+Math.round(Math.random()*1000);
	}
	var i, ch, lastpostdate = "", lastpostdates = [];
	for(i=0; i<dv.childNodes.length; i++) {
		ch	= dv.childNodes[i];
		if( !ch.id || !ch.id.match(/^post_/) || !ch.className.match(/^(m)?post/) || !ch.getAttribute("postdate") ) { continue; }
		lastpostdates[lastpostdates.length]	= parseInt(ch.getAttribute("postdate"), 10);
	}
	for(i=0; i<lastpostdates.length; i++) {
		lastpostdate	= Math.max(lastpostdate, lastpostdates[i]);
	}
	req.onreadystatechange	= function() {
		if( req.readyState != 4  ) { return; }
		if( ! req.responseText ) { return; }
		var txt	= ltrim(req.responseText);
		if( txt.substr(0,3) != "OK:" ) { return; }
		txt	= txt.substr(3);
		dv.innerHTML	= txt;
		setTimeout(grinds_synchronize_step2, 1);
	}
	req.open("POST", url, true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send("lastpostdate="+encodeURIComponent(lastpostdate));
	if(sync_tmout) {
		clearTimeout(sync_tmout);
		sync_tmout	= false;
	}
}
function grinds_synchronize_step2()
{
	var dv	= d.getElementById("grouplist");
	if( ! dv ) { return; }
	var i, ch, h, tmp;
	for(i=0; i<dv.childNodes.length; i++) {
		ch	= dv.childNodes[i];
		if( !ch.id || !ch.className.match(/^(m)?grind/) || !ch.getAttribute("postdate") ) { continue; }
		if( ch.style.display != "none" ) { continue; }
		if( disable_animations ) {
			ch.style.display	= "block";
			return;
		}
	}
	sync_tmout	= setTimeout( grinds_synchronize, 20000 );
}

function deletegrind(grind_id)
{
	req	=	ajax_init(false);
	if( ! req ) { return; }
	var thislink	= d.getElementById("grinddelete_"+grind_id);
	req.onreadystatechange	= function() {
		if( req.readyState != 4  ) { return; }
		if( req.responseText != "OK" ) { return; }
		thislink.style.cursor	= "pointer";
		grinds_synchronize();
	}
	req.open("POST", siteurl+"ajax/grinddelete/r:"+Math.round(Math.random()*1000), true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send("grind_id="+encodeURIComponent(grind_id));
	thislink.style.cursor	= "wait";
}



function submitgrindcomment(grind_id)
{
	var txt	= d.getElementById("grind_comment_"+grind_id+"_textarea");
	var btn	= d.getElementById("grind_comment_"+grind_id+"_submitbtn");
	txt.value	= trim(txt.value);
	if( txt.value === "" ) {
		txt.focus();
		return;
	}
	txt.disabled	= true;
	btn.disabled	= true;
	txt.style.cursor	= "wait";
	btn.style.cursor	= "wait";
	btn.blur();
	var req = ajax_init(false);
	if( ! req ) { return; }

	req.onreadystatechange	= function() {
		if( req.readyState != 4  ) { return; }
		if( ! req.responseText ) { return; }
		var res	= trim(req.responseText);
		if( res != "OK" ) {
			return;
		}
			viewgrind_synchronize();
		//postcomments_open_state[post_id] = 0;
	}
	req.open("POST", siteurl+"ajax/grind-comment/ajaxtp:txt/r:"+Math.round(Math.random()*1000), true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send("grindid="+encodeURIComponent(grind_id)+"&message="+encodeURIComponent(txt.value));
}

function viewgrind_synchronize()
{
	var req = ajax_init(false);
	if( ! req ) { return; }
	var dv	= d.getElementById("view_grind");
	if( ! dv ) { return; }
	var url	= w.location.href.toString();
	if( ! url ) { return; }
	if( url.substr(0, siteurl.length) == siteurl ) {
		url	= url.substr(siteurl.length);
		url	= siteurl+url+"/from:ajax/r:"+Math.round(Math.random()*1000);
	}
	else {
		url	= url.replace(/^http(s)?\:\/\//, "");
		url	= url.substr(url.indexOf("/"));
		url	= siteurl+url+"/from:ajax/r:"+Math.round(Math.random()*1000);
	}
	req.onreadystatechange	= function() {
		if( req.readyState != 4  ) { return; }
		if( ! req.responseText ) { return; }
		var txt	= ltrim(req.responseText);
		if( txt.substr(0,3) != "OK:" ) { return; }
		txt	= txt.substr(3);
		var ndv	= d.createElement("DIV");
		ndv.innerHTML	= txt;
		setTimeout( function() {
			ndv	= ndv.getElementsByTagName("DIV")[0].cloneNode(true);
			dv.style.visibility	= "hidden";
			while(dv.firstChild) {
				dv.removeChild(dv.firstChild);
			}
			dv.parentNode.insertBefore(ndv, dv);
			dv.style.display	= "none";
			dv.parentNode.removeChild(dv);
		}, 1);
		setTimeout( function() {
			var i, all	= ndv.getElementsByTagName("INPUT");
			for(i=0; i<all.length; i++) {
				postform_forbid_hotkeys_conflicts(all[i]);
			}
			all	= ndv.getElementsByTagName("TEXTAREA");
			for(i=0; i<all.length; i++) {
				postform_forbid_hotkeys_conflicts(all[i]);
			}
		}, 1 );
	}
	req.open("POST", url, true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send("");
}

function grindcomment_delete(grind_id,comment_id,msg)
{
	if(!confirm(msg)) { return; }
	var req	=	ajax_init(false);
	if(!req) { return; }
	var thislink	=	d.getElementById("grind_commentdelete_"+comment_id);
	req.onreadystatechange	= function() {
		if( req.readyState != 4  ) { return; }
		if( req.responseText != "OK" ) { return; }
		thislink.style.cursor	= "pointer";
		viewgrind_synchronize();
	}
	req.open("POST", siteurl+"ajax/delgrindcomment/r:"+Math.round(Math.random()*1000), true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send("grind_id="+encodeURIComponent(grind_id)+"&comment_id="+encodeURIComponent(comment_id));
	thislink.style.cursor	= "wait";
}

function showinnerdiv(id) {
	
	document.getElementById('outerheadersubitems_'+id).style.display="block";
	
}

function hideinnerdiv(id) {
	
	document.getElementById('outerheadersubitems_'+id).style.display="none";
	
}

function addtoenable(id) {
	var elm	=	d.getElementById('enabled');	
	if(!elm.value.match(','+id+','))
	{
		elm.value	+= ","+id+",";
		elm.value	= elm.value.replace(",,", ",");
	}
}

function deletefromenable(id) {
	var elm	=	d.getElementById('enabled');
	elm.value	= elm.value.replace(","+id+",", ",");
	elm.value	= elm.value.replace(",,", ",");
}

var is_del	=	[];
var is_featured	=	[];

function delgrind(id) {
	var elm	=	d.getElementById('delete');
	if(is_del[id]==0)
	{
		elm.value	+= ","+id+",";
		elm.value	= elm.value.replace(",,", ",");
		is_del[id]	=	1;
	}
	else
	{
		elm.value	= elm.value.replace(","+id+",", ",");
		elm.value	= elm.value.replace(",,", ",");
		is_del[id]	=	0
	}
}

function featuredgrind(id) {
	var elm	=	d.getElementById('featured');
	if(elm.value.match(','+id+','))
	{
		elm.value	= elm.value.replace(","+id+",", ",");
		elm.value	= elm.value.replace(",,", ",");
		is_featured[id]	=	0
	}
	else
	{
		elm.value	+= ","+id+",";
		elm.value	= elm.value.replace(",,", ",");
		is_featured[id]	=	1;
	}
}

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){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}


function checkformemail(){
	var req = ajax_init(true);
	if( ! req ) { return; }
	var emailID=document.getElementById("ced_email");
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}	
	var dv	= d.getElementById("lpForm");
	req.onreadystatechange	= function() {
		if( req.readyState != 4  ) { return; }
		if( ! req.responseText ) { return; }
		var txt	= ltrim(req.responseText);		
		if(txt=='OK')
		{
			cedpf_post_state=1;
			postform_htmlobject_show("cedpf_postedok", 36, function() { pf_open_state=0; pf_post_state=1; postform_statusmsg_setTimeout(); });
			document.getElementById("ced_name").value="";
			document.getElementById("ced_message").value="";
			document.getElementById("ced_email").value="";
		}
		else
		{
			cedpf_post_state=3;
			postform_htmlobject_show("cedpf_postederror", 36);
			document.getElementById("ced_name").value="";
			document.getElementById("ced_message").value="";
			document.getElementById("ced_email").value="";
		}
		
	}
	var email=emailID.value;
	var name=document.getElementById("ced_name").value;
	var message=document.getElementById("ced_message").value;
	var subject=document.getElementById("ced_subject").value;
	var to_mailid=document.getElementById("ced_toemailid").value;
	req.open("POST", siteurl+"ajax/postcontactform-submit/ajaxtp:xml/r:"+Math.round(Math.random()*1000), true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	
	req.send("name="+name+"&emailid="+email+"&subject="+subject+"&message="+message+"&toemailid="+to_mailid);
	return true;
 }
function cedpostform_topmsg_close()
{
	d.getElementById("cedpf_postedok").style.display="none";
	d.getElementById("cedpf_postederror").style.display="none";
}

function filterbyid(id) {
	/*if(!id) { return; }
	alert(id);
	window.location=siteurl+'grinds/subid:'+id;*/
	alert("DFGDF");
}

function bump_grind(id) {
	var req	=	ajax_init(true);
	if(!req) { return false; }
	if(!id) { return }
	var thislink	=	d.getElementById('bump_'+id);
	req.onreadystatechange	=	function() {
		if(req.readyState !=4 ) { return; }
		if(! req.responseText) { return ; }
		var txt =	ltrim(req.responseText);
		if(txt!='OK') { return; }
		thislink.style.cursor	= "pointer";
		grinds_synchronize();
	}
	thislink.style.cursor	= "wait";
	req.open("POST", siteurl+"ajax/bump_grind/r:"+Math.round(Math.random()*1000), true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send("grind_id="+encodeURIComponent(id));
}

function showprice() {	

	var elmids = '';
	var selectedArray = new Array();
	var selObj = document.getElementById('subject');
	var i;
	var count = 0;
	for (i=0; i<selObj.options.length; i++) {
		if (selObj.options[i].selected) {
		  selectedArray[count] = selObj.options[i].value;
		  count++;
		}
	}
	elmids = selectedArray;
	var req	=	ajax_init(true);
	if(!req) { return false; }
	if(elmids=='') { return; }
	req.onreadystatechange	=	function() {
		if(req.readyState !=4 ) { return; }
		if(! req.responseText) { return ; }
		var txt =	ltrim(req.responseText);
		if(is_featured==1) {
			txt		=	parseInt(txt)+parseInt(count)*5;	
		}	
		document.getElementById('showprice_div').style.display="block";
		document.getElementById('showprice_div').innerHTML	=	'Price Of Your Ad Is &#8364;'+ txt;
		document.getElementById('grind_price').value	=	txt;
	}
	req.open("POST", siteurl+"ajax/calprice/r:"+Math.round(Math.random()*1000), true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	req.send("subject_ids="+encodeURIComponent(elmids));
}

var is_featured	=	0;

function addfeaturedprice() {
	if(is_featured==1) {
		return;
	}
	if(is_featured==0) {
		
		is_featured	=	1;
		var selectedArray = new Array();
		var selObj = document.getElementById('subject');
		var i;
		var count = 0;
		var featured_price	=	0;
		for (i=0; i<selObj.options.length; i++) {
			if (selObj.options[i].selected) {
			  selectedArray[count] = selObj.options[i].value;
			  featured_price	=	parseInt(featured_price)+10;
			  count++;
			}
		}
		var	prevalue	=	document.getElementById('grind_price').value
		prevalue	=	parseInt(prevalue)+parseInt(featured_price);
		document.getElementById('showprice_div').style.display="block";
		document.getElementById('showprice_div').innerHTML	=	'Price Of Your Ad Is &#8364;'+ prevalue;
		document.getElementById('grind_price').value	=	prevalue;	
	}
}

function lessfeaturedprice() {
	if(is_featured==0) {
		return;
	}
	if(is_featured==1) {
		is_featured=0;
		var selectedArray = new Array();
		var selObj = document.getElementById('subject');
		var i;
		var count = 0;
		var featured_price	=	0;
		for (i=0; i<selObj.options.length; i++) {
			if (selObj.options[i].selected) {
			  selectedArray[count] = selObj.options[i].value;
			  featured_price	=	parseInt(featured_price)+10;
			  count++;
			}
		}
		var	prevalue	=	document.getElementById('grind_price').value
		prevalue	=	parseInt(prevalue)-parseInt(featured_price);
		document.getElementById('showprice_div').style.display="block";
		document.getElementById('showprice_div').innerHTML	=	'Price Of Your Ad Is &#8364;'+ prevalue;
		document.getElementById('grind_price').value	=	prevalue;		
	}
}
