var sUrl = 'http://www.kovideo.net/AJAX/';

function AjaxRequest() {
	var req = new Object();
	
	// Member variables
	// ----------------
	req.timeout = null;
	req.generateUniqueUrl = true;
	req.url = window.location.href;
	req.method = "GET";
	req.async = true;
	req.username = null;
	req.password = null;
	req.parameters = new Object();
	req.requestIndex = AjaxRequest.numAjaxRequests++;
	req.responseReceived = false;
	req.groupName = null;
	req.queryString = "";
	
	req.responseText = null;
	req.responseXML = null;
	req.status = null;
	req.statusText = null;

	// Event handlers
	// --------------
	req.onTimeout = null;
	req.onLoading = null;
	req.onLoaded = null;
	req.onInteractive = null;
	req.onComplete = null;
	req.onSuccess = null;
	req.onError = null;
	req.onGroupBegin = null;
	req.onGroupEnd = null;
	req.aborted = false;
	
	// Get the XMLHttpRequest object itself
	req.xmlHttpRequest = AjaxRequest.getXmlHttpRequest();
	if (req.xmlHttpRequest==null) { return null; }
	
	// Attach the event handlers for the XMLHttpRequest object
	// -------------------------------------------------------
	req.xmlHttpRequest.onreadystatechange = 
	function() {
		if (req==null || req.xmlHttpRequest==null) { return; }
		if (req.xmlHttpRequest.readyState==1) { req.onLoadingInternal(req); }
		if (req.xmlHttpRequest.readyState==2) { req.onLoadedInternal(req); }
		if (req.xmlHttpRequest.readyState==3) { req.onInteractiveInternal(req); }
		if (req.xmlHttpRequest.readyState==4) { req.onCompleteInternal(req); }
	}
	
	// Internal event handlers that fire, and in turn fire the user event handlers
	// ---------------------------------------------------------------------------
	// Flags to keep track if each event has been handled, in case of multiple calls
	req.onLoadingInternalHandled = false;
	req.onLoadedInternalHandled = false;Handled = false;
	req.onInteractiveInternalHandled = false;
	req.onCompleteInternalHandled = false;
	req.onLoadingInternal = 
		function() {
			if (req.onLoadingInternalHandled) { return; }
			AjaxRequest.numActiveAjaxRequests++;
			if (AjaxRequest.numActiveAjaxRequests==1 && typeof(window['AjaxRequestBegin'])=="function") {
				AjaxRequestBegin();
			}
			if (req.groupName!=null) {
				if (typeof(AjaxRequest.numActiveAjaxGroupRequests[req.groupName])=="undefined") {
					AjaxRequest.numActiveAjaxGroupRequests[req.groupName] = 0;
				}
				AjaxRequest.numActiveAjaxGroupRequests[req.groupName]++;
				if (AjaxRequest.numActiveAjaxGroupRequests[req.groupName]==1 && typeof(req.onGroupBegin)=="function") {
					req.onGroupBegin(req.groupName);
				}
			}
			if (typeof(req.onLoading)=="function") {
				req.onLoading(req);
			}
			req.onLoadingInternalHandled = true;
		}
	req.onLoadedInternal = 
		function() {
			if (req.onLoadedInternalHandled) { return; }
			if (typeof(req.onLoaded)=="function") {
				req.onLoaded(req);
			}
			req.onLoadedInternalHandled = true;
		}
	req.onInteractiveInternal = 
		function() {
			if (req.onInteractiveInternalHandled) { return; }
			if (typeof(req.onInteractive)=="function") {
				req.onInteractive(req);
			}
			req.onInteractiveInternalHandled = true;
		}
	req.onCompleteInternal = 
		function() {
			if (req.onCompleteInternalHandled || req.aborted) { return; }
			req.onCompleteInternalHandled = true;
			AjaxRequest.numActiveAjaxRequests--;
			if (AjaxRequest.numActiveAjaxRequests==0 && typeof(window['AjaxRequestEnd'])=="function") {
				AjaxRequestEnd(req.groupName);
			}
			if (req.groupName!=null) {
				AjaxRequest.numActiveAjaxGroupRequests[req.groupName]--;
				if (AjaxRequest.numActiveAjaxGroupRequests[req.groupName]==0 && typeof(req.onGroupEnd)=="function") {
					req.onGroupEnd(req.groupName);
				}
			}
			req.responseReceived = true;
			req.status = req.xmlHttpRequest.status;
			req.statusText = req.xmlHttpRequest.statusText;
			req.responseText = req.xmlHttpRequest.responseText;
			req.responseXML = req.xmlHttpRequest.responseXML;
			if (typeof(req.onComplete)=="function") {
				req.onComplete(req);
			}
			if (req.xmlHttpRequest.status==200 && typeof(req.onSuccess)=="function") {
				req.onSuccess(req);
			}
			else if (typeof(req.onError)=="function") {
				req.onError(req);
			}

			// Clean up so IE doesn't leak memory
			delete req.xmlHttpRequest['onreadystatechange'];
			req.xmlHttpRequest = null;
		}
	req.onTimeoutInternal = 
		function() {
			if (req!=null && req.xmlHttpRequest!=null && !req.onCompleteInternalHandled) {
				req.aborted = true;
				req.xmlHttpRequest.abort();
				AjaxRequest.numActiveAjaxRequests--;
				if (AjaxRequest.numActiveAjaxRequests==0 && typeof(window['AjaxRequestEnd'])=="function") {
					AjaxRequestEnd(req.groupName);
				}
				if (req.groupName!=null) {
					AjaxRequest.numActiveAjaxGroupRequests[req.groupName]--;
					if (AjaxRequest.numActiveAjaxGroupRequests[req.groupName]==0 && typeof(req.onGroupEnd)=="function") {
						req.onGroupEnd(req.groupName);
					}
				}
				if (typeof(req.onTimeout)=="function") {
					req.onTimeout(req);
				}
			// Opera won't fire onreadystatechange after abort, but other browsers do. 
			// So we can't rely on the onreadystate function getting called. Clean up here!
			delete req.xmlHttpRequest['onreadystatechange'];
			req.xmlHttpRequest = null;
			}
		}

	// Instance methods
	// ----------------
	req.process = 
		function() {
			if (req.xmlHttpRequest!=null) {
				// Some logic to get the real request URL
				if (req.generateUniqueUrl && req.method=="GET") {
					req.parameters["AjaxRequestUniqueId"] = new Date().getTime() + "" + req.requestIndex;
				}
				var content = null; // For POST requests, to hold query string
				for (var i in req.parameters) {
					if (req.queryString.length>0) { req.queryString += "&"; }
					req.queryString += encodeURIComponent(i) + "=" + encodeURIComponent(req.parameters[i]);
				}
				if (req.method=="GET") {
					if (req.queryString.length>0) {
						req.url += ((req.url.indexOf("?")>-1)?"&":"?") + req.queryString;
					}
				}
				req.xmlHttpRequest.open(req.method,req.url,req.async,req.username,req.password);
				if (req.method=="POST") {
					if (typeof(req.xmlHttpRequest.setRequestHeader)!="undefined") {
						req.xmlHttpRequest.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
					}
					content = req.queryString;
				}
				if (req.timeout>0) {
					setTimeout(req.onTimeoutInternal,req.timeout);
				}
				req.xmlHttpRequest.send(content);
			}
		}

	req.handleArguments = 
		function(args) {
			for (var i in args) {
				// If the AjaxRequest object doesn't have a property which was passed, treat it as a url parameter
				if (typeof(req[i])=="undefined") {
					req.parameters[i] = args[i];
				}
				else {
					req[i] = args[i];
				}
			}
		}


	return req;
}

