function openNewWin(strUrl, strHeigh, strWidth)
{
        self.open(strUrl,"","toolbar=0,status=0,menubar=0,top=50,left=100,height=" + strHeigh + ",width=" + strWidth + ",resizable=0,scrollbars=1");
}

function CloseWin()  
{  
    try
    { 
        window.opener=null;    
        window.open("", "_self");   
    }
    catch(e)
    {}
    
    window.close(); 
} 

var ns6=document.getElementById&&!document.all

function restrictinput(maxlength,e,placeholder)
{
        if (window.event&&event.srcElement.value.length>=maxlength)
        return false;
        else if (e.target&&e.target==eval(placeholder)&&e.target.value.length>=maxlength)
        {
                var pressedkey=/[a-zA-Z0-9\.\,\/]/; //detect alphanumeric keys
                if (pressedkey.test(String.fromCharCode(e.which)))
                e.stopPropagation();
        }
}

function countlimit(maxlength,e,placeholder)
{
        var theform=eval(placeholder);
        var lengthleft=maxlength-theform.value.length;
        var placeholderobj=document.all? document.all[placeholder] : document.getElementById(placeholder);
        if (window.event||e.target&&e.target==eval(placeholder))
        {
                if (lengthleft<0)
                        theform.value=theform.value.substring(0,maxlength);
                        placeholderobj.innerHTML=lengthleft;
        }
}

function displaylimit(theform,thelimit)
{
        var limit_text='<b><span id="'+theform.toString()+'">'+thelimit+'</span></b> characters remaining on your input limit <b>(No HTML tags)</b>.';

        if (document.all||ns6)
                document.write(limit_text);
        if (document.all)
        {
                eval(theform).onkeypress=function(){ return restrictinput(thelimit,event,theform);}
                eval(theform).onkeyup=function(){ countlimit(thelimit,event,theform);}
        }
        else if (ns6)
        {
                document.body.addEventListener('keypress', function(event)
                 { restrictinput(thelimit,event,theform) }, true);
                        document.body.addEventListener('keyup', function(event)
                         { countlimit(thelimit,event,theform) }, true);
        }
}

function splitURL(strUrl)
{
    if (strUrl == "http://") { return false; }
    
     var preFix = strUrl.substr(0,7);
     var preFixFinal = preFix.toLowerCase();
     //var len = strUrl.length;

     if (preFixFinal == "http://")
     {
         return true;
     }
     else
     {
         return false;
     }
}



function selectedDropDownMenu(ctrlid, ctrlval)
{
    var lentype = document.getElementById(ctrlid).length;
    ctrlval--;
    for(var j=0; j<lentype; j++)
    {    
       if(j == ctrlval)
       {  
          //document.all[ctrlid][j].selected = true;
          document.getElementById(ctrlid)[j].selected = true;
       }
    }       
}

function selectedDropDownMenuValue(ctrlid, ctrlval)
{
    //var lentype = document.all[ctrlid].length;
             
    lentype = document.getElementById(ctrlid).length;      

    for(var j=0; j<lentype; j++)
    {  
       //if(document.all[ctrlid][j].value == ctrlval)
       if (document.getElementById(ctrlid)[j].value == ctrlval)
       {     
          //document.all[ctrlid][j].selected = true;
          document.getElementById(ctrlid)[j].selected = true;
       }
    }    
}

function selectedCheckboxValue(ctrlid, ctrlval)
{
    try
    {
        var obj = document.getElementsByName(ctrlid);
        var lentype = obj.length;     
                  
        for(var j=0; j<lentype; j++)
        {  
        
           if(ctrlval.indexOf(obj[j].value) >= 0)
           {     
              obj[j].checked = true;
           }
        } 
    }
    catch(e)   
    {
    }
}

function selectedRadioButtonValue(ctrlid, ctrlval)
{
    if (ctrlval == null) return;
    
    var lentype = document.all[ctrlid].length;     

    for(var j=0; j<lentype; j++)
    {  
       if(document.all[ctrlid][j].value == ctrlval)
       {     
          document.all[ctrlid][j].checked = true;
       }
    }    
}

function getRadioButtonSelectedValue(ctrlid)
{
    try
    {
        var getval="";
        var obj = document.getElementsByName(ctrlid);
        var lentype = obj.length;     

        for(var j=0; j<lentype; j++)
        {  
           if(obj[j].checked)
           {     
              getval = obj[j].value;
           }
        } 

        return getval;
    }
    catch(e)   
    {
        return "";
    }
}

