var GLOBAL_URL = "http://www.dikani.com";

function refreshBody() {
	document.location.reload();
}			

function openwindow(url) {
		window.open(url,"_blank","width=800,height=550,scrollbars=yes,toolbar=no");
	}
function openwindow_chat(url) {
	window.open(url,"_blank","width=620,height=550,toolbar=no,scrollbars=yes");
}
function openupload(url) {
	window.open(url,"_blank","width=400,height=170,scrollbars=no,tootlbar=no,statusbar=no");
}

function NumberOnly(e) { 
	var unicode=e.keyCode? e.keyCode : e.charCode
	return (unicode==8 || unicode==9 || (unicode>45 && unicode<58))
}
function NoChars(e) {
	var unicode=e.keyCode? e.keyCode : e.charCode
	return (unicode==95 || unicode==8 || unicode==9 || unicode==45 || unicode==46 || (unicode>47 && unicode<58) || (unicode>96 && unicode<123))
}

function YourName(e) { 
	var unicode=e.keyCode? e.keyCode : e.charCode
	return (unicode<48 || unicode>57)
}

function CheckUsername(e) { 
	var unicode=e.keyCode? e.keyCode : e.charCode
	return (unicode==8 || unicode==45 || unicode==95 || (unicode>96 && unicode<123) || (unicode>47 && unicode<58))
}

function validate_email(stremail) {
	var objRegExp  = /(.*)@(.*){3,}\.(.*)/i;
	
	return objRegExp.test(stremail);
}

function ReloadCaptcha() {
	var randomnumber=Math.floor(Math.random()*17);
	$("img.captcha").attr({src:GLOBAL_URL+"/index.php/captcha/index/"+randomnumber});
}

function Cancelprice(){
	/*
	var thisobj=$(".price_cancel");
	var text=thisobj.attr("alt");
	thisobj.parent().parent().find("span.elprice").append(text);
	*/
	$("h3.editprice").show();
	$("small.form").remove();return false
}

function Saveprice1(the_id){
	var thisobj=$(".price_save");
	var price=$(".inplace_price");
	var fils=$(".inplace_fils");
	var element_id=the_id;
	if(parseInt(fils.val())!=0)
		var pricedb=price.val()+"."+fils.val();
	else var pricedb=price.val();
	
	$.ajax({type:"POST",url:GLOBAL_URL+"/index.php/ajax/edit/price/",data:"update_value="+pricedb+"&element_id="+the_id,async:false,
		success:function(msg){
			thisobj.parent().parent().find("span.elprice").append(msg);
			$("small.form").remove();
		}
	});
	$("h3.editprice").show();
	$("small.kd").show();
	return false
}

function Saveprice(the_id){
	var thisobj=$(".price_save");
	var price=$(".inplace_price");
	var fils=$(".inplace_fils");
	var p2 = $(".inplace_price_old").val();
	var f2 = $(".inplace_fils_old").val();
	
	var theoldprice = parseFloat(p2+"."+f2);
	var thenewprice = parseFloat(price.val() + "." + fils.val());
	
	if(theoldprice <= thenewprice) { 
		alert("The top price should more than the bottom price");
		p2 = 0; f2 = 0;
	}
	else {
		thisobj.parent().parent("h3.editprice").css({color:"#FFF"});
	}
	
	var element_id=the_id;
	if(parseInt(fils.val())!=0) var pricedb=price.val()+"."+fils.val();
	else var pricedb=price.val();
	
	
	$.ajax({
		type:"POST",
		url:GLOBAL_URL+"/index.php/ajax/edit/price/",
		data:"update_value="+pricedb+"&element_id="+the_id+"&p2="+p2+"&f2="+f2,
		success:function(arr){
			var msg = eval(arr);
			var oldprice = msg[1];
			var newprice = msg[0];
			
			var thenewprice = thisobj.parent().prev().find(".here-is-the-price");
			var theoldprice = thisobj.parent().prev().prev().find(".here-is-the-price");
			thisobj.parent().prev().show();
			
			thenewprice.empty().append(newprice);
			theoldprice.empty().append(oldprice);
			
			/*
			var msg = eval(arr);
			var oldprice = msg[1];
			var newprice = msg[0];
			
			var kd = thisobj.parent().parent().siblings("h3.oldprice").find(".curr-kd").text();
			
			if(kd.trim() == "KD") var dataprice = "<span class='curr-kd'>"+kd+"</span>&nbsp;"+oldprice;
			else var dataprice = oldprice+"&nbsp;<span class='curr-kd'>"+kd+"</span>&nbsp;";
			if(parseFloat(oldprice) > 0) {
			thisobj.parent().parent().siblings("h3.oldprice").show();
			}
			else {
			thisobj.parent().parent().siblings("h3.oldprice").hide();
			}
			thisobj.parent().parent().siblings("h3.oldprice").children("span").empty().append(dataprice);
			
			thisobj.parent().parent().find("span.elprice").append(newprice);
			
			
			*/
			$("small.form").remove();
		}
	});
	$("small.kd").show();
	return false;
}


