var par, act, di;


function ajax(action, div, parameters) {
    var xmlHttpReq = false;
    var self = this;
	
	par = parameters;
	act = action;
	di = div;
	

	if(action == 'comment'){
		script = 'http://www.anivide.com/comment.php';
	}else if (action == "share"){
		script = 'http://www.anivide.com/share.php';
	}else if (action == "video"){
		script = 'http://www.anivide.com/vidplayer.php';
	}else if (action == "manga"){
		script = 'http://www.anivide.com/viewmanga.php'; 
	}else if (action == "misc"){
		
		var cookies = ReadCookie('vote');

		if(cookies == 'voted'){
			alert('Please wait for 10 seconds before voting again');
			return 0;
			
		}else{
			script = 'http://www.anivide.com/misc.php';
			Set_Cookie('vote', 'voted', 10);
		}
	}

    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', script, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

    self.xmlHttpReq.onreadystatechange = function() {
		
	//0 = uninitialized 1 = loading 2 = loaded 3 = interactive 4 = complete
	switch(self.xmlHttpReq.readyState)
	{
	case 0:
	  loadp = ' 0% ';
	  break;    
	case 1:
	  loadp = ' 25% ';
	  break;
	case 2:
	  loadp = ' 55% ';
	  break;
	case 3:
	  loadp = ' 75% ';
	  break;
	case 4:
	  loadp = ' 100% ';
	  break;
	}
	document.getElementById(div).innerHTML = '<center><br><img src="http://www.anivide.com/images/loading.gif"><br><br><b>Loading... [ ' + loadp + ' ] </b><br></center><br><br>' ;

	 if (self.xmlHttpReq.readyState == 4) {
		updatepage(self.xmlHttpReq.responseText,div);
 
	 }
	 
	 if (action == "manga" || action == "video"){
		 if (self.xmlHttpReq.readyState == 1) {
			setTimeout('dorefresh()',3000);
	 
		 }
	 }
	   

        
    }
	

    self.xmlHttpReq.send(parameters);

	
}



function updatepage(str, div){
	
	if (self.xmlHttpReq.readyState == 4) {
		
		document.getElementById(div).innerHTML = str;
	
	}
	
}


function dorefresh(){

		if (self.xmlHttpReq.readyState == 1){
			window.location.reload(true);
		}

}

function friend_request(){
	var ask_friend = confirm("Do you really want to add this person as a friend?"); 
	if (ask_friend == true) 
	{ 
		window.location="http://www.anivide.com/friends.html?action=add"; 
	}
}


function delete_friend(friend){
	var ask_friend = confirm("Do you really want to delete this person from your friends?"); 
	if (ask_friend == true) 
	{ 
		window.location="http://www.anivide.com/friends.html?action=delete&uid=" + friend; 
	}
}

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct 
expires time, the current script below will set 
it for x number of days, to make it for hours, 
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}

function ReadCookie(cookieName) {
 var theCookie=""+document.cookie;
 var ind=theCookie.indexOf(cookieName);
 if (ind==-1 || cookieName=="") return ""; 
 var ind1=theCookie.indexOf(';',ind);
 if (ind1==-1) ind1=theCookie.length; 
 return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}


function resizeImg(whichId, maxW) {
var pix=document.getElementById(whichId).getElementsByTagName('img');
  for (i=0; i<pix.length; i++) {
    w=pix[i].width;
    h=pix[i].height;
    if (w > maxW) {
      f=1-((w - maxW) / w);
      pix[i].width=w * f;
      pix[i].height=h * f;
	  
	  im = pix[i]; 
	  
	  eval("pop" + String(i) + " = new Function(\"pop = window.open('" + im.src + " ','fullscale','width="+w+",height="+h+",scrollbars=0,resizable=1'); pop.focus();\")"); 
            eval("im.onclick = pop" + String(i) + ";"); 
            if (document.all) im.style.cursor = 'hand'; 
            if (!document.all) im.style.cursor = 'pointer'; 
            im.title = 'Click To View Full Size ';
	  
    }
  }
}

function stopsound() {
	
	if( document.getElementById('cpxads') ) {
		document.getElementById('cpxads').innerHTML = '';
	}
	
}