function newWindow(url,height,width,scrollbars) {
    popupWindow = window.open(url,'popUpWindow','height='+height+',width='+width+',left=100,top=100,resizable=yes,scrollbars='+scrollbars+',toolbar=no,menubar=no,location=no,directories=no, status=no');
}

function closeWindow(url)
{
window.close(url, 'remote')
}


$(document).ready(function(){
$('#tabs1 div').hide();
$('#tabs1 div:first').show();
$('#tabs1 ul li:first').addClass('active');
$('#tabs1 ul li a').click(function(){ 
$('#tabs1 ul li').removeClass('active');
$(this).parent().addClass('active'); 
var currentTab = $(this).attr('href'); 
$('#tabs1 div').hide();
$(currentTab).show();
return false;
});
});


//check user vaild with ajax by wec


    $(document).ready(function () {
        // Username validation logic
        
		var validatePassword = $('#validatePassword');
		
		$('#password').keyup(function () {
            // cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
            var t = this; 
            
            // only run the check if the username has actually changed - also means we skip meta keys
            if (this.value != this.lastValue) {
                
                // the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
                // a particular key, it will only fire when the release the key.
                                
                if (this.timer) clearTimeout(this.timer);
                
                // show our holding text in the validation message space
                validatePassword.removeClass('error').html('<img src="images/ajax-loader.gif" height="16" width="16" /> checking availability...');
                
                // fire an ajax request in 1/5 of a second
                this.timer = setTimeout(function () {
                    $.ajax({
                        url: 'ajax-validation.php',
                        data: 'action=check_password&password=' + t.value,
                        dataType: 'json',
                        type: 'post',
                        success: function (j) {
                            // put the 'msg' field from the $resp array from check_username (php code) in to the validation message
                            validatePassword.html(j.msg);
                        }
                    });
                }, 200);
                
                // copy the latest value to avoid sending requests when we don't need to
                this.lastValue = this.value;
            }
        });
		
		var validateUsername = $('#validateUsername');
        $('#username').keyup(function () {
            // cache the 'this' instance as we need access to it within a setTimeout, where 'this' is set to 'window'
            var t = this; 
            
            // only run the check if the username has actually changed - also means we skip meta keys
            if (this.value != this.lastValue) {
                
                // the timeout logic means the ajax doesn't fire with *every* key press, i.e. if the user holds down
                // a particular key, it will only fire when the release the key.
                                
                if (this.timer) clearTimeout(this.timer);
                
                // show our holding text in the validation message space
                validateUsername.removeClass('error').html('<img src="images/ajax-loader.gif" height="16" width="16" /> checking availability...');
                
                // fire an ajax request in 1/5 of a second
                this.timer = setTimeout(function () {
                    $.ajax({
                        url: 'ajax-validation.php',
                        data: 'action=check_username&username=' + t.value,
                        dataType: 'json',
                        type: 'post',
                        success: function (j) {
                            // put the 'msg' field from the $resp array from check_username (php code) in to the validation message
                            validateUsername.html(j.msg);
                        }
                    });
                }, 200);
                
                // copy the latest value to avoid sending requests when we don't need to
                this.lastValue = this.value;
            }
        });
        
        // avatar validation
        // we use keyup *and* change because 
        $('#avatar').keyup(function () {
            var t = this;
            clearTimeout(this.timer);
            this.timer = setTimeout(function () {
                if (t.value == t.current) {
                    return true;
                }

                var preview = $('#validateAvatar').html('<img src="images/ajax-loader.gif" height="16" width="16" /> trying to load avatar...');
                var i = new Image();

                clearTimeout(t.timeout);

                if (t.value == '') {
                    preview.html('');
                } else {
                    i.src = t.value;
                    i.height = 32;
                    i.width = 32;
                    i.className = 'avatar';

                    // set a timeout of x seconds to load the image, otherwise, show the fail message
                    t.timeout = setTimeout(function () {
                        preview.html('Image could not be loaded.');
                        i = null;
                    }, 3000);

                    // if the dummy image holder loads, we'll show the image in the validation space,
                    // but importantly, we clear the timer set above
                    i.onload = function () {
                        clearTimeout(t.timeout);
                        preview.empty().append(i);
                        i = null;
                    };
                }
                
                t.current = t.value;
            }, 250);
        }).change(function () {
            $(this).keyup(); // call the keyup function
        });
    });


