function stripHTML(text){
	
	var re= /<\S[^><]*>/g
	
	var result=text.replace(re, "");
	return result;
}

function changePhoto(objectId,image){
	objectId.style.backgroundImage = image;
}

function showFolder(folderName,toDoAction) {
	form = document.getElementById("welcomeForm");	
	form.toDoAction.value =toDoAction;
	form.folder_to_display.value = folderName;
	form.submit();
}
function toInbox(folderName,action) {
	document.compose.toDoAction.value = action;
	document.compose.folder_to_display.value = folderName;
	document.compose.submit();
}
function sendto(Action) {
	form = document.getElementById("welcomeForm");
	form.toDoAction.value = Action;
	form.submit();
}
function goToTemplates(toDoAction){
	form = document.getElementById("welcomeForm");
	form.toDoAction.value  = toDoAction;
	form.submit();
}
function sendFriendShipRequest(username,toDoAction,action) {
	//regexp=/^[\w-]+(\.[\w-]+)*@([a-z0-9-]+(\.[a-z0-9-]+)*?\.[a-z]{2,6}|(\d{1,3}\.){3}\d{1,3})(:\d{4})?$/;
	regexp=/^[A-Za-z][A-Za-z0-9._-]*@[A-Za-z0-9][A-Za-z0-9._-]*\.[A-Za-z0-9][A-Za-z0-9._-]+$/;
	//regexp2=/^[\w-]+(\.[\w-]+)*/;
	regexp2=/^[A-Za-z][A-Za-z0-9._-]*$/;
	textField = document.getElementById("sendFriendship");
	if (textField.value==""){
		alert("يرجى كتابة اسم صديقك الذي تود إرسال طلب له");
		textField.focus();
		return;
	}else{
		if (regexp.test(textField.value)||regexp2.test(textField.value)){
			var friendName=textField.value;
			var temp = new Array();
			temp = friendName.split('@');		
			if (temp.length>1){
				if (temp[1]!="spacetoon.com"){
					alert("لا يمكنك إرسال دعوة إلا إلى عضو من نادي سبيستون....");
					textField.focus();
					return;
				}
			}
			friendName=temp[0];
			if(temp[0].toLowerCase() == username.toLowerCase()){
				alert('لايمكنك إرسال دعوة إلى نفسك');
				textField.focus();
				return;
			} 
			textField.value=temp[0];
			form = document.getElementById("botomForm");
			form.action=action;
			form.toDoAction.value = toDoAction ;
			form.friend_request_to.value =temp[0];
			form.submit();
			return;
		}else{
			alert(" عنوان بريد خاطئ "+textField.value+"\n");
			return;
		}	
	}
}
function doSubmit(toDoAction) {
	form = document.getElementById("contactsForm");
	form.toDoAction.value  = toDoAction;
	form.submit();
}

