var error_emptyfield = "This field is required and can't be empty";
var error_invalidemailmsg = "Email is invalid";

dom = (document.getElementById)? true : false;
nn4 = (document.layers)? true : false;
ie4 = (!dom && document.all)? true : false;

function GetObj(name)
{
  if (dom) nobj=document.getElementById(name);
  else if (ie4) nobj=document.all[name];
  else if (nn4) nobj=document.layers[name];
  return nobj;
}
function sg_show_div(id)
{
    ovdiv = document.getElementById(id);
    if (ovdiv)
    {
        if (ovdiv.style.display == "none") { ovdiv.style.display = "block"; }
        else { ovdiv.style.display = "none"; }
    }
    return true;
}

function me(menu_name, event_type) //Menu Event
{
  if(event_type == 1)
  {
    GetObj(menu_name).click();
  }
  if(event_type == 2)
  {
    GetObj(menu_name).className = 'menu_link2';
  }
  if(event_type == 3)
  {
    GetObj(menu_name).className = 'menu_link';
  }
}

function trim(inputString) {
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


function validateForm( f, type )
{
    var elem, i = 0, attr = "alt";
    while ( elem = f.elements[i++] )
    {
        //  Skip fieldsets
        if ( elem.nodeName == "FIELDSET" ) continue;

        //  Does element have validator attribute? (short-circuit check)
        fvCode          = ( elem[attr] ) ? elem[attr] : elem.getAttribute( attr );
        if ( !( typeof fvCode == 'undefined' || fvCode == null || fvCode == "" ) )
        {
            //alert(fvCode);
            //  Set params, validation type, and validation state
            validcontrols = fvCode.split( "|" );

            vld = true;

            var spanobj = document.getElementById(elem.name + "_valid");

            var clsname = elem.className;

            var poserr = clsname.indexOf("_error");
            if (poserr != -1) // this is already error tag
            {
                clsname = clsname.substring(0, poserr);
            }
            elem.className = clsname;

            for (k=0; k<validcontrols.length; k++)
            {
                validcontrol = validcontrols[k];
                var params = validcontrol.split( "," );    
                switch( validcontrol )
                {
                    case "empty" :
                        try
                        {
                            var str = elem.value;
                            if(str == null || str.length == 0) 
                            {
                                vld = false;
                                if (type == 2)
                                {
                                    var globalspanobj = document.getElementById("form_error_span");
                                    if (globalspanobj) 
                                    {
                                        elem.className = clsname + "_error";
//                                      elem.style.border = "red 2px solid";
                                        globalspanobj.innerHTML = "&nbsp;<font color=red> " + error_emptyfield + " </font>";
                                    }
                                }
                                else
                                {
                                    if (spanobj) { spanobj.innerHTML = "&nbsp;<font color=red> " + error_emptyfield + " </font>";}
                                }

                                elem.focus();
                                return false;    
                            }
                        } 
                        catch(e) { }
                        break;

                    case "email" :
                        var bad = 0;
                        try
                        {

                            var str = elem.value;
                            var retValue = trim(str);
                            invalidChars = " /:,;";
                            if (retValue == "") {
                                bad = 1;
                            }
                            for (ii=0; ii < invalidChars.length; ii++) {
                                badChar = invalidChars.charAt(ii);
                                if (retValue.indexOf(badChar,0) > -1) {
                                    bad = 1;
                                }
                            }
                            atPos = retValue.indexOf("@",1);
                            if (atPos == -1) {
                                bad = 1;
                            }
                            if (retValue.indexOf("@",atPos+1) > -1) {
                                bad = 1;
                            }
                            periodPos = retValue.indexOf(".",atPos)
                            if (periodPos == -1) {
                                bad = 1;
                            }
                            if (periodPos+3 > retValue.length) {
                                bad = 1;
                            }

                        }
                        catch(e) {}

                        if (bad > 0 )
                        {                   
                            if (type == 2)
                            {
                                var globalspanobj = document.getElementById("form_error_span");
                                if (globalspanobj) 
                                {
                                    elem.style.border = "red 2px solid";
                                    globalspanobj.innerHTML = "&nbsp;<font color=red> " + error_invalidemailmsg + " </font>";
                                }
                            }
                            else
                            {
                                if (spanobj) { spanobj.innerHTML = "&nbsp;<font color=red> " + error_invalidemailmsg + " </font>"; }
                            }
                                
                            elem.focus();
                            return false;
                        }

                        break;
                        default: break;
                }
            }
        }
    } 
    return true;
}

document.write('<script type="text/javascript" src="/imgmod/menulib.js"></script>');