//loading form
function loading_request() {
 //$('#loading').html('<p><img src="../templates/standard/images/loadingAnimation.gif" width="220" height="19" /></p>');
 $('#loading').html('<div class="loading" >جاري التحميل ...</div>');
}

//contact us

$(document).ready(function(){
$("#contact-form").submit(function(){
$("#msg").fadeOut("slow"); 
var str = $(this).serialize();


   $.ajax({
   type: "POST",
   url: "WEC_contact.php?send=process",
   data: str,
   success: function(msg){

$("#msg").fadeIn("slow");  
$("#msg").ajaxComplete(function(event, request, settings){

if(msg == 'OK'){ // Message Sent? Show the 'Thank You' message and hide the form
  
result = msg;

$("#fields").hide();
$("#loading").hide();

}else{

result = msg;
$("#loading").hide();

}
$(this).fadeIn("slow");
$(this).html(result);


});

}

 });

return false;

});

});

//login user
$(document).ready(function(){
$("#login-form").submit(function(){
$("#msg").fadeOut("slow"); 
var str = $(this).serialize();


   $.ajax({
   type: "POST",
   url: "WEC_login.php?login=1",
   data: str,
   success: function(msg){
  
$("#msg").fadeIn("slow");
$("#msg").ajaxComplete( function(event, request, settings){

if(msg == 'OK'){ // Message Sent? Show the 'Thank You' message and hide the form
  
result = msg;

$("#fields").hide();
$("#loading").hide();

}else{

result = msg;
$("#loading").hide();

}

$(this).fadeIn("slow");
$(this).html(result);

});

}

 });

return false;

});

});


 
//add comment
$(document).ready(function(){
$("#comment-form").submit(function(){
$("#msg").fadeOut("slow"); 
var inputSiteurl = $("#siteurl"); 
var siteurl = inputSiteurl.attr("value"); 

var str = $(this).serialize();

//alert ("id: "+message+" - name: "+message);

   $.ajax({
   type: 'POST',
   url:  siteurl +'/WEC_comment.php?p=process',
   data: str,
   success: function(msg){
 
$("#msg").fadeIn("slow"); 

$("#msg").ajaxComplete(function(event, request, settings){

if(msg == 'OK'){ // Message Sent? Show the 'Thank You' message and hide the form
  
result = msg;

$("#fields").hide();
$("#loading").hide();

}else{

result = msg;
$("#loading").hide();
}

$("#msg").fadeIn("slow"); 
$(this).html(result);


});

}

 });

return false;

});

});


//Register
$(document).ready(function(){
$("#register-form").submit(function(){
$("#msg").fadeOut("slow"); 
var inputSiteurl = $("#siteurl"); 
var siteurl = inputSiteurl.attr("value"); 

var str = $(this).serialize();

   $.ajax({
   type: 'POST',
   url:  siteurl +'/WEC_register.php?p=process',
   data: str,
   success: function(msg){
 
$("#msg").fadeIn("slow"); 

$("#msg").ajaxComplete(function(event, request, settings){

if(msg == 'OK'){ // Message Sent? Show the 'Thank You' message and hide the form
  
result = msg;

$("#fields").hide();
$("#loading").hide();

}else{

result = msg;
$("#loading").hide();
}

$("#msg").fadeIn("slow"); 
$(this).html(result);


});

}

 });

return false;

});

});


//Send to Friend
$(document).ready(function(){
$("#friend-form").submit(function(){
$("#msg").fadeOut("slow"); 
var inputSiteurl = $("#siteurl"); 
var siteurl = inputSiteurl.attr("value"); 

var str = $(this).serialize();

   $.ajax({
   type: 'POST',
   url:  siteurl +'/WEC_sendfriend.php?p=process',
   data: str,
   success: function(msg){
 
$("#msg").fadeIn("slow"); 

$("#msg").ajaxComplete(function(event, request, settings){

if(msg == 'OK'){ // Message Sent? Show the 'Thank You' message and hide the form
  
result = msg;

$("#fields").hide();
$("#loading").hide();

}else{

result = msg;
$("#loading").hide();
}

$("#msg").fadeIn("slow"); 
$(this).html(result);


});

}

 });

return false;

});

});


