function trimSpaces ( str ) {

	var trimmed = '';
	var start_edge = 0;
	var end_edge = 0;

	// Detect start edge
	for(var i = 0; i < str.length; i++)
		if (str.charAt(i) != ' ') {
			start_edge = i;
			break;
		}
	// Detect end edge
	for(i = str.length - 1; i > 0; i--)
		if (str.charAt(i) != ' ') {
			end_edge = i;
			break;
		}
	// Get what's between edges
	for(i = start_edge; i <= end_edge; i++)
		trimmed += str.charAt(i);
		
	if (trimmed == ' ') trimmed = '';
		
	return trimmed;

} // end function trimSpaces



/*function GetUsernameAndPassword(){
  
     var username= convertToHex("public");    
	 alert('username = '+document.formLib.user_name.value);
	 document.formLib.user_name.value = "public"// username;
	 alert('username = '+document.formLib.user_name.value);
	
	
	 var password= convertToHex("publicuser");
	 alert('password = '+document.formLib.password.value);
     document.formLib.password.value = "publicuser"//password;
     alert('password = '+document.formLib.password.value);

	return ture;	
}*/
function Encypt(str)
{
  var chrsz=8;	
  var bin = Array();
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < str.length * chrsz; i += chrsz)
    bin[i>>5] |= (str.charCodeAt(i / chrsz) & mask) << (i%32);
  return bin;
}

function Decrypt(bin)
{
  var chrsz=8;	
  var str = "";
  var mask = (1 << chrsz) - 1;
  for(var i = 0; i < bin.length * 32; i += chrsz)
    str += String.fromCharCode((bin[i>>5] >>> (i % 32)) & mask);
  return str;
}

function GetUsernameAndPassword(flag){
	
	if(flag==0)
	   {
		str1="public"
		str2="P@rlDM$PublicU$er"
	 }
	else
	 if(flag==1)
	{
		str1="member"
		str2="P@rlDM$MemberU$er"
	}
		
	EncUsername = Encypt(str1);
	//alert('username = '+EncUsername);
	Username = Decrypt(EncUsername);
	document.formLib.user_name.value = Username;
	//alert('username = '+document.formLib.user_name.value);
	
	EncPassword = Encypt(str2);
	//alert('password = '+EncPassword);
	password = Decrypt(EncPassword);
	document.formLib.password.value= password;
	//alert('password = '+document.formLib.password.value);
}

function convertToHex(num) { 
  var hex = ''; 
  for (i=0;i<num.length;i++) 
    hex += "%" + num.charCodeAt(i).toString(16).toUpperCase(); 
  return hex; 
}

function mem_page(){
		var login=document.getElementById('login');
		var pass=document.getElementById('mem_password');
		var login_value=trimSpaces(login.value);
		var pass_value=trimSpaces(pass.value);
		if (!login_value.length){
			alert('الرجاء إدخال اسم العضو');
			document.getElementById('login').select();
			document.getElementById('login').focus();
			return false;
		}
		if (!pass_value.length){
			alert('الرجاء إدخال كلمة المرور');
			document.getElementById('mem_password').select();
			document.getElementById('mem_password').focus();
			return false;
		}
}

//function for complaints
function complaint_check(){
		var user_name=document.getElementById('complaint_user_name');
		var user_name_value=trimSpaces(user_name.value);

		
		var phone=document.getElementById('complaint_phone');
		var phone_value=trimSpaces(phone.value);
		

		var address=document.getElementById('complaint_address');
		var address_value=trimSpaces(address.value);
		
		var email=document.getElementById('complaint_email');
		var email_value=trimSpaces(email.value);

				
		var notes=document.getElementById('complaint_notes');
		var notes_value=trimSpaces(notes.value);
		
		
		if (!user_name_value.length){
			alert('الرجاء إدخال اسم المرسل');
			document.getElementById('complaint_user_name').select();
			document.getElementById('complaint_user_name').focus();
			return false;
		}
		if (!phone_value.length){
			alert('الرجاء إدخال رقم الهاتف');
			document.getElementById('complaint_phone').select();
			document.getElementById('complaint_phone').focus();
			return false;
		}else{
			var phoneRE = /^\d{5}-\d{3}-\d+$/;
			var ISmatch = phoneRE.test(phone_value);
			if (!ISmatch)
			{
				window.alert("رقم الهاتف خاطئ");
				document.getElementById('complaint_phone').select();
				document.getElementById('complaint_phone').focus();
				return false;
			}			
		}
		
		if (!address_value.length){
			alert('الرجاء إدخال العنوان');
			document.getElementById('complaint_address').select();
			document.getElementById('complaint_address').focus();
			return false;
		}
		if (email_value.length){
				var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if (!filter.test(email_value))
			{
				window.alert("البريد الالكتروني خاطئ");
				document.getElementById('complaint_email').select();
				document.getElementById('complaint_email').focus();
				return false;
			}			
			
		}

		if (!notes_value.length){
			alert('الرجاء إدخال مضمون الشكوى');
			document.getElementById('complaint_notes').select();
			document.getElementById('complaint_notes').focus();
			return false;
		}
}


function Upload_File_Complaint(thisSTR){
 	validformFile = /(.doc|.DOC|.txt|.TXT|.xls|.XLS)$/;
	if(trimSpaces(thisSTR) == ""  || thisSTR == null) {
	     	          alert("!!!!!!! ÇÎÊÑ ÇÓã ÇáãáÝ ");
		          return false;
		
	   }else
	      	if(!validformFile.test(thisSTR)){
	     	          alert("!!! ÇÎÊÑ ãáÝ äÕ Ãæ  ÊäÓíÞ doc, xls");
		          return false;
	    	}else{
                        var upload_button=document.getElementById('upload');
		        upload_button.value="ok";
                        var upload_labl=document.getElementById('Uploaded_file');
		        upload_labl.innerHTML ="the file is:"+ thisSTR;



	    		return true;
	    	}
}



function in_mem_page(){
		var login=document.getElementById('in_login');
		var pass=document.getElementById('in_password');
		var login_value=trimSpaces(login.value);
		var pass_value=trimSpaces(pass.value);
		if (!login_value.length){
			alert('الرجاء إدخال اسم العضو');
			document.getElementById('in_login').select();
			document.getElementById('in_login').focus();
			return false;
		}
		if (!pass_value.length){
			alert('الرجاء إدخال كلمة المرور');
			document.getElementById('in_password').select();
			document.getElementById('in_password').focus();
			return false;
		}
}

function PopUp(page,name,w,h,scroll,pos){
	if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
	if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
	else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	win=window.open(page,name,settings);
	win.focus();
}