// make sidebar and content has the same height
$(document).ready(function() {
	$("ul.thecategory").hide();
	$("h3.title-cats a").toggle(function() {
		$("ul.thecategory").slideDown("slow");
		return false;
	},function() {
		$("ul.thecategory").slideUp("slow");
		return false;
	});
	
	
	// paging 
	if($("ul.paging").length > 0) {
		var thewidth = 0;
		$("ul.paging li").each(function() {
			thewidth = thewidth + $(this).width();
		});
		
		$("ul.paging").attr("style","width:"+thewidth+"px !important");
	}
	
	
	/*
	var sidebar = $("#sidebar").height();
	var content = $("#thecontent").height();
	
	var total = sidebar - content;
	
	if(total > 0) {
		$("#thecontent").css({paddingBottom:total+10+"px"});
	}
	*/
	
	// link to open in new window
	$("a.window").click(function() {
		var href = $(this).attr("href");
		openwindow(href);
		return false;
	});
	
	/*
	// product price
	$("h3.oldprice").each(function() {
		var number = $(this).find("span").text();
		if(!number.match(/([0-9]+\.[0-9]+)/)) {
			$(this).siblings("h3.newprice").css({color:"#000"});
			$(this).hide();
		}
		else {
			data = number.match(/([0-9]+\.[0-9]+)/);
			if(parseFloat(data[1]) <= 0) {
				$(this).hide();
				$(this).siblings("h3.newprice").css({color:"#000"});
			}
		}
	});
	*/
	
	$("input.cmdregister").show();
	
	// make sure the footer is on the bottom
	$("#content").css({background:"#FFFFFF"});
	
	// loader
	var hashurl = location.hash;
	if(hashurl.indexOf("ajax") != -1) {
		theurl = hashurl.replace("#","");
		
		
		
		$("#content").empty().append("<div style=\'text-align:center;padding:10%;\'><img src=\'http://www.dikani.com/images/ajax-loader.gif\' alt=\'\' /></div>");
		
		$.ajax({
			url: GLOBAL_URL+"/index.php/"+theurl,
			success: function(contents) {
				$("#content").empty().append("<br />"+contents);
			}
		});
		
	}
	
	/*
	if($("#tabs").length > 0) {
		if($.browser.msie) {
			content_height = $("#content").height();
			sidebar_height = $("#sidebar").height();
			
			if(content_height > sidebar_height) {
				$("#thecontent").css("height",content_height+"px");
			}
			
			$("#tabs li.product-details a, #tabs li.product-desc a").livequery("click",function() {
				content_height = $("#content").height();
				alert(content_height);
				$("#thecontent").css("height",content_height+"px");
				$("#footer").css({position:"absolute",bottom:"0px"});
				
			});
			
		}
	}
	*/
	
	$(".mychat a").click(function() {
		var href = $(this).attr("href");
		window.open(href,"_chat","width=840,height=630,toolbar=no");
		return false;
	});
	/*
	$("a.subscribe").click(function() {
		var href = $(this).attr("href");
		$.ajax({
			url: href,
			success: function(t) {
				alert(t);
				if(t) {
					$(this).after("<a class='remove-subscribe' href='"+GLOBAL_URL+"/index.php/subscribe/remove'>remove me</a>");
					$(this).remove();
				}
			}
		});
		return false;
	});
	
	$("a.remove-subscribe").click(function() {
		var href = $(this).attr("href");
		
		$.ajax({
			url: href,
			success: function(t) {
				if(t) {
					$(this).after("<a class='subscribe' href='"+GLOBAL_URL+"/index.php/subscribe/me'>subscribe</a>");
					$(this).remove();
				}
			}
		});
		return false;
	});
	*/
});