//submit content
$(document).ready(function(){
$("#submit-form").submit(function(){
$("#msg").fadeOut("slow"); 
var inputSiteurl = $("#siteurl"); 
var siteurl = inputSiteurl.attr("value"); 

var str = $(this).serialize();

   $.ajax({
   type: 'POST',
   url:  siteurl +'/WEC_submit.php?new=process',
   data: str,
   success: function(msg){
 
$("#msg").fadeIn("slow"); 

$("#msg").ajaxComplete(function(event, request, settings){

if(msg == 'OK'){ // Message Sent? Show the 'Thank You' message and hide the form
  
result = msg;

$("#fields").hide();
$("#loading").hide();

}else{

result = msg;
$("#loading").hide();
}

$("#msg").fadeIn("slow"); 
$(this).html(result);


});

}

 });

return false;

});

});


//lost password
$(document).ready(function(){
$("#lost_password-form").submit(function(){
$("#msg").fadeOut("slow"); 
var inputSiteurl = $("#siteurl"); 
var siteurl = inputSiteurl.attr("value"); 

var str = $(this).serialize();

   $.ajax({
   type: 'POST',
   url:  siteurl +'/WEC_profile.php?p=lost_password_process',
   data: str,
   success: function(msg){
 
$("#msg").fadeIn("slow"); 

$("#msg").ajaxComplete(function(event, request, settings){

if(msg == 'OK'){ // Message Sent? Show the 'Thank You' message and hide the form
  
result = msg;

$("#fields").hide();
$("#loading").hide();

}else{

result = msg;
$("#loading").hide();
}

$("#msg").fadeIn("slow"); 
$(this).html(result);


});

}

 });

return false;

});

});


//edit profile
$(document).ready(function(){
$("#edit_profile-form").submit(function(){
$("#msg").fadeOut("slow"); 
var inputSiteurl = $("#siteurl"); 
var siteurl = inputSiteurl.attr("value"); 

var str = $(this).serialize();

   $.ajax({
   type: 'POST',
   url:  siteurl +'/WEC_profile.php?p=edit_profile_process',
   data: str,
   success: function(msg){
 
$("#msg").fadeIn("slow"); 

$("#msg").ajaxComplete(function(event, request, settings){

if(msg == 'OK'){ // Message Sent? Show the 'Thank You' message and hide the form
  
result = msg;

$("#fields").hide();
$("#loading").hide();

}else{

result = msg;
$("#loading").hide();
}

$("#msg").fadeIn("slow"); 
$(this).html(result);


});

}

 });

return false;

});

});



//upload image
//show and hide the loading message
function loadingmessage(msg, show_hide){
	if(show_hide=="show"){
		$('#loader').show();
		$('#progress').show().text(msg);
		$('#uploaded_image').html('');
	}else if(show_hide=="hide"){
		$('#loader').hide();
		$('#progress').text('').hide();
	}else{
		$('#loader').hide();
		$('#progress').text('').hide();
		$('#uploaded_image').html('');
	}
}

//delete the image when the delete link is clicked.
function deleteimage(image_name, thumbnail_image){
var inputSiteurl = $("#siteurl"); 
var siteurl = inputSiteurl.attr("value"); 
$('#upload_status').html('');
$('#del_image').html('').show();

	loadingmessage('جاري حذف الصورة...', 'show');
	$.ajax({
		type: 'POST',
		url: siteurl +'/WEC_submit.php?p=get_img',
		data: 'up_file=del_image&image_name='+image_name+'&thumbnail_image='+thumbnail_image,
		cache: false,
		success: function(response){
			loadingmessage('', 'hide');
			response = unescape(response);
			var response = response.split("|");
			var responseType = response[0];
			var responseMsg = response[1];
			if(responseType=="success"){
				$('#upload_status').show().html('<p>'+responseMsg+'</p>');
				//$('#uploaded_image').html('');
				document.getElementById('get_image').value = '';
				$('#upload_link').html('<a id="upload_link" href="#">رفع صورة ...</a>').show();

			}else{
				$('#upload_status').show().html('<p>خطأ لم يتم حذف الصورة</p>'+response);
			}
		}
	});
}

