
function verifyIP (IPvalue,name) {
	errorString = "";
	theName = name;

	var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var ipArray = IPvalue.match(ipPattern);

	if (ipArray == null)
		errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
	else {
		for (var i = 1; i <= 4; i++) {
			thisSegment = ipArray[i];
			if (thisSegment > 255) {
				errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
				i = 5;
			}
		}
	}
	if (errorString == ""){
		return true;
	}else{
		alert (errorString);
		return false;
	}
}

function check_reg_intervall(val,reg){
	if (!reg) return true;
	if (!verify_number(val, 'Registration intervall')) return false;
	if (val>99999){
		alert("Registration intervall must be a number between 0 and 99999");
		return false;
	}else{
		return true;
	}
}

function check_pin(val){
	if (val.match('^[0-9]{4,4}?$') == null && val!="") {
		alert("Invalid PIN number");
		return false;
	}else{
		return true;
	}
}

function check_smsc(val){
	if (val.match('^\\+?[0-9]{1,70}?$') == null  && val!="") {
		alert("Invalid SMS center number");
		return false;
	}else{
		return true;
	}
}

function check_phone_number(val){
	if (val.match('^\\+?[0-9]{1,70}?$') == null  && val!="") {
		alert("Invalid number");
		return false;
	}else{
		return true;
	}
}

function countAndLimitChars( divId, textareaId, name ) {

    // TextArea auslesen
    var text = document.getElementById( textareaId ).value;

    // TextArea auf 670 Zeichen beschränken
    if( text.length > 670 ) {
        text = text.substring( 0, 670 );
        document.getElementById( textareaId ).value = text;
    }

    // Zeichen der TextArea zählen
    var rest = 670 - text.length;
    var str = name + ': ' + rest;
    document.getElementById( divId ).innerHTML = str;
}

function checkvlan(ch,val){
	if (ch){
		if (!confirm("Do you want to save this settings? beroFix will not be longer be accesible via network")) return false;
		if (val>=1 && val<=4094) return true;
		else{
			alert("VLAN id must be a number between 1 and 4094");
			return false;
		}
	}else return true;

}

function sip_general_val(bind_port,rtp_range){
	var ranges=rtp_range.split(',');
	var count=0;
	for (i=0; i<ranges.length;i++){
		if (!ranges[i].match(/^\d+-?\d+$/)){
			alert('RTP port range ist false');
			return false;
		}
		var values=ranges[i].split("-");
		if (values[0]<bind_port && bind_port<=values[1]){
			alert('The Bind Port is false');
			return false;
		}
		if (values.length>1) count+=values[1]-values[0];
		else count+=1;
	}

	if (count<240 || !isNaN(rtp_range)){
		alert('The number of RTP ports must be at least 240');
		return false
	}
	return true;
}

function testLength(value,len,name){
	if(value.length>len){
		alert(name +' can have a maximum of ' + len + ' characters');
		return false;
	}
	return true;
}

function verifyUsername(value,session){
	if (value=="beroAdmin" && session!="beroAdmin"){
		alert("The username can not be beroAdmin");
		return false;
	}
	return true;
}

function specChar(value,name)  {
	var iChars = "!@#$%^&*()+=[]\';,/{}|\"<>?";
	for (var i = 0; i < value.length; i++) {
		if (iChars.indexOf(value.charAt(i)) != -1)
		{
			alert(name+" contains special characters. These are not allowed. Please remove them and try again.");
			return false;
		}
	}
	return true;
}


function limited(text,max)
{
	if (text.value.length>=max){
		text.value=text.value.substring(0,max);
	}
	document.getElementById("zz").innerHTML = text.value.length + ", Rest " + (max - text.value.length);
}

function is_not_empty(ftext, name){
	if (ftext==""){
		alert("Please fill the " + name + " field.");
		return false;
	}else{
		return true;
	}
}

function isValidMacAddress(macAdd)
 {
  var RegExPattern = /^([0-9a-f]{2}([:-]|$)){6}$|([0-9a-f]{4}([.]|$)){3}$/i;

  if (RegExPattern.test(macAdd))
  {
   return true;
  }
  else
  {
	  alert("Invalid Media Access Control Address");
   return false;

  }
 }

function verify_number(ftext, name){
 	if (is_not_empty(ftext, name)){
 	 	if (isNumber(ftext)){
 	 		return true;
		}else{
			alert(name + " must be a number.");
			return false;
		}
	}else{
		return false;
	}
 	return false;
}

function verify_range(ftext, name){
	if (is_not_empty(ftext, name)){
		if (ftext>=0 && ftext<=32){
			return true;
		}else{
			alert(name + " must be a number between 0 and 32");
			return false;
		}
	}else{
		return false;
	}
}
function isNumber(str) {
  for(var position=0; position<str.length; position++){
	var chr = str.charAt(position)
        if  ( (chr < "0") || (chr > "9") )
              return false;
  };
  return true;
};

function check_blank(val, name){
	if (val.indexOf(" ")!=-1){
		alert ("The " + name + " may not contain blanks");
		return false;
	}
	return true;
}
