/*
 show time :time colock 
*/
function showtime(s,index)
{
    var now = new Date();
    now.setYear(__year);
    now.setMonth(__month-1);
    now.setDate(__date);
    now.setHours(__hour);
    now.setMinutes(__min);
    now.setSeconds(s);
    var year = now.getFullYear();
    var month = now.getMonth()+1;
    var date = now.getDate();
    var hours = now.getHours()
    var minutes = now.getMinutes()
    var seconds = now.getSeconds()

    var month_1 = new Array();
    month_1[0] = "Jan";
    month_1[1] = "Feb";
    month_1[2] = "Mar";
    month_1[3] = "Apr";
    month_1[4] = "May";
    month_1[5] = "Jun";
    month_1[6] = "Jul";
    month_1[7] = "Aug";
    month_1[8] = "Sep";
    month_1[9] = "Oct";
    month_1[10] = "Nov";
    month_1[11] = "Dec";
    
    var timeValue = "";
    timeValue += ((date < 10) ? "0" : "") + date ;
    
    timeValue += ((month < 10) ? " " : " ") + month_1[month-1] ;
    timeValue += " "+year;
    
    timeValue += " ";
    timeValue  += ((hours > 12) ? hours - 12 : hours)
    timeValue  += ((minutes < 10) ? ":0" : ":") + minutes
    timeValue  += ((seconds < 10) ? ":0" : ":") + seconds
    timeValue  += (hours >= 12) ? " P.M." : " A.M."
    
    document.getElementById('timeclock').innerHTML = timeValue;
    s++;
    
    timerID = setTimeout("showtime("+s+","+index+")",1000)
}
function GetPCTime(i)
{
    var now = new Date();      
    document.forms[i].s_year.value = now.getFullYear(); 
    document.forms[i].s_month.value = now.getMonth()+1;
    document.forms[i].s_date.value = now.getDate();
    document.forms[i].s_hour.value = now.getHours();
    document.forms[i].s_min.value = now.getMinutes();
    document.forms[i].s_sec.value = now.getSeconds();
}
function disable(eid)
{
    if (document.getElementById(eid) == null) return;
       document.getElementById(eid).disabled=true
}
function enable(id)
{
    document.getElementById(id).disabled=false;
}
function disable_box(eid)
{
    for (i=0;i<eid.getElementsByTagName("input").length;i++)
    {
        eid.getElementsByTagName("input")[i].disabled = true;        
    }
    for (i=0;i<eid.getElementsByTagName("select").length;i++)
    {
        eid.getElementsByTagName("select")[i].disabled = true;        
    }
}
function enable_box(eid)
{
    for (i=0;i<eid.getElementsByTagName("input").length;i++)
    {
        eid.getElementsByTagName("input")[i].disabled = false;        
    }
    for (i=0;i<eid.getElementsByTagName("select").length;i++)
    {
        eid.getElementsByTagName("select")[i].disabled = false;          
    }
    
}
function show(eid) 
{
    if (document.getElementById(eid) == null) return;
       document.getElementById(eid).style.display = '';  
}
function hide(eid) 
{
    if (document.getElementById(eid) == null) return;
       document.getElementById(eid).style.display = 'none';
}
function time_check(time_str)
{
    var mt = time_str.match(/^(\d{1,2})\:(\d{1,2})\:(\d{1,2})$/);
    //var mt = time_str.split(":");
    if (!mt ||mt[1] < 0 && mt[2] < 0 && mt[3] < 0)
       return 1;
    if (!mt || mt[1] > 23 || mt[2] > 59 || mt[3] > 59)
       return 1;
    else
       return parseInt(mt[1],10)+ ":"+parseInt(mt[2],10)+":"+parseInt(mt[3],10);
}
function devip_check(ip_str)
{
    var mt = ip_str.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
    //var mt = ip_str.split(".");
    if (!mt || mt[1] >= 255 && mt[2] >= 255 && mt[3] >= 255 && mt[4] >= 255)
       return 1;
    if (!mt ||mt[1] <= 0 && mt[2] <= 0 && mt[3] <= 0 && mt[4] <= 0)
       return 1;
    if (!mt || mt[1] < 1 || mt[1] > 223)
       return 1;
    if (!mt || mt[1] > 255 || mt[2] > 255 || mt[3] > 255 || mt[4] > 255)
       return 1;
    else
       return parseInt(mt[1],10)+ "."+parseInt(mt[2],10)+"."+parseInt(mt[3],10)+"."+parseInt(mt[4],10);
}
function ip_check(ip_str)
{
    var mt = ip_str.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
    //var mt = ip_str.split(".");
    if (!mt || mt[1] >= 255 && mt[2] >= 255 && mt[3] >= 255 && mt[4] >= 255)
       return 1;
    if (!mt ||mt[1] <= 0 && mt[2] <= 0 && mt[3] <= 0 && mt[4] <= 0)
       return 1;
    if (!mt || mt[1] > 255 || mt[2] > 255 || mt[3] > 255 || mt[4] > 255)
       return 1;
    else
       return parseInt(mt[1],10)+ "."+parseInt(mt[2],10)+"."+parseInt(mt[3],10)+"."+parseInt(mt[4],10);
}
function ip_same_net(ip,netmask,gateway)
{
    var mt1 = ip.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
    var mt2 = netmask.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
    var mt3 = gateway.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
    
    if (((mt1[1]&mt2[1]) == (mt2[1]&mt3[1])) && ((mt1[2]&mt2[2]) == (mt2[2]&mt3[2])) && ((mt1[3]&mt2[3]) == (mt2[3]&mt3[3])) && ((mt1[4]&mt2[4]) == (mt2[4]&mt3[4])))
       return 0;
    else
       return 1;
}

