VPNClientTableURL = new Array("VPNClientTable1.htm");

function CombineScheduleList(passForm,index)
{
	var schedule="";
	index -= 1;
	if ((passForm.policy_name.value!="") && 
		(passForm.days_8.checked==false)&&(passForm.days_1.checked==false)&&(passForm.days_2.checked==false)&&(passForm.days_3.checked==false) &&
		(passForm.days_4.checked==false)&&(passForm.days_5.checked==false)&&(passForm.days_6.checked==false)&&(passForm.days_7.checked==false)
		)
	{
		alert("Please select the date!");
		return 0;
	}		
	if (passForm.time[1].checked && (!CheckTimeRange(passForm))) return;
	//Day:if everyday then no other day	
	if (passForm.days_8.checked)
		schedule = "8:";
	else 
	{
		for (var i=1; i<8 ;i++)
			if (eval("passForm.days_"+i).checked)
			 schedule += (i + "-");

		if (schedule.charAt(schedule.length-1) == "-")
			schedule = schedule.substring(0,schedule.length-1);
		schedule += ":";
	}
	//Time:if 24hr then no other time		
	if (passForm.time[0].checked)
		schedule += ("24");
	else
	{
		schedule += ("0:" + passForm.start_hour.value 	+ ":");
		schedule += (passForm.start_min.value + ":0:");
		schedule += (passForm.end_hour.value 	+ ":");
		schedule += (passForm.end_min.value + ":");
	}
	//schedule += passForm.RuleName.value;	
	
	var schedulelist = passForm.ScheduleList.value.split("|");
	var schedules="";
	schedulelist[index] = schedule;	

	for (var i=0;i<10;i++)
	{
		if (schedulelist[i] == "" || schedulelist[i] == null)
			schedulelist[i]=0;
       if (i<9)			
		schedules += (schedulelist[i] + "|");
	   else 
	    schedules +=schedulelist[i]
	}
	document.forms[0].ScheduleList.value = schedules;
	
	return 1;
}

function CombineWeb(passForm, index, str)
{
	var webobj;
	var webui;
	var webfilter="";
	index -= 1;
		if (str == "url")
		{
			webobj = eval("document.forms[0].URL"+index);
			webui  = passForm.urladdr;
			for (var i=0; i < webui.length; i++)
				CheckURL(webui[i]);
		}
		else 
		{
			webobj = eval("document.forms[0].Text"+index);
			webui  = passForm.urlkeyword; 
		}
		for (var i=0; i < webui.length; i++)
		{
			if (webui[i].value=="")
				continue;
			if (!(CheckLegalChar(webui[i])))
				return 0;
			if(i<4)
				webfilter += (webui[i].value + "#");
			else 
				webfilter += (webui[i].value);		
		}
		webobj.value = webfilter;
	return 1;
}

