
function Trim(s) {
   while (s.charCodeAt(0) == 32 || s.charCodeAt(0) == 13 || s.charCodeAt(0) == 10) {s = s.slice(1)}
   while (s.charCodeAt(s.length - 1) == 32 || s.charCodeAt(s.length - 1) == 13 || s.charCodeAt(s.length - 1) == 10) {s = s.slice(0,s.length - 1)}
   return s;
}

function RoundToCents(n) {
   Cents = n * 100;
   Cents = Math.round(Cents);
   strCents = "" + Cents;
   len = strCents.length;
   return strCents.substring(0, len - 2) + "." + strCents.substring(len - 2, len);
}

function Validate(i,cn,o) {
   if (isNaN(o.value) || (o.value == '') || (o.value=='0')) {
      o.value = 1;
      alert('Invalid quantity - used \'1\' instead!');
   }
   P = eval('document.frmCart.P'+i+'.value').slice(3);
   S = eval('document.frmCart.S'+i);
   S.value = ' R ' + RoundToCents(P * o.value);
   T = 0
   for (i = 1;i <= cn + 1;i++) {
      T = T + parseFloat(eval('document.frmCart.S'+i+'.value').slice(3));
   }
   document.frmCart.T.value = ' R ' + RoundToCents(T);
}

function Calculate(u,j) {
    var s = '';
    for (i=0;i<j;i++) {
        a = eval('document.frmCart.I'+(i+1));
        v = eval('document.frmCart.Q'+(i+1));
        s += a.value+'~'+v.value+'|';
    }
    self.location.href=u+'?Action=Calculate&CartStr='+escape(s)+'#Cart';
}

function selMoveOptions(s,d) {
    for (i=0;i<s.length;i++) {
        if (s.options[i].selected == true) {
            var newoption = new Option(s.options[i].text, s.options[i].value)
            d.options[d.length] = newoption;
            s.options[i] = null;
            i--;
        }
    }
}

function DoSelects(f,s) {
    arrS = s.split(',');
    for (j=0;j<arrS.length;j++) {
        var selS = eval('document.'+f.name+'.'+arrS[j]);
        for (i=0;i<selS.length;i++) {
            selS.options[i].selected = true;
        }
    }
   return true;
}

function AddObj (o) {
   var s = '|';
   for (i = 0;i < o.length;i++) {
      if ((o.options[i].selected) && (o.options[i].value > 0)) {
         var ttxt = o.options[i].text;
         o.options[i].text = '[X] ' + ttxt.slice(4);
         s = s + o.options[i].value + '|';
      }
   }
   for (i = 0;i < o.length;i++) {
      if (s.indexOf('|'+o.options[i].value+'|') >= 0) {
         o.options[i].selected = true;
      }
   }
}

function DelObj (o) {
   var s = '|';
   for (i = 0;i < o.length;i++) {
      if ((o.options[i].selected) && (o.options[i].value > 0)) {
         var ttxt = o.options[i].text;
         o.options[i].text = '[o] ' + ttxt.slice(4);
         s = s + o.options[i].value + '|';
      }
   }
   for (i = 0;i < o.length;i++) {
      if (s.indexOf('|'+o.options[i].value+'|') >= 0) {
         o.options[i].selected = true;
      }
   }
}

function Toggle (o,i) {
   var ttxt = o.options[i].text;
   if (o.options[i].text.indexOf('[X]') >= 0) {
      o.options[i].text = '[o] ' + ttxt.slice(4);
   } else {
       if (o.options[i].text.indexOf('[o]') >= 0) {
           o.options[i].text = '[X] ' + ttxt.slice(4);
       }
   }
   o.options[i].selected = true;
}

function DoCheckList(f,s) {
    var arrS = s.split(',');
    for (j=0;j<arrS.length;j++) {
        var selS = eval('document.'+f.name+'.'+arrS[j]);
        for (i=0;i<selS.length;i++) {
            selS.options[i].selected = false;
            if (selS.options[i].text.indexOf('[X]') >= 0) {
                selS.options[i].selected = true
            }
        }
    }
   return true;
}

function getCheckList(f,s) {
    var ListSelected = ''
    var arrS = s.split(',');
    for (j=0;j<arrS.length;j++) {
        var selS = eval('document.'+f.name+'.'+arrS[j]);
        for (i=0;i<selS.length;i++) {
            selS.options[i].selected = false;
            if (selS.options[i].text.indexOf('[X]') >= 0) {
                selS.options[i].selected = true
                if ( ListSelected.length > 0) {
                    ListSelected = ListSelected + ','
                }
                ListSelected = ListSelected + selS.options[i].value
            }
        }
    }
   return ListSelected;
}

