// JavaScript Document
$(document).ready(function() {
   
   $('#ds').click(function() {
	$('#rd_dep_id').toggle();
   })
   if($("#ds").attr("checked")!=true) $('#rd_dep_id').toggle();
   
   
    $("input[@rel='dtext']").focus(function(){
    clearText(this);     
    })
   
 	$("#myform").submit(function(){
    clearDefaultText();
    });
	
   
	
});

function clearText(thefield){
if (thefield.defaultValue==thefield.value)
thefield.value = "";
}

function clearDefaultText(){
$("input[@rel='dtext']").each(function () {clearText(this)});
}


function change_select(url,sendvar,from,to) {
	$.get(url, { 'id' : $(from).val() },function (data){ $(to).html(data);$("OPTION:first",to).attr("selected","selected"); })
	
}
function edit_change_select(url,sendvar,from,to,other,next) {
	$.get(url, { 'id' : $(from).val() },function (data){ $(to).html(data);chk_other(from,other,next);})
}
function chk_other(from,target, nexttarget) {
	
	if($("OPTION:selected", $(from) ).text()=='Other'){
		$(target).show();
		$(target+' input').focus();
	}else if($(target).css('display')!='none'){
		$(target).hide();
		if(nexttarget!="")
		$(nexttarget).focus();
	}
}


function duplicateRow(strTableName) {
        // First, lets create the new row using the last one as template...
        var clonedRow = $( "#" + strTableName + " tr:last" ).clone();
        // Take the current identifier, some number in the first cell of the row
        intCurrentRowId = parseInt( $( "#numberIdentifier:last",clonedRow ).val() );
        // Set the new ID
        intNewRowId = intCurrentRowId + 1;
        // Change the current identifier of the row to the new one
        $( "#numberIdentifier:last", clonedRow ).val( intNewRowId );

        // Now let's create the Delete link using the ID of the new TR
        $( "#delete_link"+ intCurrentRowId , clonedRow ).attr( "onclick", "deleteRow( '"+ strTableName +"', 'detailsTr"+ intNewRowId +"' )" );
        
        
		// Change the Id / Name or anything you want for the new attribs...here is where you need to add a lot of stuff to change the id of your variables

        // The following code works without problems on Firefox or IE7
        $( "#bp_name"+ intCurrentRowId , clonedRow ).attr( { "id" : "bp_name" + intNewRowId, "value" : "" } );
        $( "#awu_name"+ intCurrentRowId , clonedRow ).attr( { "id" : "awu_name" + intNewRowId, "value" : "" } );      
		$( "#img"+ intCurrentRowId , clonedRow ).attr( { "id" : "img" + intNewRowId, "name" : "img" + intNewRowId, "value" : "" } );      
		
		$( "#delete_link"+ intCurrentRowId , clonedRow ).attr( { "id" : "delete_link" + intNewRowId } );
        $( "#delete_link"+ intNewRowId , clonedRow ).css( { "display" : "" } );
        
		
		ac = $("[@rel='auto']", clonedRow).autocomplete(GLOBAL_PATH+"/index.php?main=registeration&sub=ajax_auto&process=1", 
								 { 
								 minChars:2, 
								 autoFill:true,
								 selectFirst:true,
								 maxItemsToShow:10
								 
								 });
		ac.each(function(i){
			var tab = $(this).attr('mytable');			 
			this.autocompleter.setExtraParams({
			table: tab
			}); 
		});
		
		
        // Add to the new row to the original table
        $( "#" + strTableName ).append( clonedRow );
        // And finally change the ID of the last row to something we can delete later, not sure why can not be done before the append :S
        $( "#" + strTableName + " tr:last" ).attr( "id", "detailsTr" + intNewRowId );

		$focus_elem=$("[@type='text']", clonedRow)[0];
		if($focus_elem)
		$focus_elem.focus();
		
}

function deleteRow( strTableName, strId )
{
    if ( $( "#" + strTableName + " .detailsTr" ).length > 1 )
    {
        $( "#" + strTableName + " #" + strId ).remove();
    }
    else
    {
        alert( 'You have to keep at least 1 row!' );
    }

}