// JavaScript Document

var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }

  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }

  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}

/********************************************************
* makes a popup of determinate picture
*
* @param img (image path)
* @param toWidth (required width)
* @param toHeight (required height)
*
********************************************************/
function PopUp(img, toWidth, toHeight){
	foto1= new Image();
	foto1.src=(img);
	Control(img, toWidth, toHeight);
}

/********************************************************
* control popup creation of determinate picture
*
* @param img (image path)
* @param toWidth (required width)
* @param toHeight (required height)
*
********************************************************/
function Control(img, toWidth, toHeight){
	if((foto1.width!=0)&&(foto1.height!=0)){  
		verFoto(img, toWidth, toHeight);
	} else {funcion="Control('"+img+"')";  
		intervalo=setTimeout(funcion,20);
	}
}

/********************************************************
* open a popup of determinate picture on new window
*
* @param img (image path)
* @param toWidth (required width)
* @param toHeight (required height)
*
********************************************************/
function verFoto(img, toWidth, toHeight){  
	ancho=foto1.width; 
	alto=foto1.height;
	ratio = ancho/alto;
	if (ratio > 1 ){ 
		reducc = ancho/toWidth;
		ancho = toWidth;
		alto = alto * 1/reducc;
	} else {
		reducc = alto/toHeight;
		alto = toHeight;
		ancho = ancho * 1/reducc;
	}
	height = alto+20;
	width = ancho+20;
	cadena="width="+width+",height="+height+",resizable=no, centered=yes";
	ventana=window.open(img,"",cadena);
}

/********************************************************
* resize an IMG object
*
* @param slider (IMG object)
* @param toWidth (required width)
* @param toHeight (required height)
*
********************************************************/
function preload(slider, toWidth, toHeight){
	image1 = new Image();
	image1.src = slider.src;
	width = image1.width;
	height = image1.height;
	ratio = width/height;
	if (width != 0 && height != 0){
		if (ratio >= 1){
			reducc = width/toWidth;
			width = toWidth;
			height = height * 1/reducc;
		} else {
			reducc = height/toHeight;
			height = toHeight;
			width = width * 1/reducc;
		}
	} else {
		height = toHeight;
		width = toHeight;
	}
	slider.setAttribute("width",width);
	slider.setAttribute("height",height);
}

/********************************************************
* confirm delete
*
* @param act (delete url action)
*
********************************************************/
function confdel(act){
	if(confirm("Are you sure to delete this item?")){
		document.forms[0].action = act;
		return true;
	} else {
		return false;
	}
}

/********************************************************
* send resort list values
*
* @param field (send field to sort list)
*
********************************************************/
function reSort(field){
	if (document.forms[0].orderDir.value == "DESC"){
		document.forms[0].orderDir.value = "ASC";
	} else {
		document.forms[0].orderDir.value = "DESC";
	}
	document.forms[0].orderField.value = field;
	document.forms[0].submit();
}

/********************************************************
* max characters of textarea ***acoted yet***
*
* @param obj (object textarea)
*
********************************************************/
function maxInput(obj, message){
	var textSize = (obj.value.length);
	document.formulary.counter.value = (200 - textSize);
	if (textSize == 199){
		aux=obj.value;
	}
	if (textSize >= 200){
		alert(message);
		obj.value = aux;
		obj.blur();
		return false;
	}
}

/********************************************************
* max characters of textarea ***limited to Troxler***
*
* @param obj (object textarea)
*
********************************************************/
function maxInput(obj, message){
	var textSize = (obj.value.length);
	document.formulary.counter.value = (200 - textSize);
	if (textSize == 199){
		aux=obj.value;
	}
	if (textSize >= 200){
		alert(message);
		obj.value = aux;
		obj.blur();
		return false;
	}
}

/********************************************************
* counter reset ***limited to Troxler***
*
* @param obj (object textarea)
*
********************************************************/
function counterReset(){
	if (document.formulary.counter){
		var textSize = (document.formulary.description.value.length);
		document.formulary.counter.value = (200 - textSize);
	}
}

function borrarFoto(id){
	location.href="delPictures.php?picture_id="+id;
}

function imgXY(imgID) {
  /* This function returns the upperleft x,y coordinates of the specified image */
  var XY = new Object();
  var imgObj = document.images[imgID];
  if (document.layers) {
	XY.x = eval(imgObj).x;
	XY.y = eval(imgObj).y;
  } else {
	var x,y, tempEl;
	x = eval(imgObj).offsetLeft;
	y = eval(imgObj).offsetTop;
	tempEl = eval(imgObj).offsetParent;
  	while (tempEl != null) {
  		x += tempEl.offsetLeft;
		y += tempEl.offsetTop;
  		tempEl = tempEl.offsetParent;
  	}
	XY.x = x;
	XY.y = y;
  }
  return XY;
}


function emailCheck (emailStr) 
{
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
	//alert("Email address seems incorrect (check @ and .'s)");
	return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
	if (user.charCodeAt(i)>127) {
	//alert("Ths username contains invalid characters.");
	return false;
	   }
	}
	for (i=0; i<domain.length; i++) {
	if (domain.charCodeAt(i)>127) {
	//alert("Ths domain name contains invalid characters.");
	return false;
	   }
	}
	if (user.match(userPat)==null) {
	//alert("The username doesn't seem to be valid.");
	return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
	for (var i=1;i<=4;i++) {
	if (IPArray[i]>255) {
	//alert("Destination IP address is invalid!");
	return false;
	   }
	}
	return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
	if (domArr[i].search(atomPat)==-1) {
	//alert("The domain name does not seem to be valid.");
	return false;
	   }
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 && 
	domArr[domArr.length-1].search(knownDomsPat)==-1) {
	//alert("The address must end in a well-known domain or two letter " + "country.");
	return false;
	}
	if (len<2) {
	//alert("This address is missing a hostname!");
	return false;
	}
	return true;
}

function validate(){
	var msg = "";
	var flag = true;
	
	if (document.form1.Name.value == ""){
		msg+= "Name is a required field\n";
		flag = false;
	}
	if (document.form1.Email.value == ""){
		msg+= "Email is a required field\n";
		flag = false;
	} else {
		if (!emailCheck(document.form1.Email.value)){
			msg+= "Insert a valid email address\n";
			flag = false;
		}
	}

	if (!flag){
		alert(msg);
	}
	return flag;
}

function validate_dec(){
	var msg = "";
	var flag = true;
	
	if (document.form1.Name.value == ""){
		msg+= "Name is a required field\n";
		flag = false;
	}
	if (document.form1.Email.value == ""){
		msg+= "Email is a required field\n";
		flag = false;
	} else {
		if (!emailCheck(document.form1.Email.value)){
			msg+= "Insert a valid email address\n";
			flag = false;
		}
	}
	if (document.form1.abn.value == ""){
		msg+= "ABN,ACN,BN is a required field\n";
		flag = false;
	}

	if (!flag){
		alert(msg);
	}
	return flag;
}