function CheckVals(frm,arrV) {
   var DoIt = true;
   if (arguments[2] > '') {
      var errmsg = arguments[2];
   } else {
      var errmsg = 'You have not completed all the required fields !';
   }
   for (i=0;i<arrV.length;i++) {
      var Required = false;
      var p = arrV[i].split(':');
      var p1 = p[0];
      var p2 = '';
      var p3 = '';
      var p4 = '';
      var p5 = '';
      var p6 = '';
      if (p.length > 1) {p2 = p[1]}
      if (p.length > 2) {p3 = p[2]}
      if (p.length > 3) {p4 = p[3]}
      if (p.length > 4) {p5 = p[4]}
      if (p.length > 5) {p6 = p[5]}
      if (p4 != '') {errmsg = p4} else {errmsg = 'You have not completed all the required fields !'};
      var o = eval('document.'+frm.name+'.'+p1);
      var v = Trim(o.value);
      
      if (p5 > 0) {
          if (v.length != p5) {
              errmsg = 'You have not entered the required number of characters. Please try again!.';
              DoIt = false;
          }
      }
      
      if (p3 == '1') {Required = true}
      if ((Required) && (v <= '')) {DoIt = false}
      if (DoIt) {
         switch (p2.toUpperCase().slice(0,3)) {
         case 'EMA':
            if (v.length > 0) {
               if (v.indexOf('@',0) == -1 || v.indexOf('.',0) == -1 || v.indexOf(' ',0) > -1) {
                  if (p4 == '') {errmsg = 'You have not entered a valid e-mail address. Please try again!'} else {errmsg = p4}
                  DoIt = false;
               }
            }
            break;
         case 'TEL':
            if (v.length > 0) {
               for (j=0;j<v.length;j++) {
                  var c = v.charAt(j);
                  if (((c >= '0') && (c <= '9')) || (c == ' ') || (c == '+') || (c == '-') || (c == ')') || (c == '(')) {
                  } else {
                     if (p4 == '') {errmsg = 'You have not entered a valid phone number. Please try again!'} else {errmsg = p4}
                     DoIt = false;
                  }
               }
            }
            break;
         case 'PRI':
            if (v.length > 0) {
               if (isNaN(v)) {
                  if (p4 == '') {errmsg = 'You have not entered a valid price. Please try again!'} else {errmsg = p4}
                  DoIt = false;
               }
            }
            break;
         case 'NUM':
            if (v.length > 0) {
               if (isNaN(v)) {
                  if (p4 == '') {errmsg = 'You have not entered a valid number. Please try again!'} else {errmsg = p4}
                  DoIt = false;
               }
            }
            break;
         case 'DAT':
            if (v.length > 0) {
               d = Date.parse(v)
               if (isNaN(d)) {
                  if (p4 == '') {errmsg = 'You have not entered a valid date. Please try again!'} else {errmsg = p4}
                  DoIt = false;
               }
            }
            break;
         }
      }
      if (!DoIt) {
         alert(errmsg);
         //if (p5 != '') {
         //   tp1.setSelectedIndex(p5);
         //}
         o.focus();
         o.select();
         return false
      }
   }
   return true;
}

//remove all options
function clearOptions(ob) {
    for (i=0;i<ob.length;i++) {
        ob.options[i] = null;
        i--;
    }
}

//add option
function addOption(val,text,ob) {
    var newoption = new Option(text, val)
    ob.options[ob.length] = newoption;
}

//remove selected option(s)
function removeOption(ob) {
    for (i=0;i<ob.length;i++) {
        if (ob.options[i].selected == true) {
            ob.options[i] = null;
            i--;
        }
    }
}

//remove specified option
function removeSpecOption(ob,val) {
    for (i=0;i<ob.length;i++) {
        if (ob.options[i].value == val) {
            ob.options[i] = null;
            i--;
        }
    }
}

function checkLen(o,i) {
   if (o.value.length > i) {
      o.value = o.value.substr(0,i);
   }
   status=i - o.value.length+' characters remaining';
   return true;
}

function clearLen(o) {
   status='';
   return true;
}

function displayWindow(url, width, height) {
        var Win = window.open('../'+url,"displayWindow",'width=' + width + ',height=' + height + ',resizable=0,scrollbars=no,menubar=yes,status=yes' );
}