function CheckURL(obj)
{
	var data = obj.value;
	data = data.replace(/http:\/\//g, "");
	data = data.replace(/\//g, "");
	obj.value = data;
}

function CheckLegalChar(obj)
{
	var data = obj.value;
	
	for (var i=0;i<data.length;i++)
	{
	 	if (((data.charAt(i)=='.')||(data.charAt(i)=='-')) && (obj.name == "urladdr"))//for URL		
	 		continue;	
		if (!((data.charAt(i)>='0') && (data.charAt(i)<='9')) &&
			!((data.charAt(i)>='a') && (data.charAt(i)<='z')) &&
			!((data.charAt(i)>='A') && (data.charAt(i)<='Z'))
			)
		{
			alert(mavis_str(Alert_Msg_27));
			//alert("Please enter legal character in [0-9 a-z A-Z]!");
			obj.focus();
			return 0;
		}
	}
	return 1;
}

function SelVPNClientTable(n)
{
	n -= 0 ;
	VPNClientTable.location.href = VPNClientTableURL[n];	
}

function CheckPortRange(start, end)
{
	var port;

	port = end.value - start.value;

	if(port < 0)
	{
		alert(mavis_str(Alert_Msg_28));
		//alert('Invalid Port Range: Start port number must be less than end port number.');
		start.focus();
		start.select();
		return false;
	}

	return true;
}
function CheckDay(passForm, index)
{
	if (index==8)
	{
		if (passForm.days[0].checked)
			for (var i=1;i<=7;i++)
				passForm.days[i].checked = false;
	}

	else
	{
		passForm.days[0].checked = false;
		
		var seven=1;
		
		for (var i=1;i<=7;i++)
			if (!passForm.days[i].checked)
			{
				seven=0;
				break;
			}
		if (seven)
		{
			passForm.days[0].checked = true;
			for (var i=1;i<=7;i++)
				passForm.days[i].checked = false;
		}
	}
		
}

function CheckTimeRange(passForm)
{
	var starthour = parseInt(passForm.start_hour.value, 10);
	var endhour = parseInt(passForm.end_hour.value, 10);	
	var startminute = parseInt(passForm.start_min.value, 10);
	var endminute = parseInt(passForm.end_min.value, 10);	
	if (starthour > endhour)
	{
		alert(mavis_str(Alert_Msg_60));
		//alert("End time must be bigger than begin time!");
		return 0;
	}
	else if ((starthour == endhour) && (startminute >= endminute))
	{
		alert(mavis_str(Alert_Msg_60));
		//alert("End time must be bigger than begin time!");
		return 0;
	}		
	return 1;
}

function CheckTimeRange_Summary(passForm, i)
{
	var starthour = parseInt(eval("passForm.start_hour"+i).value, 10);
	var endhour = parseInt(eval("passForm.end_hour"+i).value, 10);	
	var startminute = parseInt(eval("passForm.start_min"+i).value, 10);
	var endminute = parseInt(eval("passForm.end_min"+i).value, 10);	
	if (starthour > endhour)
	{
		alert(mavis_str(Alert_Msg_60));
		//alert("End time must be bigger than begin time!");
		return 0;
	}
	else if ((starthour == endhour) && (startminute >= endminute))
	{
		alert(mavis_str(Alert_Msg_60));
		//alert("End time must be bigger than begin time!");
		return 0;
	}		
	return 1;
}

function CheckTime(passForm)
{
	if 	(passForm.time[0].checked)
	{
		passForm.starthour.disabled = 1;
		passForm.startminute.disabled = 1;
		passForm.startm.disabled = 1;
		passForm.endhour.disabled = 1;
		passForm.endminute.disabled = 1;
		passForm.endm.disabled = 1;
	}
	else
	{
		passForm.starthour.disabled = 0;
		passForm.startminute.disabled = 0;
		passForm.startm.disabled = 0;
		passForm.endhour.disabled = 0;
		passForm.endminute.disabled = 0;
		passForm.endm.disabled = 0;	
	}	
}
function GetPortVar(portlist, str , index)
{
	var portservices = portlist.split(" ");
	var portservice = new Array();

	if (!isNaN(index))
	{
		portservice = portservices[index].split(":");
	}

	if (str=="len")
	{
		if ((portlist=="") || (portlist==null))
			return 0;
		return portservices.length;
	}
	else if (portlist=="" || index==null)
	{
		return "";
	}
	else if (str=="proto")
	{
		return portservice[0];
	}
	else if (str=="start")
	{
		return portservice[1];	
	}
	else if (str=="end")
	{
		return portservice[2];	
	}
	else if (str=="name")
	{
		return portservice[3];
		
	}
	else{return "";}
}

function SelPortFilter(passForm, index)
{
	var portservicelist = passForm.PortServiceList.value;
	var len = GetPortVar(portservicelist,"len");

	for (var i=0;i<len;i++)
	{
		
		if (passForm.portProtocol[index].options[passForm.portProtocol[index].selectedIndex].text == "None")
		{

			passForm.portStart[index].value = 0;
			passForm.portEnd[index].value = 0;
			return;
		}		
		if (passForm.portProtocol[index].options[passForm.portProtocol[index].selectedIndex].text == GetPortVar(portservicelist, "name", i))
		{

			passForm.portStart[index].value = GetPortVar(portservicelist, "start", i);
			passForm.portEnd[index].value = GetPortVar(portservicelist, "end", i);
			return;
		}
	}

}

function parsePortFilter(passForm, index)
{
	var portservicelist = passForm.PortServiceList.value;
	for (var i=0;i<passForm.portProtocol.length;i++)
	{
		passForm.portProtocol[0].remove(0);
		passForm.portProtocol[1].remove(0);
	}	
	var item1 = new Option("None", -1);
	var item2 = new Option("None", -1);
	passForm.portProtocol[0].options[0] = item1;
   	passForm.portProtocol[1].options[0] = item2;
   	var len = GetPortVar(portservicelist,"len");
	for (var i=0;i < len;i++)
	{
        item1 = new Option(GetPortVar(portservicelist,"name", i), i);
        item2 = new Option(GetPortVar(portservicelist,"name", i), i);
    	passForm.portProtocol[0].options[i+1] = item1;
    	passForm.portProtocol[1].options[i+1] = item2;
	}
	var portfilterlist 	= eval("passForm.PortFilter"+index).value;	
	if (portfilterlist == "") return;
	var portfilters    	= portfilterlist.split(" ");	
	
	for (var i=0;i<2;i++)
	{
		var portfilter	= portfilters[i].split(":");	
		for (var j=0;j<len;j++)
		{
			if ((portfilter[0] == GetPortVar(portservicelist,"proto", j)) &&
				(portfilter[1] == GetPortVar(portservicelist,"start", j)) &&
				(portfilter[2] == GetPortVar(portservicelist,"end", j))
				)
			{
				passForm.portProtocol[i].selectedIndex = j + 1;
				passForm.portStart[i].value = GetPortVar(portservicelist,"start", j);
				passForm.portEnd[i].value = GetPortVar(portservicelist,"end", j);
				break;
			}
			
		}
	}
}

function CombinePortFilter(passForm, filterindex)
{
	var portservicelist = passForm.PortServiceList.value;
	var portfilters = "";
	var len = GetPortVar(portservicelist,"len");

	for (var index=0;index<2;index++)
	{
	for (var i=0;i<len;i++)
	{
		if ((passForm.portProtocol[index].value == -1))
		{
			portfilters += "-1:0:0";
			break;
		}
		if (passForm.portProtocol[index].options[passForm.portProtocol[index].selectedIndex].text == GetPortVar(portservicelist,"name",i))
		{
			portfilters += (GetPortVar(portservicelist,"proto",i) + ":" + 
							GetPortVar(portservicelist,"start",i) + ":" + 
							GetPortVar(portservicelist,"end",i) );
			break;
		}
	}
	if (index!=1)
		portfilters+=" ";
	}
	eval("passForm.PortFilter"+filterindex).value = portfilters;

}

function SelRule(passForm)
{
	var index = passForm.RuleID.selectedIndex;
	
	passForm.RuleName.value="";
	for (var i=0;i<8;i++)
		passForm.days[i].checked = false;
	passForm.time[0].checked = false;
	passForm.time[1].checked = false;
	passForm.starthour.selectedIndex = 0;
	passForm.startminute.selectedIndex = 0;
	passForm.startm.selectedIndex = 0;
	passForm.endhour.selectedIndex = 0;
	passForm.endminute.selectedIndex = 0;
	passForm.endm.selectedIndex = 0;	
	parseScheduleList(passForm, index);
	//ip and mac filter
	ShowInternetFilter(passForm, index);
	//web filter
	if (passForm.FilterType.selectedIndex == 0)
	{
		ShowWeb(passForm, index, "url");
		ShowWeb(passForm, index, "text");
	}
	CheckTime(passForm, index);
	//port filter
	for (var i=0;i<2;i++)
	{
		passForm.portProtocol[i].selectedIndex = 0;
		passForm.portStart[i].value = 0;
		passForm.portEnd[i].value = 0;
	}

	parsePortFilter(passForm, index);
}

function parseScheduleList(passForm, index)
{
	var schedulelist = passForm.ScheduleList.value.split("|");
	var schedulename = passForm.ScheduleName.value.split("#");
	//No Rule, every day, 24hr as default
	if ((passForm.ScheduleList.value=="") || (schedulelist[index] == 0) || (schedulelist[index] == null))
	{
		passForm.days[0].checked = true;
		passForm.time[0].checked = true;
		return 1;
	}

	var schedule= schedulelist[index].split(":");
	
	//Day:if everyday then no other day
	var days	= schedule[0].split("-");
	for (var i=0;i<days.length;i++) {
		if (days[i] == 8) {
			passForm.days[0].checked = true;
			break;
		}
		if (days[i] == 7)
			passForm.days[1].checked = true;
		else{
			passForm.days[parseInt(days[i],10)+1].checked = true;
		}


	}
	//Time:if 24hr then no other time
	if (schedule[1] == 2)
	{
		passForm.time[0].checked = true;
	}
	else
	{
		passForm.time[1].checked = true;
		passForm.startm.selectedIndex = schedule[1];
		for (var i=0;i<passForm.starthour.length;i++)
			if (passForm.starthour.options[i].text == schedule[2])
			{
				passForm.starthour.selectedIndex = i;
				break;
			}
		for (var i=0;i<passForm.startminute.length;i++)
			if (passForm.startminute.options[i].text == schedule[3])
			{
				passForm.startminute.selectedIndex = i;
				break;
			}					
		
		passForm.endm.selectedIndex = schedule[4];				
		for (var i=0;i<passForm.endhour.length;i++)
			if (passForm.endhour.options[i].text == schedule[5])
			{
				passForm.endhour.selectedIndex = i;
				break;
			}
		for (var i=0;i<passForm.endminute.length;i++)
			if (passForm.endminute.options[i].text == schedule[6])
			{
				passForm.endminute.selectedIndex = i;
				break;
			}					
	}
	if (schedulename[index]==null || schedulename[index]=="" || schedulename[index]=="---")
		passForm.RuleName.value="";
	else
		passForm.RuleName.value = schedulename[index];	
	return 1;
}


function DeleteRule(passForm)
{
	var rulelist = passForm.ScheduleList.value.split("|");
	var namelist = passForm.ScheduleName.value.split("#");
	var rules="";
	var names="";
	var index =passForm.RuleID.selectedIndex;
/*	
	if (rulelist[index] == "0" || rulelist[index] == null)
		return 0;
*/
	rulelist[index] = 0;	
	namelist[index] = "---";
	for (var i=0;i<10;i++)
	{
		
		rules += (((rulelist[i]==null)?0:rulelist[i]) + "|");
		names += (((namelist[i]==null)?"---":namelist[i]) + "#");
	}

	
	if (rules.charAt(rules.length-1)=="|")
		rules = rules.substring(0, rules.length-1);
	if (names.charAt(names.length-1)=="#")
		names = names.substring(0, names.length-1);
	passForm.ScheduleList.value = rules;
	passForm.ScheduleName.value = names;
	ShowRuleList(passForm);	
	passForm.RuleName.value="";
	for (var i=0;i<8;i++)
		passForm.days[i].checked = false;
	passForm.days[0].checked = true;
	passForm.time[0].checked = true;
	passForm.time[1].checked = false;
	CheckTime(passForm);
	CheckDay(passForm,8);
	
	passForm.starthour.selectedIndex = 0;
	passForm.startminute.selectedIndex = 0;
	passForm.startm.selectedIndex = 0;
	passForm.endhour.selectedIndex = 0;
	passForm.endminute.selectedIndex = 0;
	passForm.endm.selectedIndex = 0;
	passForm.filterStatus[1].checked = true;
	passForm.filterAllow[0].checked = true;
	for (var i=0;i<passForm.urladdr.length;i++)
		passForm.urladdr[i].value="";
	for (var i=0;i<passForm.urlkeyword.length;i++)
		passForm.urlkeyword[i].value="";
	eval("passForm.URL"+index).value="";
	eval("passForm.Text"+index).value="";
	eval("passForm.IPFilter"+index).value="";
	eval("passForm.MACFilter"+index).value="";
	for (var i=0;i<2; i++)
	{
		passForm.portProtocol[i].selectedIndex = 0;
		passForm.portStart[i].value = 0;
		passForm.portEnd[i].value = 0;
	}

	eval("passForm.PortFilter"+index).value="";
	
	passForm.filter_SchedularWebFilter.value=0;	
	for (var i=0;i<10;i++)
		if ((eval("passForm.URL"+i).value!="") || (eval("passForm.Text"+i).value!=""))
		{
			passForm.filter_SchedularWebFilter.value=1;	
			break;
		}
//	passForm.submit();	
}

function ShowInternetFilter(passForm, index)
{
	var filterStatusList = passForm.FilterStatuss.value;
	var filterAllowList= passForm.FilterAllows.value;
	var filterStatus = filterStatusList.split("|");
//	var filterAllow  = filterAllowList.split("|");
	
	if	(filterStatus[index] == 1)
		passForm.filterStatus[0].checked = true;		
	else
		passForm.filterStatus[1].checked = true;		
				
//	if	(filterAllow[index] == 1)
//		passForm.filterAllow[1].checked = true;		
//	else
//		passForm.filterAllow[0].checked = true;		
		
	return 1;	
}

function CombineInternetFilter(passForm, index)
{
	
	var filterStatus = passForm.FilterStatuss.value.split("|"); 
	var filterAllow  = passForm.FilterAllows.value.split("|"); 

	var filterStatusList="";
	var filterAllowList="";
	index-=1;

	if (passForm.filterStatus[1].checked)
	{
		filterStatus[index] = 0;
		filterStatus[index] = 0;
	}
	else
	{
		filterStatus[index] = 1;
		filterStatus[index] = 1;	
	}
	
	if (passForm.filterAllow[0].checked)
	{
		filterAllow[index] = 0;
		filterAllow[index] = 0;
	}
	else
	{
		filterAllow[index] = 1;
		filterAllow[index] = 1;	
	}
	
	for(var i=0;i<10;i++)
	{
		if (filterStatus[i] == null || filterStatus[i] =="")
			filterStatus[i] = 0;
		if (filterAllow[i] == null || filterAllow[i] == "")
			filterAllow[i] = 0;				
		if (i!=9)
		{
			filterStatusList += (filterStatus[i]+"|");
			filterAllowList  += (filterAllow[i]+"|");

		}
		else
		{
			filterStatusList += (filterStatus[i]+"|");
			filterAllowList  += (filterAllow[i]+"|");
		}
	}
	passForm.FilterStatuss.value = filterStatusList;
	passForm.FilterAllows.value = filterAllowList;	

}

function SavePolicy(passForm)
{
	var index = passForm.RuleID.selectedIndex;
	//schedule must save first	
	if (!CombineScheduleList(passForm, index))
		return;
	
	CombineInternetFilter(passForm, index);
	if (!CombineWeb(passForm, index, "url"))
		return;
	if (!CombineWeb(passForm, index, "text"))
		return;

	ShowRuleList(passForm);
	SelRule(passForm);
	//web filter 
	passForm.filter_SchedularWebFilter.value=0;	
	for (var i=0;i<10;i++)
		if ((eval("passForm.URL"+i).value!="") || (eval("passForm.Text"+i).value!=""))
		{
			passForm.filter_SchedularWebFilter.value=1;	
			break;
		}
	alert(mavis_str(Alert_Msg_64));
	//alert("Policy saved!");
}

function ShowWeb(passForm, index, str)
{

	var webobj;
	var webui;
	index -= 1;
	if (str == "url")
	{
		webobj = eval("passForm.URL"+index);
		webui  = passForm.urladdr;
	}
	else
	{
		webobj = eval("passForm.Text"+index);
		webui  = passForm.urlkeyword;
	}
	
	webfilter = webobj.value.split("#");	
	for	(var i=0;i<webui.length; i++)
	{
		webui[i].value = "";
	}
	
	for (var i=0;i<webfilter.length-1; i++)
	{
		if (webfilter[i]==null)
			break;
			
		webui[i].value = webfilter[i];
	}		
}

function CombineFilterType(passForm, index)
{
	var filtertype = passForm.FilterTypes.value.split("|");
	var filtertypes="";
	
	filtertype[index] = passForm.FilterType.selectedIndex;
	for (var i=0; i<10; i++)
	{
		if (filtertype[i]==null)
			filtertype[i] = 0;
		filtertypes += (filtertype[i] + "|");
	}
	if (filtertypes.charAt(filtertypes.length-1)=="|")
		filtertypes = filtertypes.substring(0, filtertypes.length-1);
	passForm.FilterTypes.value = filtertypes;
}

function GetFilterType(passForm, index)
{
	var filtertype = passForm.FilterType.value.split("|");
	
	if ((filtertype[index] == null) || (filtertype[index] == 0))
		return 0;
	return filtertype[index];
}

function SelFilterType(passForm, index)
{
	if (isNaN(index))
	{
		document.dummy.filter_index.value = passForm.RuleID.selectedIndex;
		document.dummy.filter_type.value = passForm.FilterType.selectedIndex;	
	}
	else
	{
		document.dummy.filter_index.value = index;
		var filtertype = passForm.FilterTypes.value.split("|");		
		document.dummy.filter_type.value = filtertype[passForm.RuleID.selectedIndex];		
	}
	passForm.RuleID.disabled = 1;
	passForm.FilterType.disabled=1;
	document.dummy.submit();
}
