// JavaScript Document
String.prototype.Trim = function(){
    return this.replace(/(^\s*)|(\s*$)/g, "");
}
String.prototype.LTrim = function(){
    return this.replace(/(^\s*)/g, "");
}
String.prototype.Rtrim = function(){
    return this.replace(/(\s*$)/g, "");
}

function copyclipboard(inputurl){
	document.getElementById(inputurl).select();
	try{clipboardData.setData('Text',document.getElementById(inputurl).value)}catch(e){}
}

function inArray(arrayToSearch, stringToSearch){
    for (var s=0; s<arrayToSearch.length; s++){
        var thisEntry = arrayToSearch[s].toString();
        if (thisEntry == stringToSearch)
            return true;
    }
    return false;
}

function newAjax(){
	var xmlHttp;
	if(window.ActiveXObject){
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}else if(window.XMLHttpRequest){
		xmlHttp = new XMLHttpRequest();
	}else{
		alert("Ajax error");
		xmlHttp = null;
	}
	return xmlHttp;
}

function specialStrReplace(str){
	str = str.Trim().toLowerCase();
	var res_str = "";
	var re = /^[A-Za-z0-9]+$/
	
	for(var i=0;i<str.length;i++){
		var sstr = str.substr(i, 1);
		if(re.test(sstr))
			res_str += sstr;
		else if(i>0 && re.test(str.substr(i-1, 1)))
			res_str += "-";
	}		
	if(!re.test(res_str.substr(res_str.length-1)))
		res_str = res_str.substr(0, res_str.length-1);		

	return res_str;
}
	
	
function Search(form){
	var str = form.str.value;
	str = specialStrReplace(str);
	form.action = '/search/'+str+'/';
	form.submit();
}

function userRating(eid,vtype,link_id){
	var xmlHttp = newAjax();
	var value = document.getElementById("rating").value;
	var s = parseInt(Math.random()*10000000).toString(10);
	var url = "/Ajax/userrating.php?vtype="+vtype+"&link_id="+link_id+"&value="+value+"&random="+s;
	xmlHttp.open("GET", url, true);
	
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState == 4){
			var result = xmlHttp.responseText;

			if(result.indexOf("::") != -1){
				var res = result.split("::");
				var ratestr = res[0]
				var ratecount = res[1];
				var rateavg = res[2];
				var rateimg = res[3];
				
				document.getElementById("rateimg_"+link_id).src = "/img/rating/blue/rating"+rateimg+".png";
				document.getElementById("ratecount_"+link_id).innerHTML = ratecount;
				alert('Thanks for rating. You succesfully rated it!');

			}else if(result == 'Error'){
				alert('You have already rated it!');
			}
		}
	}
	
	xmlHttp.send(null);
}

if (top.location != location){ top.location.href = self.location; }

var domType = '';
if (document.all) {
	domType = "ie4";
} else if (document.getElementById) {
	domType = "std";
} else if (document.layers) {
	domType = "ns4";
}

var objects=new Array();
function $(idname, forcefetch) {
	if (forcefetch || typeof(objects[idname]) == "undefined") {
		switch (domType) {
			case "std": {
				objects[idname] = document.getElementById(idname);
			}
			break;

			case "ie4": {
				objects[idname] = document.all[idname];
			}
			break;

			case "ns4": {
				objects[idname] = document.layers[idname];
			}
			break;
		}
	}
	return objects[idname];
}

function externallinks(){
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "_blank"){
			anchor.target = "_blank";
		}
	}

var iearVersion = navigator.appVersion.split("MSIE");
var ieversion = parseFloat(iearVersion[1]);
if ((ieversion >= 5.5) && (ieversion < 7) && (document.body.filters)){
   for(var i=0; i<document.images.length; i++){
      var img = document.images[i];
      var imgName = img.src.toUpperCase();
      if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
		 var imgID = (img.id) ? "id='" + img.id + "' " : "";
		 var imgClass = (img.className) ? "class='" + img.className + "' " : "";
		 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
		 var imgStyle = "display:inline-block;" + img.style.cssText;
		 if (img.align == "left") imgStyle = "float:left;" + imgStyle;
		 if (img.align == "right") imgStyle = "float:right;" + imgStyle;
		 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
		 var strNewHTML = "<span " + imgID + imgClass + imgTitle
		 + " style=\"margin:2px 0 2px 0;" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
		 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>";
		 img.outerHTML = strNewHTML;
		 i = i-1;
	      }
	   }
	}
}
window.onload = externallinks;

function startmarquee(lh,speed,delay,index){
	var t;
	var p=false;
	var o=document.getElementById("marqueebox"+index);
	o.innerHTML+=o.innerHTML;
	o.onmouseover=function(){p=true}
	o.onmouseout=function(){p=false}
	o.scrollTop = 0;
	function start(){
		t=setInterval(scrolling,speed);
		if(!p) o.scrollTop += 2;
	}
	function scrolling(){
		if(o.scrollTop%lh!=0){
			o.scrollTop += 2;
			if(o.scrollTop>=o.scrollHeight/2) o.scrollTop = 0;
		}else{
			clearInterval(t);
			setTimeout(start,delay);
		}
	}
	setTimeout(start,delay);
}

function addfavorite(url,name){
	if (document.all){
		window.external.addFavorite(url,name);
	}else if(window.sidebar){
		window.sidebar.addPanel(name,url, "");
	}
}

function checkCookie(){
    var cookieEnabled=(navigator.cookieEnabled)? true : false;
    if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){
        document.cookie="testcookie";
        cookieEnabled=(document.cookie=="testcookie")? true : falsedocument.cookie="";
    }
    if(cookieEnabled){
        return true;
    }else{
        return false;
    }
}

function addCookie(name,value,expireHours){
    var cookieString=name+"="+escape(value);
    if(expireHours>0){
        var date=new Date();
        date.setTime(date.getTime+expireHours*3600*1000);
        cookieString=cookieString+"; expire="+date.toGMTString();
    }
    document.cookie=cookieString;
}

function getCookie(name){
    var strCookie=document.cookie;
    var arrCookie=strCookie.split("; ");
    for(var i=0;i<arrCookie.length;i++){
        var arr=arrCookie[i].split("=");
        if(arr[0]==name){
            return unescape(arr[1]);
        }
    }
    return false;
}

function deleteCookie(name){
    var date=new Date();
    date.setTime(date.getTime()-10000);
    document.cookie=name+"=; expire="+date.toGMTString();
}

function change_tab(target,num) {			
	for(i=1; i<num+1; i++) {
		if(target == i) {
			document.getElementById('tabs_' + i).className = 'current';
			document.getElementById('container_' + i).style.display = 'block';
		} else {
			document.getElementById('tabs_' + i).className = '';
			document.getElementById('container_' + i).style.display = 'none';

		}
	}
}