function clean_DropDownMenu(ctrlid)
{
    var obj = document.getElementById(ctrlid); 
    
    for (var i=0; i<obj.length; i++) 
    {  
        obj.remove(i);
        i--;
    }    
}

function getCheckboxSelectedValue(ctrlid)
{
    try
    {
        var getval="";
        var obj = document.getElementsByName(ctrlid);
        var lentype = obj.length;     
                  
        for(var j=0; j<lentype; j++)
        {  
           if(obj[j].checked)
           {     
              getval += (getval=="") ? obj[j].value : "," + obj[j].value;
           }
        } 
       
        return getval;
    }
    catch(e)   
    {
        return "";
    }
}

function getDropDownMenuSelectedValue(ctrlid)
{
    try
    {
        var getval="";
        var obj = document.getElementById(ctrlid);
        var lentype = obj.length;     

        for(var j=0; j<lentype; j++)
        {  
           if(obj[j].selected)
           {     
              getval += (getval=="") ? obj[j].value : "," + obj[j].value;
           }
        } 

        return getval;
    }
    catch(e)   
    {
        return "";
    }
}

function getListboxValue(ctrlid)
{
    try
    {
        var getval="";
        var obj = document.getElementById(ctrlid);
        var lentype = obj.length;     
      
        for(var j=0; j<lentype; j++)
        {       
            getval += (getval=="") ? obj[j].value : "," + obj[j].value;
        } 

        return getval;
    }
    catch(e)   
    {  
        return "";
    }
}

function getDropDownMenuSelectedCount(ctrlid)
{
    try
    {
        var getval = 0;
        var obj = document.getElementById(ctrlid);
        var lentype = obj.length;     

        for(var j=0; j<lentype; j++)
        {  
           if(obj[j].selected)
           {     
              getval++;
           }
        } 

        return getval;
    }
    catch(e)   
    {
        return 0;
    }
}

