function base64_encode( data ) {
	// http://kevin.vanzonneveld.net
	// +   original by: Tyler Akins (http://rumkin.com)
	// +   improved by: Bayron Guevara
	// +   improved by: Thunder.m
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// -    depends on: utf8_encode
	// *     example 1: base64_encode('Kevin van Zonneveld');
	// *     returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA=='

	// mozilla has this native
	// - but breaks in 2.0.0.12!
	//if (typeof window['atob'] == 'function') {
	//    return atob(data);
	//}
	//data = urlencode(data);

	var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, enc="", tmp_arr = [];
	//	data = utf8_encode(data);

	do { // pack three octets into four hexets
		o1 = data.charCodeAt(i++);
		o2 = data.charCodeAt(i++);
		o3 = data.charCodeAt(i++);

		bits = o1<<16 | o2<<8 | o3;

		h1 = bits>>18 & 0x3f;
		h2 = bits>>12 & 0x3f;
		h3 = bits>>6 & 0x3f;
		h4 = bits & 0x3f;

		// use hexets to index into b64, and append result to encoded string
		tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
	} while (i < data.length);

	enc = tmp_arr.join('');

	switch( data.length % 3 ){
		case 1:
		enc = enc.slice(0, -2) + '==';
		break;
		case 2:
		enc = enc.slice(0, -1) + '=';
		break;
	}

	enc =  enc.replace("/"  , "|");


	return enc;
}
function utf8_encode ( str_data ) {


	str_data = str_data.replace(/\r\n/g,"\n");
	var tmp_arr = [], ac = 0;

	for (var n = 0; n < str_data.length; n++) {
		var c = str_data.charCodeAt(n);
		if (c < 128) {
			tmp_arr[ac++] = String.fromCharCode(c);
		} else if((c > 127) && (c < 2048)) {
			tmp_arr[ac++] = String.fromCharCode((c >> 6) | 192);
			tmp_arr[ac++] = String.fromCharCode((c & 63) | 128);
		} else {
			tmp_arr[ac++] = String.fromCharCode((c >> 12) | 224);
			tmp_arr[ac++] = String.fromCharCode(((c >> 6) & 63) | 128);
			tmp_arr[ac++] = String.fromCharCode((c & 63) | 128);
		}
	}

	return tmp_arr.join('');
}

function urlencode( str ) {


	var ret = str;

	ret = ret.toString();
	ret = encodeURIComponent(ret);
	ret = ret.replace(/%20/g, '+');

	return ret;
}

function show(type){
		if(type == 'tell') {
		$('#tForm').show('slow');
		$('#cForm').hide('slow');
		}
		else {
		$('#cForm').show('slow');
		$('#tForm').hide('slow');
		}
		
}

function hide(type){
		if(type == 'tell')
		$('#tForm').hide('slow');
		else
		$('#cForm').hide('slow');
}

function addComment(){

		var cType	= $('#cType').attr('value');
		var Id		= $('#cId').attr('value');
		var Comment	= $('#body').attr('value');
		var email	= $('#email').attr('value');
		var title	= $('#title').attr('value');
		var country	= $('#country').attr('value');
		var name	= $('#author').attr('value');

		if(jQuery.trim(Comment) == '' || jQuery.trim(email) =='' || jQuery.trim(title) =='' 
				|| jQuery.trim(country) =='' || jQuery.trim(name) ==''){
			$("#ErrorDiv").text('الرجاء ملئ الحقول الفارغة ');
			$('#cForm').focus();
			return false;
		}
		
		if(!testemail($('#email')[0])){
		alert("الرجاء كتابة بريد الكتروني صحيح");  
		$('#email').focus();
		return false;
		}
		jQuery.ajax({
			async: true,
			type: "POST",
			data: "type="+cType+"&id="+Id+"&txt="+Comment+"&author="+encodeURIComponent(name)+"&email="+email+"&title="+title+"&country="+country,
			url: baseLink +"addComment.php",
			success: function(data){
					hide('comment');
					//document.location = document.location;
					$("#commentsList").html(data);
					$("#ErrorDiv").text('');
					
			},
			beforeSend: function(data){
			},
			error: function(){
				return true;
			}
		}
		);
}

function tellFriend(){

		var fname	= $('#fname').attr('value');
		var tname	= $('#tname').attr('value');
		var temail	= $('#temail').attr('value');
		var femail	= $('#femail').attr('value');
		var link	= $('#link').attr('value');

		if(jQuery.trim(fname) == '' || jQuery.trim(tname) ==''){
			$("#ErrorDiv2").text('الرجاء ملئ الحقول الفارغة ');
			$('#cForm').focus();
			return false;
		}
		
		if(!testemail($('#temail')[0])){
		alert("الرجاء كتابة بريد الكتروني صحيح");
		$('#temail').focus();
		return false;
		}
		
		if(!testemail($('#femail')[0])){
		alert("الرجاء كتابة بريد الكتروني صحيح");
		$('#femail').focus();
		return false;
		}
	
		jQuery.ajax({
			async: true,
			type: "POST",   
			data: "fname="+base64_encode(encodeURI(fname))+"&tname="+base64_encode(encodeURI(tname))+"&temail="+temail+"&link="+link,
			url: baseLink +"tellFriend.php",
			success: function(data){
					$("#ErrorDiv2").text('تمت العملية بنجاح');
					hide('tell');
					$('form').clearForm();
			},
			beforeSend: function(data){
			},
			error: function(){
				return true;
			}
		}
		);
}


function printPage(url,width,height){
 window.open(url,'popup','scrollbars=yes,status=no,width='+width+',height='+height);
}

$.fn.clearForm = function() {
  return this.each(function() {
	var type = this.type, tag = this.tagName.toLowerCase();
	if (tag == 'form')
	  return $(':input',this).clearForm();
	if (type == 'text' || type == 'password' || tag == 'textarea')
	  this.value = '';
	else if (type == 'checkbox' || type == 'radio')
	  this.checked = false;
	else if (tag == 'select')
	  this.selectedIndex = -1;
  });
};


function resizeText(inc){
	var size = $(".addition p").css('font-size');
	var currentFontSizeNum = parseFloat(size, 10);
    var newFontSize = currentFontSizeNum + inc;
    if(newFontSize == 8 || newFontSize == 22){
    	return;
    }
	$(".addition p").css('font-size',newFontSize+"px");
}