function check_mask(ip,netmask)
{
    var mt1 = ip.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
    var mt2 = netmask.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/);
    var chk1=255-mt2[1],chk2=255-mt2[2],chk3=255-mt2[3],chk4=255-mt2[4];
    
    if (((mt1[1]&chk1) == 0) && ((mt1[2]&chk2) == 0) && ((mt1[3]&chk3) == 0) && ((mt1[4]&chk4) == 0))
       return 1;
    if (((mt1[1]&chk1) == chk1) && ((mt1[2]&chk2) == chk2) && ((mt1[3]&chk3) == chk3) && ((mt1[4]&chk4) == chk4))
       return 1;
    return 0;
}
function checkIntRange(i,max,min)
{
    if ((isNaN(i)) || (i>max) || (i<min))
       return 1;
    if (parseInt(i) != i)
       return 1;
    return 0;
}
function CheckHex(str)
{
//  var mt = str.match(/[^\xhex]/);
    var mt = str.match(/[^a-fA-F0-9]/);
    if (mt)
       return 1
    else 
       return 0;
}

function CheckWPAKey(Key)
{
    if ((Key.length < 8) || (Key.length > 64))
       return 1;
    if (Key.length == 64)
    {
       if (CheckHex(Key) == 1)
          return 1;
       else
          return 0;
    }
    return 0;
}

function filename_check(str)
{
    var mt = str.match(/[^a-zA-Z0-9_-]/);
    if (mt)
       return 1;
    else
       return 0;
}

function getHEXString(instr)
{
   var chrsz = 8;  /* bits per input character. 8 - ASCII; 16 - Unicode */
   var mask = (1 << chrsz) - 1;
   var hex_tab = "0123456789ABCDEF";
   var outstr = "";
   var i,j;

   for (i=0;i<instr.length;i++)
   {
       j = instr.charCodeAt(i);
       k = (j&0xf0)>> 4;
       outstr += hex_tab.charAt(k);
       l = (j&0x0f);
       outstr += hex_tab.charAt(l);
   }
   return outstr;
}