/* ---------------------------------------------------- */
/* JQuery												*/
/* ---------------------------------------------------- */
$jQuery(document).ready(function() 
{
	// Make any links or images that have a class of view_image to open up in the lightbox
	$jQuery('a.lightbox').lightBox({
		overlayOpacity: 0.6,
		type: 'preview',
		imageLoading: 'http://www.ucanski.eu/Les_Carroz/lightbox-ico-loading.gif',
		imageBtnClose: 'http://www.ucanski.eu/Les_Carroz/lightbox-btn-close.gif',
		imageBtnPrev: 'http://www.ucanski.eu/Les_Carroz/lightbox-btn-next.gif',
		imageBtnNext:  'http://www.ucanski.eu/Les_Carroz/lightbox-blank.gif',
		containerResizeSpeed: 350
   });
	
});

/* Scrolls the page back top the top */
function BackToTop()
{
	$jQuery('#top').ScrollTo(800, 'easeout'); 
	return false;
}

/* This centres the notification box on the screen, depending upon screen size */
function SetNotifyBoxPosition() {

	var notify_width = 500;
	
	var de = document.documentElement;
    var h = self.innerHeight || (de&&de.clientHeight) || document.body.clientHeight;
    var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
    var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body;
    var top = (h)/2;
    
    $jQuery("#notify").css({width:notify_width+"px",left: ((w - notify_width)/2)+"px", top: top+"px" });
}

/* -------------------------------------------- */
/* General Functions							*/
/* -------------------------------------------- */
/* Encode Html, similar to php's urlencode */
function urlencode( html ) 
{
	encodedHtml = escape( html );
	encodedHtml = encodedHtml.replace(/\//g,"%2F");
	encodedHtml = encodedHtml.replace(/\?/g,"%3F");
	encodedHtml = encodedHtml.replace(/=/g,"%3D");
	encodedHtml = encodedHtml.replace(/&/g,"%26");
	encodedHtml = encodedHtml.replace(/@/g,"%40");
	
	return encodedHtml;
}
 
 /* Encode Html, similar to php's urldecode */
function urldecode( html ) 
{
	return unescape( html );
} 

function trim(s){
	toTrim = new Array(' ','\n','\r');
	for(x=0; x<toTrim.length; x++){
		while(s.substring(0,1) == toTrim[x]){
			s = s.substring(1,s.length);
		}
		while(s.substring(s.length-1,s.length) == toTrim[x]){
			s = s.substring(0,s.length-1);
		}
	}
	return s;
}

/* Check to see if we are only entering numbers */
function isNumberKey(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
	return false;
	
	return true;
}


/* Rollover Fuctions */
function GetObject(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=GetObject(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function SwapImage() { //v3.0
  var i,j=0,x,a=SwapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=GetObject(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function SwapImgRestore() { //v3.0
  var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

// Ask as confirmation, before an item is deleted */
function ConfirmDelete( type, msg ) 
{
	// Ask a confirmation alter box.
	input_box=confirm("Are you sure you wish to delete the selected '" + type + "?'\n\n" + msg + "");
	
	// If they have clicked 'Ok', set the checkbox to true.
	if (input_box==true) 
	{ 
		 return true;
	}
	else
	{
		// Stop processing the request, as they clicked 'Cancel'
		return false;
	}		
}

function ShowWarning( form )
{
	if ( form.checked == true )
	{
		// Ask a confirmation alter box.
		input_box=confirm("Checking this box will store your username and encrypted password on you computer\n\nDo you wish to do this?");
		
		// If they have clicked 'Ok', set the checkbox to true.
		if (input_box==true) 
		{ return true; }
		else
		{ form.checked = false; }
	}
	// Stop processing the request, as they clicked 'Cancel'
	return false;
	
}

/* -------------------------------------------- */
/* Image Upload									*/
/* -------------------------------------------- */

/* Array of Allowed Image extensions */
function AllowedImageExtensions()
{
	return 	extArray = new Array(".gif", ".jpg", ".jpeg", ".png");
}

/* Check the image file extension */
function CheckImageFileType( warning, form ) 
{
	// Define the form value to check
	var file = form.value;
	
	// Get a list of allowed extensions
	extArray = AllowedImageExtensions();

	/* Allows Mac users to upload images */
	while (file.indexOf("/") != -1)
	{
		file = file.slice(file.indexOf("/") + 1);
		ext = file.slice(file.indexOf(".")).toLowerCase();
		for (var i = 0; i < extArray.length; i++) {
			
			// return true, if we have found a match
			if (extArray[i] == ext) { return true; }
		}
	}

	/* Allows Windows users to upload images */
	while (file.indexOf("\\") != -1)
	{
		file = file.slice(file.indexOf("\\") + 1);
		ext = file.slice(file.indexOf(".")).toLowerCase();
		for (var i = 0; i < extArray.length; i++) {
			
			// return true, if we have found a match
			if (extArray[i] == ext) { return true; }
		}
	}
	
	return false;
}