// Static methods of the AjaxRequest class
// ---------------------------------------
AjaxRequest.getXmlHttpRequest = function() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		// Based on http://jibbering.com/2002/4/httprequest.html
		/*@cc_on @*/
		/*@if (@_jscript_version >= 5)
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				return null;
			}
		}
		@end @*/
	}
	else {
		return null;
	}
}
	
AjaxRequest.isActive = function() {
	return (AjaxRequest.numActiveAjaxRequests>0);
}

AjaxRequest.get = function(args) {
	AjaxRequest.doRequest("GET",args);
}

AjaxRequest.post = function(args) {
	AjaxRequest.doRequest("POST",args);
}

AjaxRequest.doRequest = function(method,args) {
	if (typeof(args)!="undefined" && args!=null) {
		var myRequest = new AjaxRequest();
		myRequest.method = method;
		myRequest.handleArguments(args);
		myRequest.process();
	}
}	

AjaxRequest.submit = function(theform,args) {
	var myRequest = new AjaxRequest();
	if (myRequest==null) { return false; }
	var serializedForm = AjaxRequest.serializeForm(theform);
	myRequest.method = theform.method.toUpperCase();
	myRequest.url = theform.action;
	myRequest.handleArguments(args);
	myRequest.queryString = serializedForm;
	myRequest.process();
	return true;
}



