/**
* validates a given string str with the given regular expression reg
*
* @param str a string
* @param type
*/

var elements;
var hasFocus;

function submitForm(_form) {
	if (validateAll(_form)) {
    _form.submit();
  }
}

function validateAll(_form) {
	for(i=0;i<_form.length;++i) {
    if (_form.elements[i].type == "text") {
    	//_form.elements[i].blur();
		  //_form.text.value = _form.text.value + "\n" + _form.elements[i].onblur;
		  if (!_form.elements[i].onblur()) {
		  	return false;
		  }
		}
	}
  return true;
}

function validate(inputElement, type, notNull) {
  ret = true;
  str = inputElement.value;
  if (str.length == 0) {
  	if (notNull) {
  		window.alert("Sorry, you have to specify a value for " + inputElement.name + "!");
  		ret = false;
  	} else {
  		return true;
  	}
  }
  if (ret) {
		if (type == "EMAIL") {
			ret = checkEmail(str);
		} else if (type == "ISO") {
			ret = checkIso(str);
		} else if (type == "LINK") {
			ret = checkLink(str);
		} else if (type == "INT") {
			ret = checkInt(str);
		} else if (type == "REAL") {
			ret = checkFloat(str);
		} else if (type == "ISBN") {
			ret = checkISBN(str);
		} else if (type == "ISSN") {
			ret = checkISSN(str);
		} else if (type == "EAN") {
			ret = checkEAN(str);	
		} else if (type == "YEAR") {
			ret = checkYEAR(str);	
		}
	}
	if (!ret) {
		/*if (hasFocus != inputElement.name) {
		  inputElement.focus();
		  hasFocus = inputElement.name;
		  window.alert(hasFocus);
		}*/
	}
	//return ret;
	return ret;
}

function checkEmail(str) {
	var reg = /^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
	if (!reg.exec(str)) {
		window.alert("Sorry, this is not a valid email-address! (" + str + ")");
		return false;
	}
	return true;
}

function checkIso(str) {
	//var reg = /\d\d \d\d \d\d/;
	var reg = /^\d\d(\s\d\d)*$/;
	if (!reg.exec(str)) {
		window.alert("Sorry, this is not a valid ISO-code! (" + str + ")");
		return false;
	}
	return true;
}

