
//if(!WWW_ROOT) 
//  {alert('WWW_ROOT undefined');var WWW_ROOT='';}
//if(!WWW_LANG) 
//  {alert('WWW_LANG undefined');var WWW_LANG='en';}
var WWW_LANG='en';
//var POPUP_PATH=WWW_ROOT+'/popups';

function stripParams(url)
{
   url=url+''
   p=url.indexOf('?')
   return (p<0)? url : url.substr(0,p)
}

function NL2BR(s)
{
 return s.replace(/\n/g,'<br>')
}

function RadioValue(r)
{
 for(var i=0;i< r.length; i++)
  {
    if(r[i].checked=="1")
  return r[i].value;
  }
 return 0;
}

function SelectValue(SelectCtrl)
{
 return SelectCtrl.options[SelectCtrl.selectedIndex].value;
}


var MsgText
var MsgCaption
var MsgOnConfirm
function EAlert(AMsg,ACaption)
{/* popup window with msg message*/
alert(AMsg,ACaption); return;
  MsgCaption= (ACaption)? ACaption :  'Attention'
  MsgText=NL2BR(AMsg)
  openFrameless(POPUP_PATH+'/EAlert.html','Attention',420,250);
}


function EConfirm(AMsg,AOnConfirm,ACaption)
{
  MsgCaption= (ACaption)? ACaption :  'Confirmation'
  MsgText=NL2BR(AMsg)
  MsgOnConfirm=AOnConfirm
  openFrameless(POPUP_PATH+'/EConfirm.html','Confirmation',420,250);
}

/*    Gap's functions   */


function MM_findObj(n, d) { 
  var p,i,x;  if(!d) d=document; 
if((p=n.indexOf("?"))>0&&parent.frames.length) 
{
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
}

  if(!(x=d[n])&&d.all) x=d.all[n]; 
  for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}



function mOvr(src,clrOver) { 
  if (!src.contains(event.fromElement)) { src.style.cursor = 'hand'; src.bgColor = clrOver;}
}

function mOut(src,clrIn) { 
  if (!src.contains(event.toElement)) { src.style.cursor = 'default'; src.bgColor = clrIn; }
} 


function MM_validateEmail(s) { 
  var p, v, i, isValid;
  var forbiddenChar = new Array("," ," ", ";", "|", "'", '"');
  v = s;  p = v.indexOf('@');
  isValid =  !(p < 1 || p == (v.length-1) || v.lastIndexOf('.') > (v.length-3) 
       || v.lastIndexOf('.') < (v.length-5) || p != v.lastIndexOf('@') );
  if (!isValid) return false;
  for (i = 0; i < forbiddenChar.length; i++) {
    if ( v.indexOf(forbiddenChar[i]) >= 0 ) return false;
  }     
  return true;       
}




function isDate (day,month,year) {
    month=month-1;
    var test = new Date(year,month,day);

    if ( (test.getFullYear() == year) &&
         (month == test.getMonth()) &&
         (day == test.getDate())   &&
     (year>1900) && (year<2021))

        return true;
    else
        return false
}



function MM_validateDate(d)
{
var dl=d.split('/');
if(dl.length!=3)
    return false;

return isDate(dl[0],dl[1],dl[2])
}

function MM_lang_id(lang)
{
  if(lang=='en')
    return 0;
  if(lang=='fr')
    return 1;
  if(lang=='de')
    return 2;
  if(lang=='it')
    return 3;
  return lang;
}



function MM_validateRadio(l, Form, e0, e1, s) {
  var lg, i, err_msg  = new Array(" You must check one "," Âû äîëæíû îòìåòèòü îäèí ");  
  lg = MM_lang_id(l);
  for (i = e0; i <= e1; i++) {
    if (document.forms(Form).elements[i].checked) return true;
  }
  alert(err_msg[lg] + s); return false;
}