function handleMessage(todo){
	if(todo == 'block&report'){
		if(confirm('هل أنت متأكد من أنك لا ترغب في استقبال أي رسالة من هذا الشخص بعد الآن؟')){
		document.displaymessage.toDoAction.value = todo ;
		document.displaymessage.submit();
		}
	}
	else{
		document.displaymessage.toDoAction.value = todo ;
		document.displaymessage.submit();
	}
}
function display_pref(){
	form = document.getElementById("prefForm");
	//form.toDoAction.value =  "editProfile";
	form.submit();
}
function display_contacts() {
	form = document.getElementById("contactsForm");
	//    form.toDoAction.value  = toDoAction;
	form.submit();
}
function editProfile(){
	form = document.getElementById("profileForm");
	//form.toDoAction.value =  "editProfile";
	form.submit();
}
function EditContacts(){
		win = window.open("addressbook.html",'addressbook','height=500,width=400,menubar=no,status=no,toolbar=no,scrollbars=yes');
	}

	function send(action){
	  document.preview.toDoAction.value = action;
	  document.preview.submit();
	}

	function returnToCompose(action){
	  document.preview.toDoAction.value = action;
	  document.preview.submit();
	}

	function savePreferencesNew(){

	  var editor = CKEDITOR.instances.signature;

	  var data = editor.getData();

	  var imgReg = /<img[^>]+>/;

	  var hrefreg = /href="([^"]+)"/g;
	  
	  var bbcodeurlreg =/\[url=([^\]]+\])/g;
	  
	  var bbcodecloseurlreg = /\[\/url\]/g;

	 // alert(data.replace(hrefreg, ''));
		
		
		clearData = StripHTML(data);
		clearData = clearData.replace(/&nbsp;/g, ' ');

	  clearText = Trim(clearData);


	  if (clearText.length > 1000)
	  {
		  alert("لا يسمح لك بإدخال أكثر من 400 حرف ضمن التوقيع الخاص بك.");
		  return;
	  }
	 
	  document.preferences.toDoAction.value = "savePreferences";

	  // remove href attribute with empty string in all text in editor except 
	  // urls contains spacetoon in it
	  
	  var matches1 = data.match(hrefreg);
	  if(matches1 != null){
		  for(i=0; i<matches1.length; i++){
			if(matches1[i].indexOf('spacetoon') == -1 && matches1[i].indexOf('quran') == -1){
				data = data.replace(matches1[i], '');
			}
		  }
	  }
	  
	  var matches2 = data.match(bbcodeurlreg);
	  
	  if(matches2 != null){
		  for(i=0; i<matches2.length; i++){
			if(matches2[i].indexOf('spacetoon') == -1 && matches2[i].indexOf('quran') == -1){
				data = data.replace(matches2[i], '');
			}
		  }
		  var matches3 = data.match(bbcodecloseurlreg);
		  if(matches3 != null){
			 for(i=0; i<matches3.length; i++){
				data = data.replace(matches3[i], '');
			}
		  }
	  }
	  
	  if(matches1 != null || matches2 != null){
		editor.loadSnapshot(data);
	  }
	  document.preferences.submit();
	
	/*	alert(clearText);
		alert(clearText.length);
	/*	var element = test.element ; 
		alert("name : " + element.getName());
		alert("Outer html : " + element.getOuterHtml());
		alert("value : " + element.getValue());
*/
	}

	function attach(todo){
		var to=document.getElementById("message_to");
		var error=checkEmail(to.value);
		if(error==""){
					document.compose.toDoAction.value = todo;
					document.compose.submit();
		}else{		
		alert(error);
		}
	}

	function attachReportImges(todo){
		document.sendReport.toDoAction.value = todo;
		document.sendReport.submit();
		
	}

	function attachTemplate(todo){
		var to=document.getElementById("message_to");
		var error=checkEmail(to.value);
		if(error==""){
					document.composeTemplate.toDoAction.value = todo;
					document.composeTemplate.submit();
		}else{		
		alert(error);
		}
	}

	function whichButton(event,constant){
		if(event.keyCode == 13){
			return;
		}else{
			return;
		}
	}
	
      // drawPercentBar()
      // Written by Matthew Harvey (matt AT unlikelywords DOT com)
      // (http://www.unlikelywords.com/html-morsels/)
      // Distributed under the Creative Commons
      // "Attribution-NonCommercial-ShareAlike 2.0" License
      // (http://creativecommons.org/licenses/by-nc-sa/2.0/)
      function drawPercentBar(width, percent, color, background)
      {
        var pixels = width * (percent / 100);
        if (!background) { background = "none"; }

        document.write("<div style=\"position: relative;top:10px; left:15px;font-size:16px;"+
		"background-color: "
        + background + ";border-style: solid;"+"border-width: 1px;"+
			"height: 24px; " +
			"border-top-color: #fff;"+
			"border-left-color: #fff;"+
		"border-bottom-color: #000;" +
		"border-right-color: #000;  width: "
        + width + "px\">");
        document.write("<div style=\"height: 24px; width: " + pixels + "px; background-color: "
        + color + ";\"></div>");
        document.write("<div style=\"position: absolute; text-align: center; padding-top: .25em; width: "
        + width + "px; top: 0; left: 0\">" + percent + "%</div>");
        document.write("</div>");
      }

		function Trim(TRIM_VALUE){
			if(TRIM_VALUE.length < 1){
			return"";
			}
			TRIM_VALUE = RTrim(TRIM_VALUE);
			TRIM_VALUE = LTrim(TRIM_VALUE);
			if(TRIM_VALUE==""){
			return "";
			}
			else{
			return TRIM_VALUE;
			}
			} //End Function

			function RTrim(VALUE){
			var w_space = String.fromCharCode(32);
			var v_length = VALUE.length;
			var strTemp = "";
			if(v_length < 0){
			return"";
			}
			var iTemp = v_length -1;

			while(iTemp > -1){
			if(VALUE.charAt(iTemp) == w_space){
			}
			else{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
			}
			iTemp = iTemp-1;

			} //End While
			return strTemp;

			} //End Function

			function LTrim(VALUE){
			var w_space = String.fromCharCode(32);
			if(v_length < 1){
			return"";
			}
			var v_length = VALUE.length;
			var strTemp = "";

			var iTemp = 0;

			while(iTemp < v_length){
			if(VALUE.charAt(iTemp) == w_space){
			}
			else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
			}
			iTemp = iTemp + 1;
			} //End While
			return strTemp;
} //End Function


function StripHTML(str){
	return Trim(str.replace(/(<([^>]+)>)/ig,"")); 

}
function suggestFriend(str){
 if(str!=null && str!=""){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){ 
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('friends');
			ajaxDisplay.style.display="block";
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}
	  var url="http://magnolia.spacetoon.local/magnoliaAuthor/mail"; //////////////// ONLY LOCAL
	  //url="http://spacetoon.com/spacetoon/mail 
	url += "?toDoAction=suggestFriends&suggestion=" +str;
	ajaxRequest.open("GET", url, true);
	ajaxRequest.send(null); 
} 
}
 
function selectItem(user){
			var friend = document.getElementById('sendFriendship');
				friend.value=user;
				document.getElementById('friends').style.display="none";
}
		


