$(document).ready(function() {
   
   $("[rel=auto]").each(function(i){
					 
		var mytab = $(this).attr('mytable');			 
    	$(this).autocomplete(GLOBAL_PATH+"/index.php?main=registeration&sub=ajax_auto&process=1", 
								 { 
								 minChars:2, 
								 autoFill:false,
								 selectFirst:false,
								 maxItemsToShow:10,
								 extraParams: { 
								 				'table': mytab 
											  }
								 }); 
   });
	

light_settings = {
imageLoading:			GLOBAL_PATH+'/images/lightbox/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
imageBtnPrev:			GLOBAL_PATH+'/images/lightbox/lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
imageBtnNext:			GLOBAL_PATH+'/images/lightbox/lightbox-btn-next.gif',			// (string) Path and the name of the next button image
imageBtnClose:			GLOBAL_PATH+'/images/lightbox/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
imageBlank:				GLOBAL_PATH+'/images/lightbox/lightbox-blank.gif'
}
$('a.lightbox').lightBox(light_settings); 

});

function showhide(elemid) {
	$(elemid).slideToggle();
}
function showhidesec(elemid) {
	$("[@relsh="+elemid+"]").slideToggle();
}

function warn() {
	if(confirm("Are you sure you want to delete the selected product?")) 
	return true;
	else
	return false;
}
function warn2(elem) {
	if(confirm(elem)) 
	return true;
	else
	return false;
}

function LimitAttach(myvar) {
	var file = $(myvar).val();
	extArray = new Array(".gif", ".jpg", ".jpeg", ".png");
	allowSubmit = false;
	if (!file) return false;
	ext = file.slice(file.lastIndexOf(".")).toLowerCase();
	for (var i = 0; i < extArray.length; i++) {
		if (extArray[i] == ext) { allowSubmit = true; break; }
	}
	
	if(allowSubmit==false) {
	alert('Please select the image file with .gif, .jpg, .jpeg, .png extension');		
	$(myvar).val('');
	}
		
	//alert(allowSubmit);
}
function LimitAttachAll(myvar) {
	var file = $(myvar).val();
	extArray = new Array(".gif", ".jpg", ".jpeg", ".png", ".doc", ".docx", ".pdf", ".zip");
	allowSubmit = false;
	if (!file) return false;
	ext = file.slice(file.lastIndexOf(".")).toLowerCase();
	for (var i = 0; i < extArray.length; i++) {
		if (extArray[i] == ext) { allowSubmit = true; break; }
	}
	
	if(allowSubmit==false) {
	alert('Please select the file with .gif, .jpg, .jpeg, .png, .doc, .docx, .pdf, .zip extension');		
	$(myvar).val('');
	}
		
	//alert(allowSubmit);
}
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

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
		
        $( "#files"+ intCurrentRowId , clonedRow ).attr( { "id" : "files" + intNewRowId, "name" : "files" + intNewRowId, "value" : "" } );    
		
		$( "#bp_name"+ intCurrentRowId , clonedRow ).attr( { "id" : "bp_name" + intNewRowId, "value" : "" } );
		$( "#awu_name"+ intCurrentRowId , clonedRow ).attr( { "id" : "awu_name" + intNewRowId, "value" : "" } );      
		  
		$( "#matmacpro"+ intCurrentRowId , clonedRow ).attr( { "id" : "matmacpro" + intNewRowId, "value" : "" } );     
		$( "#language"+ intCurrentRowId , clonedRow ).attr( { "id" : "language" + intNewRowId, "value" : "" } );
        $( "#xp"+ intCurrentRowId , clonedRow ).attr( { "id" : "xp" + intNewRowId, "value" : "" } );      
		
		$( "#category_id"+ intCurrentRowId , clonedRow ).attr( { "id" : "category_id" + intNewRowId, "value" : "" } );
		$( "#category_text"+ intCurrentRowId , clonedRow ).attr( { "id" : "category_text" + intNewRowId, "value" : "" } );
		$( "#clickcat:last" , clonedRow ).attr( { "clk" : intNewRowId } );
		
		
		$( "#delete_link"+ intCurrentRowId , clonedRow ).attr( { "id" : "delete_link" + intNewRowId } );
        $( "#delete_link"+ intNewRowId , clonedRow ).css( { "display" : "" } );
       
		
		
        // Add to the new row to the original table
        $( "#" + strTableName ).append( clonedRow );
         
       // $( "#" + 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!' );
    }

}

function ajax_del_image(from) {
	if(!confirm('Are you sure you want to delete this image'))
	return false;
	//to   = '#icontent3 span';
	$.get(GLOBAL_PATH+'/admin/index.php?main=ajax&sub=ajax_img&process=1', {  'img_id' : $(from).attr('img_id') }, 
	function (data){
		if(data==1) {
			$('#imgtable_'+$(from).attr('img_id')).fadeOut("slow");
		}else{
			alert('Error in deleting the specific image');
		}
	})
}