var isNS4=document.layers?true:false;
var isIE=document.all?true:false;
var isNS6=!isIE&&document.getElementById?true:false;
var isDOM = (document.getElementById) ? true : false;
var popup_win=[];
var crop;

function btSubmit(pForm,pCampo,pValore) {
	document.forms[pForm].elements[pCampo].value = pValore;
	document.forms[pForm].submit();
}

function btFormSubmit(fName) {
	opener.document.forms[fName].submit();
	return false;
}

function btSetVal(pForm,pCampo,pValore) {
	document.forms[pForm].elements[pCampo].value = pValore;
}

function btConfirm(pMsg) {
    return confirm(pMsg,'Violet');
}

function btSleep(naptime){
      var sleeping = true;
      var now = new Date();
      var alarm;
      var startingMSeconds = now.getTime();
      while(sleeping){
         alarm = new Date();
         alarmMSeconds = alarm.getTime();
         if(alarmMSeconds - startingMSeconds > naptime){ sleeping = false; }
      }
   }

function showImagePopUp(pSOP, idFoto){
	var id = 'popup_'+idFoto;
	var popW = 840, popH = 768;
	var winl = (screen.width-popW)/2;
	var wint = (screen.height-popH)/2;
	var eURL = 'index.php?sop=' + encodeURIComponent(pSOP);
	popup_win[idFoto] = window.open(eURL, id, 'status=no,resizable=1,scrollbars=1,width=' + popW + ',height=' + popH + ',top=' + wint + ',left=' + winl);
}

function refreshImagePopup(idFoto){
	popup_win[idFoto].location.href = popup_win[idFoto].location.href
	//popup_win[idFoto].document.getElementById('lightbox_box_scheda').innerHTML = 'CICCIO';
}

function reloadMainPage(pNewURL){
	location.href=pNewURL;
}
/*
function btOpenPopup(pUrl) {
	day = new Date();
	id = day.getTime();
	var pCompleteUrl = pUrl + '&popupid=' + id;
	var popW = 830, popH = 700;
	var winl = (screen.width-popW)/2;
	var wint = (screen.height-popH)/2;
	ww=window.open(pCompleteUrl,id,'status=no,resizable=1,scrollbars=1,width=' + popW + ',height=' + popH + ',top=' + wint + ',left=' + winl);
}
*/

function btOpenPopup(eURL, popW, popH) {
	day = new Date();
	id = day.getTime();
	var winl = (screen.width-popW)/2;
	var wint = (screen.height-popH)/2;
	window.open(eURL,id,'titlebar=no,scrollbars=1,toolbar=0,resizable=1,status=no,width=' + popW + ',height=' + popH + ',top=' + wint + ',left=' + winl);
}

function btOpenPopupS_scroll(pSOP, popW, popH) {
	day = new Date();
	id = day.getTime();
	var winl = (screen.width-popW)/2;
	var wint = (screen.height-popH)/2;
	var eURL = 'index.php?sop=' + encodeURIComponent(pSOP);
	window.open(eURL,id,'titlebar=no,scrollbars=1,toolbar=0,resizable=1,status=no,width=' + popW + ',height=' + popH + ',top=' + wint + ',left=' + winl);
}

function btSearchAndClose(pKW) {
	opener.location = 'index.php?op=src_result&kw='+pKW;
	self.close();
}

function setOpacity(id, level) {
    var element = document.getElementById(id);
    element.style.display = 'inline';
    element.style.zoom = 1;
    element.style.opacity = level;
    element.style.MozOpacity = level;
    element.style.KhtmlOpacity = level;
    element.style.filter = "alpha(opacity=" + (level * 100) + ");";
}

function fadeIn(id, steps, duration, interval, fadeOutSteps, fadeOutDuration){
    var fadeInComplete;
    for (i = 0; i <= 1; i += (1 / steps)) {
      setTimeout("setOpacity('" + id + "', " + i + ")", i * duration);
      fadeInComplete = i * duration;
    }
    //set the timeout to start after the fade in time and the interval to display the

    //message on the screen have both completed

    setTimeout("fadeOut('" + id + "', " + fadeOutSteps + ", " + fadeOutDuration + ")", fadeInComplete + interval);
}

function fadeOut(id, steps, duration) {
    var fadeOutComplete;
    for (i = 0; i <= 1; i += (1 / steps)) {
      setTimeout("setOpacity('" + id + "', "  + (1 - i) + ")", i * duration);
      fadeOutComplete = i * duration;
    }
    //completely hide the displayed message after the fade effect is complete

    setTimeout("hide('" + id + "')", fadeOutComplete);
}

function hide(id){
    document.getElementById(id).style.display = 'none';
}

function POPUP_addLightboxRow(idFoto){
	xajax_addLightboxRow(idFoto);
}

function makeHistory(newHash) {
  window.location.hash = newHash;
}

/************************************************* FUNZIONI CROP *************************************************
* http://www.defusion.org.uk/code/javascript-image-cropper-ui-using-prototype-scriptaculous/comment-page-27/
******************************************************************************************************************/

// setup the callback function
function onEndCrop( coords, dimensions ) {
	$( 'x1' ).value = coords.x1;
	$( 'y1' ).value = coords.y1;
	$( 'x2' ).value = coords.x2;
	$( 'y2' ).value = coords.y2;
}

function attivaCrop(randseq, ratioX, ratioY, minW, minH) { 
	var idimg= 'imgRitaglia_' + randseq;
	crop = new Cropper.Img( 
		idimg,
		{
			ratioDim: { x: ratioX, y: ratioY },
			minWidth: minW, 
			minHeight: minH, 			 
			displayOnInit: true,
			onEndCrop: onEndCrop 
		}
	) 
}

function resetCrop() {
	crop.remove();
}