function checkLink(str) {
	// [a-zA-Z0-9-]+ erster teil der url  (z.B www) mindestens einmal
	// (\.[a-zA-Z0-9-]+)+ folgeteile der url mit punkt (mindestens einmal)
	// ((\/)?|((\/)(\S)*)) entweder ein / oder ein / mit folgezeichen (alle zeichen ausser whitespace)
	
	var reg=/^[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+((\/)?|((\/)(\S)*))$/;
	if (!reg.exec(str)) {
		window.alert("Sorry, this is not a valid link!");
		return false;
	}
	return true;
}

function checkInt(str) {
	// (\s)* whitespaces vor zahl erlaubt
	// \d+ die zahl
	// (\s)* whitespaces nach zahl erlaubt
	var reg=/^(\s)*\d+(\s)*$/;
	if (!reg.exec(str)) {
		window.alert("Sorry, this is not a valid Number!");
		return false;
	}
	return true;
}

function checkFloat(str) {
	// (\s)* whitespaces vor zahl erlaubt
	// \d+ die zahl vor dem punkt
	// \. der punkt
	// \d+ die zahl nach dem punkt
	// (\s)* whitespaces nach zahl erlaubt
	var reg=/^(\s)*\d*\.?\d*(\s)*$/;
	if (!reg.exec(str)) {
		window.alert("Sorry, this is not a valid Number(example: 10.99)!");
		return false;
	}
	return true;
}
	
function checkISBN(str) {
	var reg = /^(\d)+(\s|-)(\d)+(\s|-)(\d)+(\s|-)(\d|X)$/;
	var reg1 = /^(.){13}$/
	if (!reg.exec(str) || !reg1.exec(str)) {
		window.alert("Sorry, this is not a valid ISBN!");
		return false;
	}
	/* calculate checksum
	multiply the first digit in the ISBN by 10, the second 
  by 9, the third by 8 and so on, to the 9th digit, which 
  you multiply by 2. Add these numbers together. This sum 
  plus the check digit must equal the next greatest multiple 
  of 11. If the sum is 10 less than that next greatest multiple, 
  use X (which thus stands for the "digit" 10).
	*/
	var reg = /^(\d+)(\s|-)(\d+)(\s|-)(\d+)+(\s|-)(\d|X)$/;
	reg.exec(str);
	//alert( + RegExp.$4 + RegExp.$5 + RegExp.$6 + RegExp.$7 + RegExp.$8 + RegExp.$9);
	var isbn = RegExp.$1 + RegExp.$3 + RegExp.$5;
	var check = RegExp.$7;
	var reg = /(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)/;
	reg.exec(isbn);
	//var sum = Integer(RegExp.$1);
	//alert(sum);
	//alert(RegExp.$1 + RegExp.$2 + RegExp.$3 + RegExp.$4 + RegExp.$5 + RegExp.$6 + RegExp.$7 + RegExp.$8 + RegExp.$9 + check);
	if (check == "X") check = 10
	var sum;
	sum = parseInt(RegExp.$1)*10 + parseInt(RegExp.$2)*9 + parseInt(RegExp.$3)*8 + parseInt(RegExp.$4)*7 + parseInt(RegExp.$5)*6 + parseInt(RegExp.$6)*5 + parseInt(RegExp.$7)*4 + parseInt(RegExp.$8)*3 + parseInt(RegExp.$9)*2 + parseInt(check);
	if (sum%11) {
		alert("Sorry, this is not a valid ISBN!");
		return false;
	}
	return true;
}

function checkISSN(str) {
	var reg = /^(\d)(\d)(\d)(\d)(\s|-)(\d)(\d)(\d)(\d|X)$/;
	var reg1 = /^(.){9}$/
	if (!reg.exec(str) || !reg1.exec(str)) {
		window.alert("Sorry, this is not a valid ISSN!");
		return false;
	}
	/* calculate checksum
	multiply the first digit in the ISBN by 8, the second 
  by 7, the third by 6 and so on, to the 7th digit, which 
  you multiply by 2. Add these numbers together. This sum 
  plus the check digit must equal the next greatest multiple 
  of 11. If the sum is 10 less than that next greatest multiple, 
  use X (which thus stands for the "digit" 10).
	*/
	var reg = /^(\d)(\d)(\d)(\d)(\s|-)(\d)(\d)(\d)(\d|X)$/;
	reg.exec(str);
	var check = RegExp.$9;
	if (check == "X") check = 10
	var sum;
	sum = parseInt(RegExp.$1)*8 + parseInt(RegExp.$2)*7 + parseInt(RegExp.$3)*6 + parseInt(RegExp.$4)*5 + parseInt(RegExp.$6)*4 + parseInt(RegExp.$7)*3 + parseInt(RegExp.$8)*2 + parseInt(check);
	if (sum%11) {
		alert("Sorry, this is not a valid ISSN!");
		return false;
	}
	return true;
}	

function checkEAN(str) {
	var reg = /^(\d|\s|\(|\))+$/;
	if (!reg.exec(str)) {
		window.alert("Sorry, this is not a valid EAN-Code!");
		return false;
	}
	return true;
}

function checkYEAR(str) {
	var reg = /^(\d)(\d)(\d)(\d)$/;
	if (!reg.exec(str)) {
		window.alert("Sorry, this is not a valid Year!");
		return false;
	}
	now = new Date();
	year = now.getYear();
	if (parseInt(str) > year+10 || parseInt(str) < 1000) {
		window.alert("Sorry, this is not a valid Year!");
		return false;
  }		
	return true;
}