var id=0;
var count = 1;
var max = 10;
var currentimage = 0;
var ALLOWED_TYPES = '.gif.jpg.jpeg.png.';

function add_item()
{
var element;
if(count>=max)
{
	alert('You can not add more');
	return false;
}
x = Builder.node('div',{className:'ct',id:'opt'+id});
element = Builder.node('input',{name:'pic_'+count,type:'file',className:'bttn',size:'40'});

element2 = Builder.node('span',{className:'sp'});
$(element2).innerHTML = '&nbsp;<a href="#" onclick="return remove_item('+id+')"><img src="../images/admin/remove.png" width="24" height="24" border="0" valign="bottom"></a><br /><br />';
x.appendChild(element);
x.appendChild(element2);

id++; count++;
$('boxes').appendChild(x);
return false;
}

function remove_item(id)
{
a = 'opt'+id;
x = document.getElementById(a);
$('boxes').removeChild(x);
count--;
return false;
}

function check_all() 
{
	var el = $('formid').select('[class="ptest"]');	
	var value = document.getElementById('value').checked;	
	for(var i=0; i<el.length; i++)
	{
		if (value == true) 
			el[i].checked = true;
		else 
			el[i].checked = false;
	}
	return false;
}

function show_data(status,action)
{
	window.location.href="index.php?"+action+"&status="+status;
	return false;
}

function page_go(pageno,action)
{
      if(pageno >0)
      window.location.href= "index.php?"+action+"&page="+pageno;
}

function displayimage()
{
	show_loading();
	if(arguments.length>0)
		cp.call('index.php?module=ajax&action=ajax_init','getimg',outputimage,arguments[0]);
	else
		cp.call('index.php?module=ajax&action=ajax_init','getimg',outputimage);
	cp.call('index.php?module=ajax&action=ajax_init','get5img',genall);	
	return false;
}

function outputimage(result)
{
	var root = result.getElementsByTagName('ajaxResponse').item(0);
	//var error = root.getElementsByTagName('error').item(0).firstChild.data;
	var id = root.getElementsByTagName('id').item(0).firstChild.data;
	var image = root.getElementsByTagName('image').item(0).firstChild.data;	
	var url = root.getElementsByTagName('url').item(0).firstChild.data;
	var code = root.getElementsByTagName('code').item(0).firstChild.data;
	var rating = root.getElementsByTagName('rating').item(0).firstChild.data;
	var votes = root.getElementsByTagName('votes').item(0).firstChild.data;
	var width = rating * 30;
	currentimage = id;
	$('img').src=code;
	$('code').update(url);
	$('url').update(code);
	$('current_rating').setStyle({width:width+'px'});
	$('votes').update(votes);
	$('crate').update(rating);
}

function genall(result)
{
	var root = result.getElementsByTagName('ajaxResponse').item(0);
	var total = root.getElementsByTagName('total').item(0).firstChild.data;
	var x, y, z;
	for(var i=1; i<=total; i++)
	{
		x = "id"+i;
		y = "timage"+i;
		var imgid = root.getElementsByTagName(x).item(0).firstChild.data;
		var timage = root.getElementsByTagName(y).item(0).firstChild.data;	
		$($(x)).src=timage;		
		$($(x)).alt=imgid;		
		$($(x)).setStyle({cursor:'hand' }); 
		$($(x)).onclick=function(){
			var z=this.alt;
			displayimage(z);
			return false;
		};
	}
	hide_loading();	
}

function rate(r)
{
	cp.call('index.php?module=ajax&action=ajax_init','rate',rateresult,currentimage,r);
	return false;
}

function rateresult(result)
{
	var root = result.getElementsByTagName('ajaxResponse').item(0);
	var avoted = root.getElementsByTagName('avoted').item(0).firstChild.data;
	//avoted=1  already voted    0=new vote
	displayimage();
}

function show_loading()
{
	var top= document.viewport.getScrollOffsets().top; 
	var left=document.viewport.getDimensions().width - 200;
	$('loading').setStyle({display:'block'});
	$('loading').style.top = top;
	$('loading').style.left = left;
}

function hide_loading()
{
	$('loading').setStyle({display:'none'});
}



function filetypeCheck() {


	var fn = $("ufile").value;
	if (fn == ""){
		$("allowed").className ='';
		$("upload").disabled = true;
	} else {
		var ext = fn.split(".");
		if (ext.length==1)
		ext = '.noext.';
		else
		ext = '.' + ext[ext.length-1].toLowerCase() + '.';

		if (ALLOWED_TYPES.indexOf(ext) == -1) {
			$("allowed").className ='red';
			$("upload").disabled = true;
		} else {
			$("allowed").className ='';
			$("upload").disabled = false;
		}
	}
}

function clearme()
{
	var email = $('email').value;
	if(email=='Your Email')
		$('email').value = '';
}

function notifyupload()
{
	hide_loading();
	$("submitinfo").hide();
	$("theform").hide();
	$("uresult").style.display='block';
}