// Static members
AjaxRequest.numActiveAjaxRequests = 0;
AjaxRequest.numActiveAjaxGroupRequests = new Object();
AjaxRequest.numAjaxRequests = 0;


function CheckUsername(user){
	if (user.length>5){
		document.getElementById( "userCheck" ).innerHTML = 'Checking Username...';
		AjaxRequest.post({
			'url': sUrl + 'CheckUsername/' + user + '/'
			,'onSuccess': function(req){	raspunsXML = req.responseText;
										document.getElementById( 'userCheck' ).innerHTML = raspunsXML;}
		});
	}else{
			document.getElementById( "userCheck" ).innerHTML = '<span style="color:red;">Username must have minimum 6 characters</span>';
		};
}


function SubmitComment(id,nume,comentariu){
    document.getElementById( "newcomment" ).innerHTML = "Submitting Comment. Please wait...";
	
	comentariu = comentariu.replace(/(\r\n|\r|\n)/g, '\n');
	comentariu = comentariu.replace(/\n/g, '|||');

	AjaxRequest.post({
		'url': sUrl + 'videocomment/' + id + '/' + escape( nume ) + '/' + escape( comentariu ) + '/' + id + '/'
		,'onSuccess': function(req){	raspunsXML = req.responseText;
									raspunsXML = raspunsXML.replace(/\|\|\|/g, '<br />');
									document.getElementById( 'newcomment' ).innerHTML = raspunsXML;
									document.getElementById( 'addcommentspan' ).innerHTML = "";}
	});
}


function SubmitRating(id,ratingnote){
    document.getElementById( "rateVideoMsg" ).innerHTML = "Submitting Rating. Please wait...";
	AjaxRequest.post({
		'url': sUrl + 'ratingsubmit/' + id + '/' + ratingnote + '/'
		,'onSuccess': function(req){	raspunsXML = req.responseText;
									document.getElementById( 'rateVideoMsg' ).innerHTML = raspunsXML;}
	});
}

function SubmitSexyRating(id,ratingnote){
    document.getElementById( 'voteButton' + id ).innerHTML = "Submitting Rating. Please wait...";
	AjaxRequest.post({
		'url': sUrl + 'sexyvideosubmit/' + id + '/' + ratingnote + '/'
		,'onSuccess': function(req){	raspunsXML = req.responseText;
									document.getElementById( 'voteButton' + id ).innerHTML = raspunsXML;
										if (raspunsXML == 'ok'){document.location='http://www.kovideo.net/top20sexyvideos.html';}
									}
	});
}