function MM_validateForm() { 
 /*params: form, defenition by 3 elemets
  arrays use (fr,de,it) elements*/
  var i, p, q, nm, test, num, s, min, max, errors = new Array(), args = MM_validateForm.arguments;
  var lg, wrong_email, wrong_number, wrong_number_range, and_word, required_field, error_mess
  lg = MM_lang_id(WWW_LANG);
  form=args[0];
  wrong_email        = new Array(" must contain an e-mail address"," doit contenir une adresse e-mail valide","Geben Sie eine E-Mail-Adresse ein.","Deve contenere un indirizzo e-mail valido.");  
  wrong_date         = new Array(" must contain a right dd/mm/yyyy format date"," doit contenir une date au format JJ/MM/AAAA","Geben Sie Ihr Komplettes Geburtsdatum ein (TT/MM/JJJJ)","Deve contenere una data al formato gg/mm/aaaa.");  
  wrong_number       = new Array(" must contain a number more than zero"," doit contenir un nombre supérieur à zéro ","Geben Sie eine Zahl ein die grösser als \"0\" ist","Deve contenere un numero superiore a zero");
  wrong_int_number   = new Array(" must contain an integer number more than zero"," doit contenir un nombre entier supérieur à zéro","Geben Sie ein Zahl ein die grösser als \"0\" ist","Deve contenere un numero intero superiore a zero");
  wrong_int_number_zero = new Array(" must contain an integer number more or equal than zero"," doit contenir un nombre entier supérieur ou égal à zéro ","Geben Sie eine Zahl ein die und egal als \"0\" ist","Deve contenere un numero superiore o uguale a zero");
  wrong_length = new Array(" minimal length is ","","");
  wrong_number_range = new Array(" must contain a number between "," doit contenir un nombre entre ","Geben Sie eine Zahl zwischen ","Deve contenere un numero fra");
  and_word           = new Array(" and "," et "," und "," e ");
  required_field     = new Array(" is required"," est requis"," ist verlangt"," è richiesto");
  error_mess         = new Array("The following error(s) occurred","L'erreur suivante s'est produite ","Dieses Irrtum ist vorgekommen ","L'errore seguente è stata commessa ");
  for (i = 1; i < (args.length-1); i+=3) { 
    test=args[i+2]; 
//    val=MM_findObj(args[i]);
    val=LastItem(form.elements[args[i]]);   
    if (val) 
    { 
      nm = ( args[i+1] ) ? args[i+1] : val.name; 
      if ((val=val.value)!="") 
      {
        if ((lp=test.indexOf('minlength='))!=-1) 
          { l=test.charAt(lp+10);
          if (val.length<l) errors[errors.length]=(nm+wrong_length[lg]+' '+l+'.');
        } 
        
        if (test.indexOf('isEmail')!=-1 && val != "" ) 
          { 
          if ( !MM_validateEmail(val) ) errors[errors.length]=(nm+wrong_email[lg]+'.');
        } 
        else if (test.indexOf('isDate')!=-1 && val != "" ) 
          { 
          if ( !MM_validateDate(val) ) errors[errors.length]=(nm+wrong_date[lg]+'.');
        } 
    
        else if (test.indexOf('isInt')!=-1 ) 
        {
            num = parseInt(val);
            if (test.indexOf('isIntZero')!=-1 ) 
            {
              if (val != num || num < 0) errors[errors.length]=(nm+ wrong_int_number_zero[lg]+'.');
            }
            else {
              if (val != num || num <= 0) errors[errors.length]=(nm+wrong_int_number[lg]+'.');
            }
        }
        else if (test.indexOf('isFloat')!=-1 ) 
        {
            num = parseFloat(val);
            if ( val != ''+num && val != ''+num+'.0' && val != ''+num+'.00' || num <= 0) errors[errors.length]=(nm+wrong_number[lg]+'.');
        } 
         
      } 
      else if (test.charAt(0) == 'R') errors[errors.length]=(nm+required_field[lg]+'.'); 
    }
    else errors[errors.length]=('!!!Unknown object "'+args[i]+'".');
  } 
  if (errors.length>0) 
  {
    EAlert(error_mess[lg]+':\n'+errors.slice(0,1).join('\n'));
  }
  document.MM_returnValue = (errors == '');
}


//function MySetVar(AForm,AName,AValue)
//{/*Set input's value.  Create hidden if not exist, or use last if it is array (the same name) */
//  vv=AForm.elements[AName];
//  if(vv==null)
//      insertHTML(AForm,'<INPUT TYPE="hidden" NAME="'+AName+'" VALUE="'+AValue+'"> ');
//  else
//  {   inp= vv.length?  vv[vv.length-1] : vv;
//  inp.value=AValue;
//  }
//}