function stripSlashes(val)
{
    val = val.replace(/\&/g, "|");
    val = val.replace(/\'/g, "*");
    return val;
}

function ButtonStatus(id, truefalse)
{
    document.getElementById(id).disabled = truefalse;
}

function DisableRadioButton(ctrlid, truefalse)
{

    try
    {
        var obj = document.getElementsByName(ctrlid);
        var lentype = obj.length;     

        for(var j=0; j<lentype; j++)
        {   
            obj[j].disabled = truefalse;
        } 
    }
    catch(e)   
    {
    }
    
    
    //document.all[ctrlid].disabled = truefalse;
}

function isIE(){ //ie? 
    if (window.navigator.userAgent.indexOf("MSIE")>=1) 
        return true; 
    else 
        return false; 
} 

function ConfirmMsg()
{
    yy=confirm("Are you sure you want to delete this record?")
    if (yy==true)
    {
        return true;
    }
    else
    {
      return false;
    }
} 

function Check_Box()
{
    try
    {    
        var obj = document.getElementById("form");
        var checkedval = false;
        var cbxindex = 0;
        
        for(var i=0; i<obj.length; i++)
        { 
            if(obj[i].type == 'checkbox')     
            {                        
                var chkbox = obj[i].checked;
               
                if (chkbox)
                { 
                    checkedval = true;
                }
                
                cbxindex++;
            }
        }
        
        return checkedval;
    
    }
    catch(e)
    {
        //alert("common:"+e);
    }    
}
    
function validate(ctrl)
{
    
    str=new String();
    str=ctrl.value;

    var CurrChar = str.substring(str.length-1, str.length);
    var v = parseInt(CurrChar,10);

    if (/\D+/g.test(v))
    {
        ctrl.value="";
        ctrl.value=str.substring(0, str.length-1);
        ctrl.focus();
    }
    
}

function StripSlashes(ctrl)
{
    
    str=new String();
    str=ctrl.value;

    var CurrChar = str.substring(str.length-1, str.length);
    if ("'" == CurrChar)
    {
        var v = parseInt(CurrChar,10);
        if (/\D+/g.test(v))
        {
            ctrl.value="";
            ctrl.value=str.substring(0, str.length-1);
            ctrl.focus();
            divShow(ctrl.name + ' Must be Numeric.',ctrl); 
        }
    }
    else
    {
        divHidden();
    }                
}   

function isNumeric(ctrl)
{
    var str = new String();
    str = ctrl.value;
    var CurrChar = str.substring(str.length-1, str.length);
    var v = parseInt(CurrChar,10);    
    
    if (/\D+/g.test(v))
    {
        ctrl.value="";
        //ctrl.value = str.substring(0, str.length-1);    
        ctrl.focus();
        divShow('[' + ctrl.name + '] ' + msg_isNumeric, ctrl);            
        return false;
    }
    else
    {
        divHidden();
        return true;
    }
}

function isFloat(ctrl)
{
    var str = new String();
    str = ctrl.value;
    var CurrChar = str.substring(str.length-1, str.length);
    var v = parseInt(CurrChar,10);     
  
    if (CurrChar != '.' && /\D+[^\.]/g.test(v))
    {
        ctrl.value="";
        ctrl.value = str.substring(0, str.length-1); 
        ctrl.focus();
        divShow('[' + ctrl.name + '] ' + msg_isFloat, ctrl);     
        return false;      
    }    
    else
    {
        divHidden();
        return true;
    }
}

function isEquals(str1, str2)
{
    if (str1.value == str2.value)
    {
        divHidden();
        return true;
    }
    else
    {
        str1.focus();
        divShow(msg_twoEmail, str1);  
        return false;
    }
}

function isValidEmail(ctrl)
{

    var strval = ctrl.value;
   
    if (strval.indexOf('@') < 1)
    {
        ctrl.focus();
        divShow(msg_isEmail, ctrl);     
        return false;      
    }    
    else
    {
        divHidden();
        return true;
    }   
}

function isRequired(ctrl)
{
    var strval = ctrl.value;
            
    if (strval == null || strval == "")
    {
        tabview_switch('tab', 1);
        ctrl.focus();
        divShow('[' + ctrl.id + ']' + msg_isRequired, ctrl);     
        return false;      
    }    
    else
    {
        divHidden();
        return true;
    }
}


function isRequired2(ctrl)
{
    var strval = ctrl.value;
            
    if (strval == null || strval == "")
    {
        ctrl.focus();
        divShow('[' + ctrl.id + ']' + msg_isRequired, ctrl);     
        return false;      
    }    
    else
    {
        divHidden();
        return true;
    }
}

function isNummeric2(str)
{
    if (str == "" || str == null) { return false; }
    
    var Number = "0123456789.";
    for (i = 0; i < str.length; i++)
    {   
        var c = str.charAt(i);
        if (Number.indexOf(c) == -1) return false;
    }
    return true;                                                                                            
}

function isIncludeHTMLtabs(ctrl)
{
    var regul = /<\/?[^>]*>/g;    
    var strval = ctrl.value;
            
    if (regul.test(strval))
    {
        ctrl.focus();
        divShow(msg_isHTMLtags, ctrl);   //'[' + ctrl.id + ']' +   
        return false;      
    }    
    else
    {
        divHidden();
        return true;
    }
}

function isIncludeSpecialChar(ctrl)
{  
    var strval = ctrl.value;
            
    if (!isNummeric2(strval))
    {  
        ctrl.focus();
        divShow(ctrl.id + msg_specialchar, ctrl);   //'[' + ctrl.id + ']' +   
        return false;      
    }    
    else
    {  
        divHidden();
        return true;
    }
}

function isCheckboxNonSelected(ctrlname)
{
    var mthval = getCheckboxSelectedValue(ctrlname);   

    if (mthval == "")
    {  
        tabview_switch('tab', 1);    
        var obj = document.getElementsByName(ctrlname)[0];
        obj.focus();
        divShow(msg_adtype, obj); 
        return false;      
    }    
    else
    {
        return true;
    }
}

function sendUserMail(adid, type)
{
    openNewWin('/getemail.php?busid=' + adid + '&type=' + type, 380, 600);
}

function clearform(formobj)
{
    var formcount = formobj.length;
    
    for (var i=0; i<formcount; i++)
    {
        var objtype = formobj[i].type;
       
        switch (objtype)
        {
            case "text":
                formobj[i].value = "";
                break;
                
            case "hidden":
                formobj[i].value = "";
                break;            
                
            case "checkbox":
                formobj[i].checked = false;
                break; 
                
            case "select-multiple":
                for (var j=0; j<formobj[i].length; j++)
                {
                    formobj[i][j].selected = false;
                }                   
                break;    
                
           case "select-one":
                formobj[i][0].selected = true;
                break;
                
            case "radio":
                formobj[i].checked = false;
                break;                            
        }
        
    }
}