function ClickReport(dump){
 	document.getElementById( "reportVideo" ).innerHTML = "loading...";
	AjaxRequest.post({
		'url': sUrl + 'report/' + dump + '/'
		,'onSuccess': function(req){	raspunsXML = req.responseText;
									document.getElementById( 'reportVideo' ).innerHTML = raspunsXML;}
	});
}


function GetVideoDiggMySpace(ele, firstClass, secondClass) {
    var srcElement = document.getElementById(ele);
    if(srcElement != null)
		{
			if(srcElement.className == firstClass) { srcElement.className = secondClass; }
											  else { srcElement.className = firstClass; }
			return false;
		}
}


function DoVidTellAfriend(dump){
 	document.getElementById( "reportVideo" ).innerHTML = "loading...";
	AjaxRequest.post({
		'url': sUrl + 'tellafriendMusicVideo/' + dump + '/'
		,'onSuccess': function(req){	raspunsXML = req.responseText;
									document.getElementById( 'reportVideo' ).innerHTML = raspunsXML;}
	});
}

function DoBeautyTellAfriend(dump){
 	document.getElementById( "reportVideo" ).innerHTML = "loading...";
	AjaxRequest.post({
		'url': sUrl + 'tellafriendBeauty/' + dump + '/'
		,'onSuccess': function(req){	raspunsXML = req.responseText;
									document.getElementById( 'reportVideo' ).innerHTML = raspunsXML;}
	});
}

function DoCodeTellAfriend(dump1,dump2){
 	document.getElementById( "reportVideo" ).innerHTML = "loading...";
	AjaxRequest.post({
		'url': sUrl + 'tellafriendCode/' + dump1 + '/' + dump2 + '/'
		,'onSuccess': function(req){	raspunsXML = req.responseText;
									document.getElementById( 'reportVideo' ).innerHTML = raspunsXML;}
	});
}

function DoLyricTellAfriend(dump){
 	document.getElementById( "reportVideo" ).innerHTML = "loading...";
	AjaxRequest.get({
		'url': sUrl + 'tellafriendLyric/' + dump + '/'
		,'onSuccess': function(req){	raspunsXML = req.responseText;
									document.getElementById( 'reportVideo' ).innerHTML = raspunsXML;}
	});
}

function DoLyricCorrection(dump){
 	document.getElementById( "reportVideo" ).innerHTML = "loading...";
	AjaxRequest.get({
		'url': sUrl + 'correctLyrics/' + dump + '/'
		,'onSuccess': function(req){	raspunsXML = req.responseText;
									document.getElementById( 'reportVideo' ).innerHTML = raspunsXML;}
	});
}

function SubmitLyricsCorrection(idLyric, email, afterlyric){
	if (emailCheck(email)) {
		afterlyric = afterlyric.replace(/(\r\n|\r|\n)/g, '\n'); afterlyric = afterlyric.replace(/\n/g, '|||');
		document.getElementById( "reportVideo" ).innerHTML = "<center>submitting... please wait...<br /><img src='http://static.kovideo.net/images/ajax-loader.gif' /></center>";
		AjaxRequest.post({
			'url': sUrl + 'correctLyrics/' + idLyric + '/submit/'
			,'parameters': { 'idLyric':idLyric, 'afterlyric':afterlyric, 'email':email }
			,'onSuccess': function(req){	raspunsXML = req.responseText;
										document.getElementById( 'reportVideo' ).innerHTML = raspunsXML;}
		});
		return false;
	} else {
		alert('Error: email address is in the wrong form. Please correct it and hit submit again.');
		return false;
	};
}

function DoASXCodeGen(dump){
 	document.getElementById( "asxcode" ).innerHTML = "<center>loading... please wait...<br /><img src='http://static.kovideo.net/images/ajax-loader.gif' /></center>";
	AjaxRequest.post({
		'url': sUrl + 'videoCodeAsxGen/embed/' + dump + '/'
		,'onSuccess': function(req){	raspunsXML = req.responseText;
									document.getElementById( 'asxcode' ).innerHTML = raspunsXML;}
	});
}



