function MM_openBrWindow(theURL,winName,features) {
  var win = window.open(theURL,winName,features);
  win.focus();
}

function setTitle(newTitle){
	parent.document.title = newTitle;
	document.title = newTitle;
}

function checkInString(instr){
	for (i = 0; i < instr.value.length; i++){
        c = instr.value.charAt(i);
        if (c == '"' || c == '<' || c == '>')
        {
          alert("You have used invalid characters.\nThese invalid characters are: \", <, >.\n");
          instr.focus();
          instr.select();
          return 0;
        }
    }

   return 1;
}

function selectTheRadioButton(radioName, radioNum, targetVal)
{
	for (i=0; i < radioNum; i++)
	{
		if (radioName[i].value == targetVal)
		{
			radioName[i].checked = true;
		}
	}
}

function saveInputValue(inputName, isChecked)
{
	if (isChecked)	inputName.value = "1";
	else			inputName.value = "0";
}

function selectTheDropdownEnt(selObj, targetVal) 
{
	for (i = 0; i < selObj.length; i ++)
	{
	    if (isNaN(targetVal))
		{
			if (selObj.options[i].value.toLowerCase() == targetVal.toLowerCase())
			{
				selObj.options[i].selected = true;
				break;
			}
		}
		else
		{
		if (selObj.options[i].value == targetVal)
		{
			selObj.options[i].selected = true;
			break;
		}
	}
}
}

function updateDynSelOpt(x, aList)
{
	var cnt;

	cnt = 0;
	for (i = 0; i < aList.length; i ++) {
		if (aList[i][0])	cnt ++;
	}

	if (cnt != 0) {
		x.length = cnt;
		j = 0;
		for (i = 0; i < aList.length; i ++) {
			if (aList[i][0]) {
				x.options[j].text = aList[i][1];
				x.options[j].value = aList[i][2];
				j ++;
			}
		}
	}
	return cnt;
}

function checkPort(thisObj, defaultPort, servName)
{
	if (checkPortDifferent(thisObj, servName) == -1)
        return -1;
	if (thisObj.value == defaultPort)
		return 0;
	if ((thisObj.value > 1024) && (thisObj.value <= 65535))
		return 0;
	alert(servName + " port must be " + defaultPort + " or from 1025 to 65535");
	thisObj.select();
	thisObj.focus();
	return -1;
}

function checkPortSimple(thisObj)
{
	if ((thisObj.value >= 0) && (thisObj.value <= 65535))
		return 0;
	alert("Port must be a number from 0 to 65535");
	thisObj.select();
	thisObj.focus();
	return -1;
}

// check all the server ports are different 
function checkPortDifferent(thisObj, servName)
{
    var h = 12, i = 0, j = 0, k = 12;
    var aPortNum = new Array (12);
/*  network_rtcpaudioport = parseInt(network_audioport) + 1;
    network_rtcpvideoport = parseInt(network_videoport) + 1;
    multicast_rtcpaudioport = parseInt(multicast_audioport) + 1;
    multicast_rtcpvideoport = parseInt(multicast_videoport) + 1; */

    aPortNum[0] = network_localftpport;
/*  aPortNum[1] = multicast_videoport;
    aPortNum[2] = multicast_audioport;
    aPortNum[3] = multicast_rtcpvideoport;
    aPortNum[4] = multicast_rtcpaudioport; */
    aPortNum[5] = network_audioport;
    aPortNum[6] = network_videoport;
/*  aPortNum[7] = network_rtcpaudioport;
    aPortNum[8] = network_rtcpvideoport; */
    aPortNum[9] = network_httpport;
    aPortNum[10] = network_rtspport;
   
    if (thisObj.name == "syslog_serverport")
    {
        return 0;    
    }
    else
{
    if(thisObj.name == "network_httpport")
    {
            j = 5;
            k = 9;
    }
    else if(thisObj.name == "network_rtspport")
    {
            j = 5;
            k = 10;
        }
        else if (thisObj.name == "network_audioport")
        {
            j = 5;
            k = 5;
        }
        else if (thisObj.name == "network_videoport")
        {
            j = 5;
            k = 6;
        }
/*      else if (thisObj.name == "network_rtcpaudioport")
        {
            j = 5;
            k = 7;
    }
        else if (thisObj.name == "network_rtcpvideoport")
        {
            j = 5;
            k = 8;
        } */
    else if(thisObj.name == "network_localftpport")
    {
            j = 11;
            k = 0;
        }
        else if (thisObj.name == "multicast_videoport")
        {
            j = 11;
            k = 1;
            h = 2;
        }
        else if (thisObj.name == "multicast_audioport")
        {
            j = 11;
        k = 2;
            h = 1;
    }
/*      else if (thisObj.name == "multicast_rtcpvideoport")
    {
            j = 11;
            k = 3;
            h = 4;
        }
        else if (thisObj.name == "multicast_rtcpaudioport")
        {
            j = 11;
            k = 4;
            h = 3;
        } */
        for (i = 0; i < j; i++)     // j indicates which ports need to be compared in order
        {
            if (i == k)     // aPortNum[k] indicates itself, need not to compare with itself
                continue; 
            else if (i == h)    // some ports already checked 'cause they are in the same page, if compare with old port number will cause error
            continue;            
        else if(thisObj.value == aPortNum[i])
        {
                alert("The " + servName + " port number you chose has been used.\nPlease check the port numbers and use a different port number.\n"); 
            return -1;
        }                
    }  
}
}

function checkemail(input){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(input.value))
		return true;
	else{
		alert("Please input a valid email address!");
		input.focus();
		input.select();
		return false;
	}
}

function checkIPaddr(input)
{
	var filter=/\d+\.\d+\.\d+\.\d+/;
	if (!filter.test(input.value)){
		alert("Please enter a valid IP address!");
		input.focus();
		input.select();
		return -1;
	}
	input.value = input.value.match(filter)[0];
	subip = input.value.split(".");
	for (i = 0; i < subip.length; i ++)
	{
		if ((parseInt(subip[i]) > 255) || (parseInt(subip[i]) < 0 ))
		{
			alert("Please enter a valid IP address!");
			input.focus();
			input.select();
			return -1;
		}
	}
	
	return 0;
}

function checkFilenameString(instr){
  for (i = 0; i < instr.value.length; i++){
    c = instr.value.charAt(i);
    if (c == '"' || c == '<' || c == '>' || c == '/' || c == ':' || c == '*' || c == '?' || c == '|' || c == '\\')
    {
       alert("You have used invalid characters. These invalid characters are: \\, /, :,*, ?, \", <, >, |.");
       instr.focus();
       instr.select();
       return 0;
    }
  }
  return 1;
}

function checkNumRange(thisObj, up, down, msg)
{
	if ((thisObj.value >= down) && (thisObj.value <= up))
		return 0;
	alert(msg);
	thisObj.select();
	thisObj.focus();
	return -1;
}

function checkHHMM(input)
{
	var filter=/\d\d:\d\d/;
	if (!filter.test(input.value)){
		alert("Please enter a valid time [hh:mm], like 12:00");
		input.focus();
		input.select();
		return -1;
	}
	input.value = input.value.match(filter)[0];
	sub_item = input.value.split(":");
	if ((parseInt(sub_item[0]) >= 24) || (parseInt(sub_item[1]) >= 60))
	{
		alert("Please enter a valid time [hh:mm], like 12:00");
		input.focus();
		input.select();
		return -1;
	}
	
	return 0;
}

// For debug
function showMe(mine, target)
{
	mine.value = target.value;
}