$(document).ready(function () {
var inputSiteurl = $("#siteurl"); 
var siteurl = inputSiteurl.attr("value"); 

var inputPathimg = $("#path_img"); 
var path_img = inputPathimg.attr("value"); 

		$('#loader').hide();
		$('#progress').hide();
		var myUpload = $('#upload_link').upload({
		   name: 'image',
		   action: siteurl +'/WEC_submit.php?p=get_img',
		   enctype: 'multipart/form-data',
		   params: {upload:'Upload'},
		   autoSubmit: true,
		   onSubmit: function() {
		   		$('#upload_status').html('').hide();
				loadingmessage('جاري رفع الصورة...', 'show');
		   },
		   onComplete: function(response) {
		   		loadingmessage('', 'hide');
				response = unescape(response);
				var response = response.split("|");
				var responseType = response[0];
				var responseMsg = response[1];
				if(responseType=="success"){
					var current_width = response[2];
					var current_height = response[3];
					//display message that the file has been uploaded
					$('#upload_status').show().html('تم رفع الصورة بنجاح');
					//put the image in the appropriate div
					
	                $('#del_image').html('<img src="'+ siteurl +'/'+ path_img +''+responseMsg+'" /><br /><a href="javascript:deleteimage(\''+responseMsg+'\', \''+responseMsg+'\');">حذف الصورة</a>');
                	document.getElementById('get_image').value = responseMsg;
                    $('#upload_link').html('').hide();
					
				}else if(responseType=="error"){
					$('#upload_status').show().html('<p>'+responseMsg+'</p>');

				}else{
					$('#upload_status').show().html('<p>خطأ لم يتم رفع الصورة</p>'+response);
					
				}
		   }
		});

}); 





//upload file 
function loadingmessage2(msg, show_hide){
	if(show_hide=="show"){
		$('#loader2').show();
		$('#progress2').show().text(msg);
		$('#uploaded_image2').html('');
	}else if(show_hide=="hide"){
		$('#loader2').hide();
		$('#progress2').text('').hide();
	}else{
		$('#loader2').hide();
		$('#progress2').text('').hide();
		$('#uploaded_image2').html('');
	}
}

//delete the image when the delete link is clicked.
function deletefile(file_name){
var inputSiteurl = $("#siteurl"); 
var siteurl = inputSiteurl.attr("value"); 
$('#upload_status2').html('');
$('#del_file').html('').show();

	loadingmessage2('جاري حذف الملف...', 'show');
	$.ajax({
		type: 'POST',
		url: siteurl +'/WEC_submit.php?p=get_file',
		data: 'up_file=del_file&file_name='+file_name,
		cache: false,
		success: function(response){
			loadingmessage2('', 'hide');
			response = unescape(response);
			var response = response.split("|");
			var responseType = response[0];
			var responseMsg = response[1];
			if(responseType=="success"){
				$('#upload_status2').show().html('<p>'+responseMsg+'</p>');
				document.getElementById('get_file').value = '';
				$('#upload_link2').html('<a id="upload_link2" href="#">رفع الملف ...</a>').show();

			}else{
				$('#upload_status2').show().html('<p>خطأ لم يتم حذف الملف</p>'+response);
			}
		}
	});
}

$(document).ready(function () {
var inputSiteurl = $("#siteurl"); 
var siteurl = inputSiteurl.attr("value"); 

var inputPathfile = $("#path_file"); 
var path_file = inputPathfile.attr("value"); 

		$('#loader2').hide();
		$('#progress2').hide();
		var myUpload = $('#upload_link2').upload({
		   name: 'file',
		   action: siteurl +'/WEC_submit.php?p=get_file',
		   enctype: 'multipart/form-data',
		   params: {upload:'Upload'},
		   autoSubmit: true,
		   onSubmit: function() {
		   		$('#upload_status2').html('').hide();
				loadingmessage2('جاري رفع الملف...', 'show');
		   },
		   onComplete: function(response) {
		   		loadingmessage2('', 'hide');
				response = unescape(response);
				var response = response.split("|");
				var responseType = response[0];
				var responseMsg = response[1];
				if(responseType=="success"){
					var current_width = response[2];
					var current_height = response[3];
					//display message that the file has been uploaded
					$('#upload_status2').show().html('تم رفع الملف بنجاح');
					//put the image in the appropriate div
					
	                $('#del_file').html('<a href="'+ siteurl +'/'+ path_file +''+responseMsg+'">'+responseMsg+'</a><br /><a href="javascript:deletefile(\''+responseMsg+'\');">حذف الملف</a>');
                	document.getElementById('get_file').value = responseMsg;
                    $('#upload_link2').html('').hide();
					
				}else if(responseType=="error"){
					$('#upload_status2').show().html('<p>'+responseMsg+'</p>');

				}else{
					$('#upload_status2').show().html('<p>خطأ لم يتم رفع الملف</p>'+response);
					
				}
		   }
		});

}); 


