function g(id){
    return document.getElementById(id);
}
function select_server(obj,id,type){
    rest_price();
    $("#item_td").find('select').attr('selectedIndex',0).hide();
    $('#server_td').find('select').attr('selectedIndex',0)
    $('#item_sel').html('<option value=0>--select--</option>').show();
    var newid=$(obj).attr('value')||id;
    if(newid==0){
	return;
    }
    if(null != document.getElementById('sel'+newid)){
	$('#server_td').find('select').hide();
	$('#sel'+newid).show();
	return ;
    }
    $('#server_td').find('select').hide();
    $('#serv_sel').html('<option value=0>loading...</option>').show();
    $.ajax({
	type:'GET',
	url:'ajax.php?a=getcategory&cid='+newid+'&type='+type,
	success: function(msg){
	    $('#serv_sel').hide();
	    if($.trim(msg) == 'none'){
		add_pruducts(newid);
		return;
	    }
	    $('#server_td').append(msg);
	}
    }
    );
}
function select_item(obj,id,type){
    rest_price();
    var newid=$(obj).attr('value')||id;
    if(newid==0){
	return;
    }
    $(obj).nextAll().hide();
    if(null != document.getElementById('sel'+newid)){
	$('#sel'+newid).show();
	return ;
    }
    $.ajax({
	type:'GET',
	url:'ajax.php?a=getcategory&cid='+newid+'&type='+type,
	success: function(msg){
	    if($.trim(msg) == 'none'){
		if(type=='currency'){
		    add_pruducts(newid,1);
		}
		if(type=='special'){
		    add_sp(newid);
		}
		if(type=='power_leveling'){
		    add_pruducts(newid,4);
		}
		return ;
	    }
	    $(obj).after(msg);
	}	
    }
    );
}

function add_sp(cid){
    rest_price();
    if(null != document.getElementById('sel'+cid)){
	$('#sel'+cid).show();
	return ;
    }
    $("#item_td").find('select').hide();
    $('#item_sel').html('<option value=0>loading...</option>').show();
    $.ajax({
	type:'GET',
	url:'ajax.php?a=getspecials&cid='+cid,
	success: function(msg){
	    $('#item_sel').hide();
	    if($.trim(msg) == 'none'){
		$('#item_sel').html('<option>no products</option>').show();
		return;
	    }
	    $('#item_td').append(msg);
	}
    }
    );
}

function add_pruducts(cid,type_id){
    rest_price();
    if(null != document.getElementById('sel'+cid)){
	$('#sel'+cid).show();
	return ;
    }
    $("#item_td").find('select').hide();
    $('#item_sel').html('<option value=0>loading...</option>').show();
    $.ajax({
	type:'GET',
	url:'ajax.php?a=getproducts&cid='+cid+'&tid='+type_id,
	success: function(msg){
	    $('#item_sel').hide();
	    if($.trim(msg) == 'none'){
		rest_price();
		$('#item_sel').html('<option>no products</option>').show();
		return;
	    }
	    $('#item_td').append(msg);
	}
    }
    );
}
function change_item(obj,id,add_attr){
    id=$(obj).attr('value')||id;
    if(id==0){
	rest_price();
	return;
    }
    $('#price').html($('#option'+id).attr('price'));
    if($('#option'+id).attr('title')){
	$('#desc').html('<h1 class=green>Products Description</h1>'+$('#option'+id).attr('title'));
    }
    if(add_attr){
	$('#attr').load('ajax.php?a=getattr&products_id='+id);
    }
    $('#products_id').attr('value',id);
}
function rest_price(){
    $('#price').html('0.00');
    $('#desc').html('');
    $('#products_id').attr('value',0);
}
function submit_form(){
    if($('#products_id').attr('value')==0){
	alert('Please select a product');
	return false;
    }
    $('#cart_form').submit();
}
function toggle_obj(id,slide){
    if(slide){
	$(id).slideToggle();
    }
    else{
	$(id).toggle();
    }
}
function confirm_product(pid,qty){
    if($('#confirm_content').length>0){
    }else{
	    var layer=$('<div id="confirm_content"></div>');
	    layer.appendTo('body');
    }
    $('#confirm_content').dialog({
	title:'Add to cart',
	autoOpen: false,
	width:600,
	modal: true,
	minWidth:150,
	buttons: {
	    'Close': function(){
		$(this).dialog('close');
	    }
	    }
	});
    $('#confirm_content').dialog('open');
    $('#confirm_content').html('<p align="center"><img src="images/loadinfo.gif" /></p>');
    $('#confirm_content').load('ajax.php?a=add_product&products_id='+pid+'&qty='+qty);
}


function calPrice(typeId,t){
    var j = 0;
    var iStartLevel = $('#sl').val();
    if(t != null) {
	g('el').length = 0
	var k = g('sl').length;
	s_current=g('sl').selectedIndex;
	for(var i = j;i < k;i++) {
	    if(i>=s_current){
		var value=g('sl').options[i].value;
		var name=g('sl').options[i].value;
		var newOption = new Option(value,name);
		g('el').options.add(newOption);
	    }
	}
    }
    var iEndLevel = $('#el').val();
    //alert(iStartLevel+' '+iEndLevel);
    if(iEndLevel == iStartLevel){
        $('#bob_price').html('0.00');
        $('#hour').html('0 Hours');
    }
    if(iEndLevel > iStartLevel) {
	$('#bob_price').html('loading');
	var queryString = {
	    a:'calPrice',
	    typeid:typeId,
	    sl:iStartLevel,
	    el:iEndLevel
	};
	$.getJSON('ajax.php',queryString,function (json) {
	    if(json.error){
		alert(json.error);
		$('#level_form').bind('submit',function(){return false});
		return ;
	    }
	    $('#bob_price').html(json.price);
	    $('#hour').html(json.duration);
	    $("input[rel='Gold-Required']").val(json.required_gold);
	})
    }
}