function ExpandChartS(liSource,expandArea,retractButton,searchString){
	document.getElementById( liSource ).onclick = function (event){RetractChartS(liSource,expandArea,retractButton,searchString); return false;};
 	document.getElementById( expandArea ).className = "ULchartsSVIDsBigVidBox chartsResNeAscuns";
 	document.getElementById( expandArea ).innerHTML = "<center>searching... please be pacient, this might take a few seconds ...<br /><img src='http://static.kovideo.net/images/ajax-loader.gif' /></center>";
	document.getElementById( retractButton ).innerHTML = '<img src="http://static.kovideo.net/images/charts-retract.gif" border="0" />';

	AjaxRequest.post({
		'url': sUrl + 'ExpandChartS/' + searchString + '/' + expandArea + '/'
		,'onSuccess': function(req){raspunsXML = req.responseText.split('|||', 2);
									document.getElementById( raspunsXML[0] ).innerHTML = raspunsXML[1];}
	});
}
function RetractChartS(liSource,expandArea,retractButton,searchString){
	document.getElementById( liSource ).onclick = function (event){ExpandChartS(liSource,expandArea,retractButton,searchString); return false;};
 	document.getElementById( expandArea ).className = "ULchartsSVIDsBigVidBox chartsResAscuns";
 	document.getElementById( expandArea ).innerHTML = "";
	document.getElementById( retractButton ).innerHTML = '<img src="http://static.kovideo.net/images/charts-expand.gif" border="0" />';
}
function changeChart(whichTop,chartsTitle){
	document.getElementById( "chartsNavToday" ).style.display="none";
	document.getElementById( "chartsNavWeek" ).style.display="none";
	document.getElementById( "chartsNavMonth" ).style.display="none";
	document.getElementById( "chartsTextToday" ).style.display="none";
	document.getElementById( "chartsTextWeek" ).style.display="none";
	document.getElementById( "chartsTextMonth" ).style.display="none";
	document.getElementById( "chartsContentToday" ).style.display="none";
	document.getElementById( "chartsContentWeek" ).style.display="none";
	document.getElementById( "chartsContentMonth" ).style.display="none";

	document.getElementById( "chartsContent"+whichTop ).style.display="block";
	document.getElementById( "chartsNav"+whichTop ).style.display="inline";
	document.getElementById( "chartsText"+whichTop ).style.display="block";
	
	document.getElementById( "chartsTitle" ).innerHTML = chartsTitle;
}




