// ------------------------------------------------------------------->>> DREAMWEAVER FUNCTIONS

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
	}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
	}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
	}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
	}
	
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
// ------------------------------------------------------------------->>> SHOW/HIDE
function show(id, maxElements){
	if(document.getElementById){
		for(i=0; i<maxElements; i++) {
			document.getElementById("row"+i).style.display = "none";
			}			
		document.getElementById("row"+id).style.display = "block"; 
		}
}

function hide(maxElements){
	if(document.getElementById){
		for(i=0; i<maxElements; i++) {
			document.getElementById("row"+i).style.display = "none";
			}			
	}
}

// ------------------------------------------------------------------->>> PRELOAD

function preloadIcons() {
	MM_preloadImages('images/icons/delete.gif');
	MM_preloadImages('images/icons/edit.gif');
	MM_preloadImages('images/icons/insert.gif');
	MM_preloadImages('images/icons/pedigree.gif');
	MM_preloadImages('images/icons/picture.gif');
	}

// ------------------------------------------------------------------->>> PRELOAD

function checkSelectedDog(row) {
	var mySelect = document.getElementById('select'  +row);
	var myField  = document.getElementById('pedigree'+row);
	if(mySelect.value == '') {
		myField.className = 'abled';
		myField.disabled  = false;
		} else {
		myField.className = 'disabled';
		myField.disabled  = true;
		}
	}

// ------------------------------------------------------------------->>> FOTOGALLERY
function submitForm(idDog, picDog){ 
		var form    = document.getElementById('fotoForm');
		form.action = 'main.php?pageToInclude=cani_fotogallery&idDog='+idDog+'&maxFiles='+count+'&picDog='+picDog;
		form.submit();
};

/* Class by Stickman -- http://www.the-stickman.com */

function MultiSelector(list_target, max ){
	// Where to write the list
	this.list_target = list_target;
	// How many elements?
	this.count = 0;
	// How many elements?
	this.id = 0;
	// Is there a maximum?
	if( max ){
		this.max = max;
	} else {
		this.max = -1;
	};
	
	// Add a new file input element
	this.addElement = function( element ){

		// Make sure it's a file input element
		if( element.tagName == 'INPUT' && element.type == 'file' ){

			// Element name -- what number am I?
			element.name = 'file_' + this.id++;
			
			// Add reference to this object
			element.multi_selector = this;
			
			// CSS
			element.className = 'little';
			
			// What to do when a file is selected
			element.onchange = function(){
				
				path = element.value;
				posizione_punto = path.lastIndexOf(".");
				lunghezza_stringa = path.length;
				estensione = path.substring(posizione_punto+1,lunghezza_stringa);
				
				if(estensione.toLowerCase() == 'jpg') {

					// New file input
					var new_element  = document.createElement( 'input' );
					new_element.type = 'file';
	
					// Add new element
					this.parentNode.insertBefore( new_element, this );
	
					// Apply 'update' to element
					this.multi_selector.addElement( new_element );
	
					// Update list
					this.multi_selector.addListRow( this );
					
					// Hide this: we can't use display:none because Safari doesn't like it
					this.style.position  = 'absolute';
					this.style.left      = '-1000px';
				
					} else {
						
					alert('Devi caricare un file .JPG !!!');
					
					}
					
					

			};
			// If we've reached maximum number, disable input element
			if( this.max != -1 && this.count >= this.max ){
				//element.disabled = true;
			};

			// File element counter
			this.count++;
			
			// Absolutize varibale count
			count = this.count;
			
			// Most recent element
			this.current_element = element;
			
		} else {
			// This can only be applied to file input elements!
			alert( 'Error: not a file input element' );
		};

	};

	/**
	 * Add a new row to the list of files
	 */
	this.addListRow = function( element ){

		// Row div
		var new_row       = document.createElement( 'div' );
		new_row.className = 'fileRow';
		
		// Delete button
		var new_row_button = document.createElement( 'input' );
		new_row_button.type      = 'button';
		new_row_button.value     = ' X ';
		new_row_button.className = 'delete';

		// References
		new_row.element = element;

		// Delete function
		new_row_button.onclick= function(){

			// Remove element from form
			this.parentNode.element.parentNode.removeChild( this.parentNode.element );

			// Remove this row from the list
			this.parentNode.parentNode.removeChild( this.parentNode );

			// Decrement counter
			this.parentNode.element.multi_selector.count--;

			// Re-enable input element (if it's disabled)
			this.parentNode.element.multi_selector.current_element.disabled = false;

			// Without it Safari wants to reload the browser window
			return false;
		};

		// Set row value
		new_row.innerHTML = element.value;

		// Add button
		new_row.appendChild( new_row_button );

		// Add it to the list
		this.list_target.appendChild( new_row );
		
	};

};

function doRedirect(sexToShow, section) {
	if(section == 'front') {
		location.href = "index.php?pageToInclude=cani&sex="+sexToShow;
		} else if(section == 'admin') {
		location.href = "main.php?pageToInclude=cani&sex="+sexToShow;	
		} else if(section == 'cuccioli') {
		location.href = "main.php?pageToInclude=cuccioli&sex="+sexToShow;	
		}
	}