// function to call popup divs
var http = createRequestObject();

function createRequestObject(){
	var request_;
	var browser = navigator.appName;

	if(browser == "Microsoft Internet Explorer" || typeof ActiveXObject != "undefined" ){
		request_ = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		request_ = new XMLHttpRequest();
	}

	if (!request_) {
		alert('you browser dosn`t support AJAX Please update your browser');
		return false;
	}

	return request_;
}

// ************************** add Friends *************************************
function addFriends(username,meuser,encriptData,userFullName){
	document.getElementById('hrfAddFriends').href="#";
	invite(username,meuser,encriptData);
	sendPvMsgFromInvite(userFullName,meuser);
}

function addFriendsPopup(){
	_m3kam_login_sbox = new ShadowBox(document.getElementById("addFriendsPopup").innerHTML, {boxWidth:"450px"  , transHoldTime:500 , maxOpacity: 70 });
	_m3kam_login_sbox.show();
	//document.getElementById("addFriendsPopup").style.display= 'block';
	//document.getElementById("addFriendsPopup").style.visibility = "visible" ;
	document.getElementById("txtAddFriends").value = "";
	//document.getElementById("txtAddFriends").focus();
	// __toBeShowSelects('hide');
}

function closeAddFriends(){
	_m3kam_login_sbox = new ShadowBox(document.getElementById("addFriendsPopup").innerHTML, {boxWidth:"450px"  , transHoldTime:500 , maxOpacity: 70 });
	_m3kam_login_sbox.hide();
	//document.getElementById("addFriendsPopup").display= 'none';
	//document.getElementById('addFriendsPopup').style.visibility = "hidden" ;
	//__toBeShowSelects('show');
}

function sendPvMsgFromInvite(sendFrom,sendMeuser){
	from = sendFrom ;
	sbody = document.getElementById("txtAddFriends").value ;
	if(sbody != ""){
		var meuser = sendMeuser ;

		// replace special characters like '&' ...
		strMsg = sbody ;
		for (i=0;i<strMsg.length;i++){
			strMsg = handleText(strMsg);
		}

		AJAXGet(domainName+"sendPvtMsgs/request/to/"+meuser+"/text/"+strMsg+"/mm/"+Math.random(),completePvtMsg,false,true)
	}
	else{
		closeAddFriends();
	}
}

function completePvtMsg(sText){

	if (sText == "0"){
		alert("لم يتم ارسال همستك");
		document.getElementById("txtAddFriends").value = "";
	}
	else{
		// send pvt msg success
		closeAddFriends();
	}
}
// ***************************************************************


function sendPvtMsgPopup(name,meuser,userFullName){
	var tt = "<table border=\"0\" class=\"nPopup\" style=\"margin:auto;display:block;margin-top:50px;\">";
	tt+="<tr><td rowspan=\"4\" class=\"bordersV\"></td><td class=\"bordersH\"></td><td rowspan=\"4\" class=\"bordersV\">";
        	 tt+="</td></tr><tr><td class=\"title\"><div style=\"float:right\">أرسل همسة </div><div style=\"float:left;font-size:11px;\">";
            	tt+="<a href=\"javascript:closeSendPvtMsg();\">x إغلاق</a></div><br clear=\"all\" /></td></tr><tr><td class=\"contents\">";
        	tt+="<table style=\"border-collapse: collapse;\" align=\"right\" border=\"0\" cellpadding=\"4\" cellspacing=\"4\" dir=\"rtl\">";
				tt+="<tbody><tr><td>أرسل همسه الى   "+name+"                </td></tr><tr><td>";
			  tt+="<textarea name=\"descriptionM\" id=\"descriptionM\" class=\"brdrinput\" cols=\"40\" rows=\"7\">			  </textarea></td>";
			  tt+="<input type=\"hidden\" id=\"sendMeuser\" value="+meuser+" /><input type=\"hidden\" id=\"sendFrom\" value="+userFullName+" />";

		tt+="</tr><tr><td align=\"right\"><input type=\"button\" value=\"إرسال\" class=\"btn\" id=\"hrfPvtMsg\" onclick=\"sendPvtMsg();\" />";
			tt+="<input type=\"button\" value=\"إلغاء\" class=\"btn\" onclick=\"closeSendPvtMsg();\" /></td></tr></tbody></table>";
        tt+="</td></tr><tr><td class=\"bordersH\"></td> </tr></table><script>document.getElementById(\"descriptionM\").focus();</script>";


	_m3kam_login_sbox = new ShadowBox(tt, {boxWidth:"450px"  , transHoldTime:500 , maxOpacity: 70 });
	_m3kam_login_sbox.show();
	//document.getElementById("PvtMsgPopup").style.display= 'block';
	//document.getElementById("PvtMsgPopup").style.visibility = "visible" ;
	//document.getElementById("descriptionM").value = "" ;
	//document.getElementById("descriptionM").focus();
	//setTimeout("document.getElementById(\"descriptionM\").focus()",2500);
	//__toBeShowSelects('hide');
}