/** --------------------------PostMYSQLCodeToPage Code--------------------------        **/
function DOBlogCodeGen(idasx,qarrAdevaratDe2,qarrAdevaratDe3,qarrAdevaratDe4,artistbrowse,melodiebrowse) {
document.write ("<textarea class='bginput' onclick='focus(); select();'>" + htmlEncode("<style>.hov:hover{background-color:yellow}</style><div id='Title' style='font:10px verdana;width:300px'>Music Video:<a class='hov' style='display:block;width:310px;border:solid 1px black;padding:5px' href='http://www.kovideo.net/videos/"+qarrAdevaratDe2+"/"+qarrAdevaratDe3+"/"+qarrAdevaratDe4+".html' target='_blank'>"+melodiebrowse+" (by "+artistbrowse+")<p><embed name='RAOCXplayer' src='http://www.kovideo.net/lib/asx.asp?id="+idasx+"' type='application/x-mplayer2' width='300' height='280' autostart='1' ShowControls='1' ShowStatusBar='0' loop='true' EnableContextMenu='0' DisplaySize='0' pluginspage='http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/'></embed></a><p style='margin:3px 0px'><a href='http://www.kovideo.net/' target='_blank' class = 'hov'>Music Video Code by KOvideo.net</a></p></div>") + "</textarea>");
}
function DOBlogLinkGen(idasx,qarrAdevaratDe2,qarrAdevaratDe3,qarrAdevaratDe4,artistbrowse,melodiebrowse) {
document.write ("<textarea class='bginput' onclick='focus(); select();'>" + htmlEncode("<a href='http://www.kovideo.net/videos/" + qarrAdevaratDe2 + "/" + qarrAdevaratDe3 + "/" + qarrAdevaratDe4 + ".html' title='" + melodiebrowse + " Music Video by " + artistbrowse + "' target='_blank'>" + melodiebrowse + " Video by " + artistbrowse + " @ KOvideo.net</a>") + "</textarea>");
}
function DOBlogForumGen(idasx,qarrAdevaratDe2,qarrAdevaratDe3,qarrAdevaratDe4,artistbrowse,melodiebrowse) {
document.write ("<textarea class='bginput' onclick='focus(); select();'>" + htmlEncode("[URL=http://www.kovideo.net/videos/" + qarrAdevaratDe2 + "/" + qarrAdevaratDe3 + "/" + qarrAdevaratDe4 + ".html]" + melodiebrowse + " video by " + artistbrowse + "[/URL]") + "</textarea>");
}



function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}



function MenuCondense(ele) {
    var srcElement = document.getElementById(ele);
    if(srcElement != null) {
		if(srcElement.className == "mvmenuClosed"){
			srcElement.className= "mvmenuOpened"; 
			createCookie('ko-menudisplay','mvmenuOpened',31);}
		else {
			srcElement.className = "mvmenuClosed"; 
			createCookie('ko-menudisplay','mvmenuClosed',31);
		}
		return false; 
	}
}



function DisplayLyricsWidget(dump){
    document.getElementById( "embedCode" ).innerHTML = ('<embed src=\"/content/scroller.swf?id='+dump+'\" type=\"application/x-shockwave-flash\" wmode=\"transparen\t" width=\"160\" height=\"200\" bgcolor=\"#e2e6ee\"></embed>');
}



function FetchEmbedCode(dump1, dump2)
{ 	
	document.getElementById( "viddisplay" ).innerHTML = "";
	document.getElementById( "lightLivePreview1" ).innerHTML = "<center>Loading...<br /><img src='http://static.kovideo.net/images/ajax-loader.gif' /></center>";
	document.getElementById( "lightVideoCode" ).innerHTML = "Loading.. Please wait.";
	AjaxRequest.post({
		'url': sUrl + 'GenerateCodeEmbed/' + dump1 + '/' + dump2 + '/'
		,'onSuccess': function(req){	raspunsXML = req.responseText.split('|||', 2);
									document.getElementById( "lightVideoCode" ).innerHTML = raspunsXML[0];
									document.getElementById( "lightLivePreview1" ).innerHTML = raspunsXML[1];}
	});
}



function DisplayPlaylist(targetId){
	document.getElementById( targetId ).innerHTML = "<center>Loading...<br /><img src='http://static.kovideo.net/images/ajax-loader.gif' /></center>";
	AjaxRequest.post({
		'url': sUrl + 'PlayList/display/'
		,'parameters':{ 'targetId':targetId }
		,'onSuccess': function(req){ raspunsXML = req.responseText.split('|||', 2);
									document.getElementById(raspunsXML[0]).innerHTML = raspunsXML[1]; }
	});
}



function DisplayPoll(whatAction, dump){
	document.getElementById( 'pollCode' ).innerHTML = "<center>Loading...<br /><img src='http://static.kovideo.net/images/ajax-loader.gif' /></center>";
	AjaxRequest.get({
		'url': sUrl + whatAction + '/' + dump + '/'
		,'onSuccess':function(req){ document.getElementById( 'pollCode' ).innerHTML = req.responseText; }
	});
}
