var http = false;

http = getHTTPObject();
var NORMAL_STATE = 4;

function rate(videoid,vote) {
  http.abort();
  http.open("GET", "rate.php?videoid="+videoid+"&vote="+vote, true);
  http.onreadystatechange=function() {
    if(http.readyState == NORMAL_STATE) {
      document.getElementById('result').innerHTML = http.responseText;
    }
  }
  http.send(null);
}

function validateLogin()
{
	username = document.getElementById('lusername').value;
	password = document.getElementById('lpassword').value;

	if (username != '' && password  != '') {
		hash = hex_md5(password);
		http.open('GET', 'login_ajax.php?' + 'task=checklogin&username='+username+'&password='+hash, true);
		http.onreadystatechange = handleHttpValidateLogin;
		http.send(null);
	} else {
        document.getElementById('loginerrorcontainer').style.display = 'block';
    }
}

function handleHttpValidateLogin()
{
	if (http.readyState == NORMAL_STATE) {
		result = http.responseText;
        if (result == 1) {
            window.location = 'video_upload.php';
        } else {
            document.getElementById('loginerrorcontainer').style.display = 'block';
        }
	}
}

function validateLoginEx()
{
	username = document.getElementById('lusernameex').value;
	password = document.getElementById('lpasswordex').value;

	if (username != '' && password  != '') {
		hash = hex_md5(password);
		http.open('GET', 'login_ajax.php?' + 'task=checklogin&username='+username+'&password='+hash, true);
		http.onreadystatechange = handleHttpValidateLoginEx;
		http.send(null);
	}
}

function handleHttpValidateLoginEx()
{
	if (http.readyState == NORMAL_STATE) {
		result = http.responseText;
        if (result == 1) {
            window.location = 'video_upload.php';
        } else {
			document.getElementById('lusernameex').value = '';
			document.getElementById('lpasswordex').value = '';	
			$('lusernameex').focus();
		}
	}
}

function update_progress() {
    http.abort();
	progress_key = document.getElementById('progress_key').value;   
	http.open('GET','getprogress.php?progress_key='+progress_key, true);
	http.onreadystatechange=function() {
	    if(http.readyState == NORMAL_STATE) {
	        document.getElementById('result').innerHTML = http.responseText;
	    }
    }
	http.send(null);
}

function progress() {
    window.setInterval("update_progress()", 2000);
}