function closeSendPvtMsg(){
	//_m3kam_login_sbox = new ShadowBox(document.getElementById("PvtMsgPopup").innerHTML, {boxWidth:"450px"  , transHoldTime:500 , maxOpacity: 70 });
	_m3kam_login_sbox.hide();
	//document.getElementById("PvtMsgPopup").display= 'none';
	//document.getElementById('PvtMsgPopup').style.visibility = "hidden" ;
	//__toBeShowSelects('show');
}


function sendPvtMsg(){

	document.getElementById('hrfPvtMsg').href="#";

	from = _("sendFrom").value;
	 _("descriptionM").value = trim ( _("descriptionM").value) ;
	sbody = _("descriptionM").value;

	if(sbody == 0){
		alert("الرجاء ادخال نص الرسالة");
		document.getElementById("descriptionM").focus();
		document.getElementById('hrfPvtMsg').href='javascript:sendPvtMsg()';
	}
	else{
		var meuser = _("sendMeuser").value;
		// replace special characters like '&' ...
		strMsg = sbody ;
		for (i=0;i<strMsg.length;i++){
			strMsg = handleText(strMsg);
		}
		AJAXGet(domainName+"sendPvtMsgs/request/to/"+meuser+"/text/"+strMsg+"/mm/"+Math.random(),completefunction,false,true)
	}

}

function completefunction(sText){

	if (sText == "0"){
		alert("لم يتم ارسال همستك");
		document.getElementById("descriptionM").value="";
	}
	else{
		closeSendPvtMsg();
	}
	closeSendPvtMsg();
	//document.getElementById('hrfPvtMsg').href='javascript:sendPvtMsg()';
}

function openBlockPopup(){
	_m3kam_login_sbox = new ShadowBox(document.getElementById("blockPopup").innerHTML, {boxWidth:"450px"  , transHoldTime:500 , maxOpacity: 70 });
	_m3kam_login_sbox.show();
	//document.getElementById("blockPopup").style.display= 'block';
	//document.getElementById("blockPopup").style.visibility = "visible" ;
	//__toBeShowSelects('hide');
}

function closeBlockPopup(){
	_m3kam_login_sbox = new ShadowBox(document.getElementById("blockPopup").innerHTML, {boxWidth:"450px"  , transHoldTime:500 , maxOpacity: 70 });
	_m3kam_login_sbox.hide();
	//document.getElementById("blockPopup").style.display= 'none';
	//document.getElementById("blockPopup").style.visibility = "hidden" ;
	//__toBeShowSelects('show');
}


function BlockUser(username,meuser,encriptData) {
   AJAXGet ( domainName+'denyUser/view/userId/'+username+'/rejectedUserId/'+meuser+'/m/'+encriptData+'/mm/'+Math.floor(1000000*Math.random())   , handleInfoBlock , false  , false);
}

function handleInfoBlock( response ){
	document.getElementById("deny").innerHTML = response;
	closeBlockPopup();
}