function LastItem(AObject)
{/*Return last object from collection, or AObject itself (standalone item)*/
  
    return  (AObject && AObject.length)?  AObject[AObject.length-1] : AObject;
}

/*Find form by form name (from argument list) or by input name(s)*/
function FindFormByInputs()
{ forms=document.forms;
 for(var i=0;i<forms.length;i++)
   for(var j=0;j<arguments.length;j++)
     if(forms[i].name==arguments[j] || forms[i].elements[arguments[j]]!=null)
    return forms[i];
return null;
}



function MarkAll(cb)
{
 if(cb)
   if(cb.length)
     for(var i=0;i<cb.length;i++)
    cb[i].checked=1;
   else
     cb.checked=1;
}

//function insertHTML(obj,html)
//{/*wors in ie and ns6x*/
//obj.innerHTML=obj.innerHTML+html;
//}

function ListForm()
{
  return FindFormByInputs('ListForm','Page','SortCol')
}

function ListPage(APage)
{f=ListForm();
 LastItem(f.Page).value=APage;
 f.submit();
}

function ListSort(ASortCol)
{
 f=ListForm();
 LastItem(f.SortCol).value=ASortCol;
 LastItem(f.Page).value=1;
 f.submit();
}

function ListDeleteConfirmed(AId)
{
 f=ListForm();
 LastItem(f.mode).value='del';
 LastItem(f.id).value=AId;
 f.submit();
}

function ListEdit(AId)
{
 f=ListForm();
 LastItem(f.mode).value='edit';
 LastItem(f.id).value=AId;
 f.submit();
}

function ListItemDetails(AId)
{
 f=ListForm();
 LastItem(f.mode).value='details';
 LastItem(f.id).value=AId;
 f.submit();
}

function SubmitMode(AForm,AMode)
{
  AForm.mode.value=AMode;
  AForm.submit();
}



function checkSize(messageCtrl,counterCtrl)
    {
    maxLength = 160 // maximum string size of text area

    if (messageCtrl.value.length > maxLength)
        {
        messageCtrl.value = messageCtrl.value.substring(0,maxLength)    // truncates the string size
        charleft = 0    // updates charleft
        }
    else
        {
        charleft = maxLength - messageCtrl.value.length // updates charleft
        }

    counterCtrl.value = charleft    // displays charleft
    }





// POPUP SECTION
var autoclose = true
var beIE = document.all?true:false

function openFrameless(urlPop,title,windowW,windowH){
var windowX = (screen.width/2)-(windowW/2);
var windowY = (screen.height/2)-(windowH/2);
s = "width="+windowW+",height="+windowH+",left="+windowX+",top="+windowY;

 NFW=window.open(urlPop,"popFrameless","scrollbars,"+s)
 NFW.blur()
 window.focus() 
   
 NFW.focus()   
  if (autoclose){ window.onunload = function(){NFW.close()}
  }
}

function SetLang(Lang)
{
 f=document.LanguageSelector;
 f.LANG.value=Lang;
 f.submit();
}

function fIntZero(v)
{
  v=parseInt(v);
  if(isNaN(v))
    v=0;
  return v;
}


function AgentSubmit(form)
{
MM_validateForm(form,
'AgentID','ISO Office #','R',
'UserName','Desired UserID','R minlength=3',
'Password','','R minlength=4',
'Email','Email','RisEmail',
'Name','','R minlength=3'
);

if(document.MM_returnValue) 
   form.submit();
}

function AppSubmit(form)
{
MM_validateForm(form,
'BusinessName','Business Name','R minlength=4',
'BusinessEmail','Business Email Address','RisEmail'
);

if(form.MerchantType.value=='Internet')
  MM_validateForm(form,
  'BusinessWebsite','Business Website','R');

if(document.MM_returnValue) 
   form.submit();
}



function EquipDelete(Aid,Aname)
{f=document.EquipmentForm;
 if(confirm('Are you sure you want to delete Equipment #'+Aid+' ('+Aname+') from system?'))
 {
   f.equip_id.value=Aid;
   f.ACTION.value='DELETE';
   f.submit();
 }
}
