//javascript document
function ajaxRequest(){
 var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]; //activeX versions to check for in IE
 if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
  for (var i=0; i<activexmodes.length; i++){
   try{
    return new ActiveXObject(activexmodes[i]);
   }
   catch(e){alert(e);
    //suppress error
   }
  }
 } else if (window.XMLHttpRequest) {// if Mozilla, Safari etc
  return new XMLHttpRequest();
 }else{
  return false;
}
}
function ajaxPost(displayArea,parameters,page_url){
	var mypostrequest=new ajaxRequest();
	mypostrequest.onreadystatechange=function(){
		if (mypostrequest.readyState==4){
			if (mypostrequest.status==200 || window.location.href.indexOf("http")==-1){
				var testVar	= mypostrequest.responseText;
				document.getElementById(displayArea).innerHTML=testVar;
			}
			else{
				alert("An error has occured making the request");
			}
		}
	}
	mypostrequest.open("POST",page_url, true);
	mypostrequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	mypostrequest.send(parameters);
}

function ajaxget(){
	namevalue='fgfg';agevalue='ssss';
	//var namevalue=encodeURIComponent(document.getElementById("name").value)
	//var agevalue=encodeURIComponent(document.getElementById("age").value)
	var parameters="name="+namevalue+"&age="+agevalue;
	ajaxPost('result'+'1',parameters,'test.php');
}



////////////////////////////////////////////






function getFile(url, passData) {
  if (window.XMLHttpRequest) {              
    AJAX=new XMLHttpRequest();              
  } else {                                  
    AJAX=new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (AJAX) {
    AJAX.open("POST", url, false);
    AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    AJAX.send(passData);
    return AJAX.responseText;                                         
  } else {
     return false;
  }                                             
}


/////validation code /////
var loggedUserRole 	= getFile('operations.php', 'page=user_role');
var mainAdminID 	= getFile('operations.php', 'page=mainAdminID');
function updateTips(t,tips) {
	tips.text(t).effect("highlight",{},2500);
}

function checkLength(o,n,min,max,tips) {
	if ( o.val().length > max || o.val().length < min ) {
		o.addClass('ui-state-error');
		updateTips("Length of " + n + " must be between "+min+" and "+max+".",tips);
		return false;
	} else {
		return true;
	}
}

function required(o,n,tips) {
	if ( o.val().length == 0 ) {
		o.addClass('ui-state-error');
		updateTips(n,tips);
		return false;
	}else{
		return true;
	}
}

function checkRegexp(o,regexp,n,tips) {

	if ( !( regexp.test( o.val() ) ) ) {
		o.addClass('ui-state-error');
		updateTips(n,tips);
		return false;
	} else {
		return true;
	}
}
function checkEmailFormat(o,n,tips) {
	regexp = /^((([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;
	if ( !( regexp.test( o.val() ) ) ) {
		o.addClass('ui-state-error');
		updateTips(n,tips);
		return false;
	} else {
		return true;
	}
}

function checkEmailExistence2(o,n,tips,var2) {
		if (var2 < 1) {
		o.addClass('ui-state-error');
		updateTips(n,tips);
		k= false;
	}else{
		k= true;
	}
return k;
}


function checkEmailExistence(o,n,tips) {
	var id = getFile('login.php', 'user_name='+o.val()+'&page=checkEmail');
	//$.post('login.php', {'user_name': o.val(),'page':'checkEmail'}, function(data) {
	//checkEmailExistence2(o,n,tips,id);
	///});
	if (id < 1) {
		o.addClass('ui-state-error');
		updateTips(n,tips);
		k= false;
	}else{
		k= true;
	}
return k;
}

	


////sandeep 
function addToFavorites(user, favId, favType){
	var parameters="user="+user+"&favId="+favId+"&favType="+favType+"&task=addFavorite";
	ajaxPost('atf',parameters,'operations.php');
	alert("You Successfully Add "+favType+" in your favorites list");	
	}
	


function howItWorks(popID,w,h){
	$.post('how_it_works.php', {'user_name': 'test'}, function(data) {
		$('#'+popID).html(data);
	});
	disablePopup();
	openNewPopup(popID,w,h);
}


function topic_reply(popID,w,h, tid){
	$.post('post.php', {'topic': tid}, function(data) {
	$('#'+popID).html(data);
	});
	disablePopup();
	openNewPopup(popID,w,h);
}

function save_post(topic){
var postMessage	= $("textarea[name='post_message']");
if(postMessage.val()){
var parameters="topic="+topic+"&postMessage="+postMessage.val()+"&task=save_post";
ajaxPost('sp',parameters,'operations.php');
disablePopup();
setTimeout("location.reload(true);",900);
} else {
alert("Please Enter Post message");	
	}
}



function sendThreadAsEmail(popID,w,h, tid){
	$.post('send_thread_email.php', {'topic': tid}, function(data) {
	$('#'+popID).html(data);
	});
	disablePopup();
	openNewPopup(popID,w,h);
	setTimeout('$("#full_name").focus()',1600);
}

function error_no_post(){
var parameters="task=error_no_post";	
ajaxPost('nopost',parameters,'operations.php');	
	}




function send_mail(topic){
if(document.getElementById('email_to_me').checked == true){
	var parameters="topic="+topic+"&task=mail_to_me";
	ajaxPost('sendmailtocurrentuser',parameters,'operations.php');
	disablePopup();
  }	
  
if(document.getElementById('email_to_others').value){
	var mailid = document.getElementById('email_to_others').value
	var parameters="topic="+topic+"&mailids="+mailid+"&task=mail_to_others";
	ajaxPost('sendmailtocurrentuser',parameters,'operations.php');
	disablePopup();
	}
  
}


function subscribe_to_the_feed(popID,w,h,tid){
	$.post('subscribe_to_the_feed.php', {'topic': tid}, function(data) {
	$('#'+popID).html(data);
	});
	disablePopup();
	openNewPopup(popID,w,h);
	setTimeout('$("#full_name").focus()',1600);
	}

	
function confirm_subscription(topic){
	var parameters="topic="+topic+"&task=confirm_subscriptions";
	ajaxPost('sendmailtocurrentuser',parameters,'operations.php');
	disablePopup();
	}	


function report_abuse(popID,w,h,tid,pid){
	$.post('report_abuse.php', {'topic': tid, 'post' : pid}, function(data) {
	$('#'+popID).html(data);
	});
	disablePopup();
	openNewPopup(popID,w,h);
	setTimeout('$("#full_name").focus()',1600);
	}
	
	
	
function confirm_report_abuse(topic, post){
 if(document.getElementById('custom_message').value){
	var message = document.getElementById('custom_message').value;
	var parameters="topic="+topic+"&post="+post+"&message="+message+"&task=confirm_abuse_report";
	ajaxPost('sendmailtocurrentuser',parameters,'operations.php');
	disablePopup();
	}
}	
	
	