var CheckUploadInterval = setInterval("CheckUpload()", 2500);


function checkFields(){
	var good = true;
	var fields = ['video_name','creator','email'];
	var x=0;
	if( $('directlink').value.length > 0 ) {
		var parts = $('directlink').value.split('.');
		if( parts[ parts.length - 1 ] != 'mov' && parts[parts.length - 1] != 'flv' ){
			$('directlink').style.backgroundColor = '#FFCCCC';
			good = false;
		} else {
			$('directlink').style.backgroundColor = '#FFFFFF';
		}
	} else {
		$('directlink').style.backgroundColor = '#FFFFFF';
	}			


	for(x=0;x<fields.size();x++){
		if( !$(fields[x]).value || $(fields[x]).value == ''){
			$(fields[x]).style.backgroundColor = "#FFCCCC";
			good = false;
		}else{
			$(fields[x]).style.backgroundColor ="#FFFFFF";
		}
	}
	if( !good ){
		return false;
	}else{
		formsubmitted();
		$('submitForm').target = 'formTarget';
		$('submitForm').submit();
	}
}	

function CheckUpload()
{
	if( window.frames['formTarget'].document.getElementById('result') )
	{
		var thehtml = window.frames['formTarget'].document.getElementById('result').innerHTML;
	} 
	else
	{
		var thehtml = '';
	}
	
	
	if( thehtml == 'success')
	{
		$('alertError').style.display = 'none';
		$('alertSuccess').style.display = 'block';
		$('alertUploading').style.display = 'none';
		$('alertSubmitted').style.display = 'none';
		window.frames['formTarget'].document.getElementById('result').innerHTML = '';
		setTimeout("document.location = 'submit'", 5000);
	}
	else if ( thehtml == 'failure' )
	{
		$('alertError').style.display = 'none';
		$('alertSuccess').style.display = 'none';
		$('alertUploading').style.display = 'none';
		window.frames['formTarget'].document.getElementById('result').innerHTML = '';
	}
}


function getheight()
{
	var pageHeight;
	var pageWidth;
	if( window.innerHeight && window.scrollMaxY ) // Firefox 
	{
		pageWidth = window.innerWidth + window.scrollMaxX;
		pageHeight = window.innerHeight + window.scrollMaxY;
	}
	else if( document.body.scrollHeight > document.body.offsetHeight ) // all but Explorer Mac
	{
		pageWidth = document.body.scrollWidth;
		pageHeight = document.body.scrollHeight;
	}
	else // works in Explorer 6 Strict, Mozilla (not FF) and Safari
	{ 
		pageWidth = document.body.offsetWidth + document.body.offsetLeft; pageHeight = document.body.offsetHeight + document.body.offsetTop; 
	}
	return pageHeight;

}


function createBackground () 
{
	
	var pageheight = getheight();
	$('lightboxbg').style.height = '100%';
	var d = document.createElement('DIV');
    d.innerHTML = '<iframe id="formTarget" name="formTarget" style="display:none;" src="about:blank"></iframe>';
    document.body.appendChild(d);
}

function formsubmitted()
{
	
	if( $('upload').value )
	{
		$('alertUploading').style.display='block';
		$('lightboxbg').style.display = 'block';
	}
	else
	{
		$('alertSubmitted').style.display='block';
		$('lightboxbg').style.display = 'block';
	//	setTimeout("document.location = 'submit'", 5000);
	}

}




function startup() 
{
	createBackground();
	CheckUpload();
}

window.onload = startup;