function openAbusePopup(){
	_m3kam_login_sbox = new ShadowBox(document.getElementById("AbusePopup").innerHTML, {boxWidth:"450px"  , transHoldTime:500 , maxOpacity: 70 });
	_m3kam_login_sbox.show();
	//document.getElementById("AbusePopup").style.display= 'block';
	//document.getElementById("AbusePopup").style.visibility = "visible" ;
	document.getElementById("txtAbuse").value = "" ;
	//document.getElementById("txtAbuse").focus();
	__toBeShowSelects('hide');
}

function closeAbusePopup(){
	_m3kam_login_sbox = new ShadowBox(document.getElementById("AbusePopup").innerHTML, {boxWidth:"450px"  , transHoldTime:500 , maxOpacity: 70 });
	_m3kam_login_sbox.hide();
	//document.getElementById("AbusePopup").style.display= 'none';
	//document.getElementById("AbusePopup").style.visibility = "hidden" ;
	//__toBeShowSelects('show');
}


function abuseUserSpam(reported){
	msg = document.getElementById('txtAbuse').value ;
	document.getElementById('hrfAbuse').href='#';

	// replace special characters like '&' ...
	strMsg = msg ;
	for (i=0;i<strMsg.length;i++){
		strMsg = handleText(strMsg);
	}

	AJAXGet(domainName+"reportSpam/request/reported/"+reported+"/msg/"+strMsg,AbusedSpamHandler,false,true);
}

function AbusedSpamHandler(sText){
	if(sText == 1){
		var response = "<center>تم ارسال تبليغ  </center>";

		if (document.getElementById("AbusedUser") != undefined){
			document.getElementById("AbusedUser").innerHTML = response;
		}

		document.getElementById("AbusedUser2").innerHTML = response;
	}
	else{
		alert(sText);
	}

	closeAbusePopup();
}


function showMailerPopup(){
	document.getElementById("MailerPopup").style.display= 'block';
	document.getElementById("MailerPopup").style.visibility = "visible" ;
	__toBeShowSelects('hide');
}

function closeMailer(){
	document.getElementById("MailerPopup").style.display= 'none';
	document.getElementById("MailerPopup").style.visibility = "hidden" ;
	__toBeShowSelects('show');
}

function doMailer(){

	var doAjax = true ;
	if (document.getElementById("chkAgree").checked == true){

		if (document.getElementById("txtEmail").value == ''){
			alert('الرجاء ادخال البريد الالكتروني');
			document.getElementById("txtEmail").focus();
			doAjax = false ;
		}
		else{
			 email = document.getElementById("txtEmail").value ;
			 var regex_user = new RegExp();
		     regex_user.compile("^.+@[^\.].*\.[a-z]{2,}$");
		     var  result = regex_user.test(email);

		    if (result == false){
		    	alert("الرجاء التأكد من صحة البريد الالكتروني");
		    	document.getElementById("txtEmail").focus();
		    	doAjax = false ;
		    }
		}
	}
	if (doAjax == true){
		// do ajax here
		strEmail = document.getElementById("txtEmail").value ;
		if (document.getElementById("chkAgree").checked == true){
			strChkAgree = "1";
		}
		else{
			strChkAgree = "0";
		}
		AJAXGet(domainName+"setOtherMail/request/txtEmail/"+strEmail+"/chkAgree/"+strChkAgree,completeOtherMailer,false,true);
	}
}

function completeOtherMailer(sText){

	var temp = new Array();
	temp = sText.split(',');

	Status = temp[0];
	mailUsed =  temp[1];


	if (Status == "1" && mailUsed == "o"){
		alert("تم ارسال رسالة على بريدك الاخر لتفعيلة على موقعنا");
	}
	else if(Status == "1" && mailUsed == "m"){
		alert("تم تفعيل بريدك على معكم لاستقبال التنبيهات البريدية عليه");
	}
	else if(Status != "1"){
		alert("حدث خطأ في تفعيل التبيهات البريدية")
	}
	closeMailer();
}