function nl2br(text) {
	original = text;
	text = escape(text);
	
	var re_nlchar = 0;
	if(text.indexOf('%0D%0A') > -1){
		re_nlchar = /%0D%0A/g;
	}
	else if(text.indexOf('%0A') > -1){
		re_nlchar = /%0A/g;
	}
	else if(text.indexOf('%0D') > -1){
		re_nlchar = /%0D/g;
	}
	
	if(re_nlchar != 0)
	return unescape(text.replace(re_nlchar,'<br />'));
	else return original;
}
function inl2br(text) {
	
	original = text;
	
	text = escape(text);
	
	var re_nlchar = 0;
	if(text.indexOf('%0D%0A') > -1){
		re_nlchar = /%0D%0A/g;
	}
	else if(text.indexOf('%0A') > -1){
		re_nlchar = /%0A/g;
	}
	else if(text.indexOf('%0D') > -1){
		re_nlchar = /%0D/g;
	}
	
	if(re_nlchar != 0)
	return unescape(text.replace(re_nlchar,'%0A'));
	else return original;
}

function endofpage() {
	
	if($(".endpage").length > 0) $(".endpage").remove();
	
	var show_data = parseInt($(".pagelimit").text());
	var text_show = $(".pagelimit_text").text();
	var i = 1;
	var j = 1;
	if($("#sortdiv li").length > show_data) {
		$("#sortdiv li").each(function() {
			if(i!=1 && i%show_data == 0) {
				$(this).append("<div class='endpage'>"+text_show.replace("0",j)+"</div>");
				j++;
			}
			i++;
		});
	}
	
	
}

function balanceContent() {
	var content = $("#content");
	var rightcol = $("#rightcol");
	var footer = $("#footer");
	/*	
	if(content.height() < rightcol.height()) {
		var total = content.height() + (rightcol.heigth() - content.height());
		
		content.css(height,total+"px");
	}
	*/
	content.css({background:"#FFF"});
	//footer.css({position:"absolute",bottom:"0px"});
}

function changing_category(obj,add) {
	if($(".cat_imgloading").length < 1) {
		obj.after("<img src='"+GLOBAL_URL+"/images/ajax_loader.gif' alt='loading' class='cat_imgloading' />");
	}
	else {
		$(".cat_imgloading").remove();
		obj.after("<img src='"+GLOBAL_URL+"/images/ajax_loader.gif' alt='loading' class='cat_imgloading' />");
	}
	
	jQuery.blockUI({ message: null,overlayCSS: { backgroundColor: '#FFFFFF' } }); 
	
	$().ajaxStop($.unblockUI);
	$(".cat_imgloading").remove();
}


function AddUserOption() {
	$(".thesubdomain").append("http://"+$("input[@name=txtuser]").val()+".dikani.com");
}
function RemoveUserOption() {
	$(".thesubdomain").empty();
}

function arrsearch(arr, find) {
	for(i=0;i<arr.length;i++) {
		if(arr[i] == find) return true;
	}
	return false;
}