// Rating by Ajax
// All part of the Dynamic Star Rating by Jordan Boesch!
// PRE-LOAD IMAGES -----------------------------

if (document.images){
  pic1 = new Image(220,19); 
  pic1.src = "images/loadingAnimation.gif"; 

  pic2 = new Image(25,75); 
  pic2.src = "images/rating_star.gif"; 

  pic3 = new Image(25,75); 
  pic3.src = "images/rating_star_2.gif"; 
  
  pic4 = new Image(16,13); 
  pic4.src = "images/rating_tick.gif";
  
  pic5 = new Image(14,14); 
  pic5.src = "images/rating_warning.gif";
}

// AJAX ----------------------------------------

var xmlHttp

function GetXmlHttpObject(){

var xmlHttp = null;

	try {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp = new XMLHttpRequest();
	  }
	catch (e) {
	  // Internet Explorer
	  try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e){
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	  }
	  
	return xmlHttp;

}

// Calculate the rating
function rate(rating,id,show5,showPerc,showVotes){

	xmlHttp = GetXmlHttpObject()
	
	if(xmlHttp == null){
		alert ("متصحفك لا يدعم AJAX!");
		return;
	  }

	xmlHttp.onreadystatechange = function(){
		
	var loader = document.getElementById('loading_'+id);
	var uldiv = document.getElementById('ul_'+id);
	
		if (xmlHttp.readyState == 4){ 
			
			//loader.style.display = 'none';
			var res = xmlHttp.responseText;
			
			//alert(res);
			
			if(res == 'already_voted'){
				
				loader.style.display = 'block';
				loader.innerHTML = '<div class="voted_twice">تم التقييم سابقأ!</div>';
				
			} else {
				
				loader.style.display = 'block';
				loader.innerHTML = '<div class="voted">شكراً لتقييمك !</div>';

				if(show5 == true){
					var out = document.getElementById('outOfFive_'+id);
					var calculate = res/20;
					out.innerHTML = Math.round(calculate*100)/100; // 3.47;
					//out.innerHTML = Math.round((calculate*2),0)/2; // 3.5;
				} 
				
				if(showPerc == true){
					var perc = document.getElementById('percentage_'+id);
					//var newPerc = Math.round(Math.ceil(res/5))*5;
					var newPerc = res;
					perc.innerHTML = newPerc+'%';
				}
				
				else if(showPerc == false){
					var newPerc = res;
				}
				
				if(showVotes == true){
					var votediv = document.getElementById('showvotes_'+id).firstChild.nodeValue;
					var splitted = votediv.split(' ');
					var newval = parseInt(splitted[0]) + 1;
					if(newval == 1){
						document.getElementById('showvotes_'+id).innerHTML = newval+' Vote';
					} else {
						document.getElementById('showvotes_'+id).innerHTML = newval+' Votes';
					}
				}
				
				var ulRater = document.getElementById('rater_'+id);
				ulRater.className = 'star-rating2';
				
				var all_li = ulRater.getElementsByTagName('li');
				
				// start at 1 because the first li isn't a star
				for(var i=1;i<all_li.length;i++){
					
					all_li[i].getElementsByTagName('a')[0].onclick = 'return false;';
					all_li[i].getElementsByTagName('a')[0].setAttribute('href','#');
					
				}
				
				if(navigator.appName == 'Microsoft Internet Explorer'){
					uldiv.style.setAttribute('width',newPerc+'%'); // IE
				 } else {
					uldiv.setAttribute('style','width:'+newPerc+'%'); // Everyone else
				 }
				
			}
		} else {
			loader.innerHTML = '<img src="images/loadingAnimation.gif" alt="loading" />';	
		}
	
	}
	var url = "../includes/rating_process.php";
	var params = "id="+id+"&rating="+rating;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);

} 
