function SetBGclr(obj, clr) {
  obj.style.backgroundColor = clr;
}

function SwapImages(obj, img) {
  obj.src = img;
}

//---------------------------------------------------------------
// SetSelectIdx()
//---------------------------------------------------------------
function SetSelectIdx(sel, value) {
  for (cnt=0; cnt<sel.options.length; cnt++) {
//    alert('value[' + cnt + '] = "' + sel.options[cnt].value + '"');
    
    if ( (sel.options[cnt].value == value) && 
         (sel.selectedIndex != cnt) ) {
//      alert('changing index');
      sel.selectedIndex = cnt;
      break;
    }
  }
}

//---------------------------------------------------------------
// OpenURL()
//---------------------------------------------------------------
function OpenURL(url) {
  document.location.href = url;
}

function isNumeric(vTestValue)
{
    // put the TEST value into a string object variable
    //RW Trim is causing an error and I can't find the JS function
    // var sField = new String(Trim(vTestValue));
    var sField = new String(vTestValue);
	
    // check for a length of 0 - if so, return false
    if (sField.length == 0) { 
      return false; 
    }
    else if (sField.length == 1 && (sField.charAt(0) == '.' || sField.charAt(0) == ',' || (sField.charAt(0) == '-'))) { return false; }
	
    // loop through each character of the string
    for (var x=0; x < sField.length; x++) {
	// if the character is < 0 or > 9, return false (not a number)
	if((sField.charAt(x) >= '0' && sField.charAt(x) <= '9') || sField.charAt(x) == '.' || sField.charAt(x) == ',' || (sField.charAt(x) == '-' && x==0)) { /* do nothing */ }
	else { return false; }
    }
	
    // made it through the loop - we have a number
    return true;
}

function StrToDate(adate) {

  var dayfield = adate.split("/")[0]
  var monthfield = adate.split("/")[1]
  var yearfield = adate.split("/")[2]
  
  return new Date(yearfield, monthfield-1, dayfield);
}

function LZero(len, data) {
  s=String(data);
  slen=s.length;
  while (slen<len) {
    s="0"+s;
    slen=s.length;
  }
  return s; 
}

function SQLDate(adate) {
  var dayfield = adate.split("/")[0]
  var monthfield = adate.split("/")[1]
  var yearfield = adate.split("/")[2]
  return LZero(4,yearfield) + "-" + LZero(2,monthfield) + "-" + LZero(2,dayfield);
}

function CompareDate(Date1,Date2) {
  SQLDate1=SQLDate(Date1);
  SQLDate2=SQLDate(Date2);
  if (SQLDate1>SQLDate2) {
    return 1
  }
  else {
    if (SQLDate1<SQLDate2) {
      return -1
    }
    else {  
      return 0
    }
  }  
}

function chkemail(obj) {
  email=obj.value;
  if (email.length==0) {
    return true
  }
  else {  
    atpos=email.indexOf('@');
    dotpos=email.indexOf('.');
    spacepos=email.indexOf(' ');
    ok=(atpos!=0) && (dotpos!=0) && (atpos<dotpos) && (spacepos<0);
    if (ok==false) {
      obj.select();
      alert("Please enter a valid email.");
      obj.focus();
      return false;
    }
    else {
      return true;
    }
  }
}

function chkphoneaus(obj,format) {
 phone=obj.value.replace(/ /g,'');
 if (phone.length==0) {
    return true
  }
  else {  
    ok=(phone.length==8) || ((phone.length==10) && (phone.substr(0,1)=='0'));
    if (ok==false) {
      obj.select();
      alert("Please enter a valid phone number. only 8 or 10 digits");
      obj.focus();
      return false;
    }
    else {
      if (format==0) {
        obj.value=phone;
      }
      if (format==1) {
        if (phone.length==8) {
          phone=phone.substr(0,4)+' '+phone.substr(4,4);
        }
        else {
          phone=phone.substr(0,2)+' '+phone.substr(2,4)+' '+phone.substr(6,4);
        }
        obj.value=phone;
      }
      if (format==2) {
        if (phone.length==8) {
          phone=phone.substr(0,4)+' '+phone.substr(4,4);
        }
        else {
          phone='('+phone.substr(0,2)+') '+phone.substr(2,4)+' '+phone.substr(6,4);
        }
        obj.value=phone;
      }
      return true;
    }
  }
}

function chkmobileaus(obj,format) {
 phone=obj.value.replace(/ /g,'');
 if (phone.length==0) {
    return true
  }
  else {  
    ok=((phone.length==10) && (phone.substr(0,2)=='04'));
    if (ok==false) {
      obj.select();
      alert("Please enter a valid phone number. 10 digits and starting with 04.");
      obj.focus();
      return false;
    }
    else {
      if (format==0) {
        obj.value=phone;
      }
      if (format==1) {
        phone=phone.substr(0,4)+' '+phone.substr(4,3)+' '+phone.substr(7,3);
        obj.value=phone;
      }
      if (format==2) {
        phone='('+phone.substr(0,4)+') '+phone.substr(4,3)+' '+phone.substr(7,3);
        obj.value=phone;
      }
      return true;
    }
  }
}

function chkpostcodeaus(obj) {
 pcode=String(obj.value.replace(/ /g,''));
 if (pcode.length==0) {
    return true;
  }
  else {  
    ok=((pcode.length==4) && (isNumeric(pcode)==true));
    if (ok==false) {
      obj.select();
      alert("Please enter a valid postcode.");
      obj.focus();
      return false;
    }
    else {
      obj.value=pcode;
      return true;
    }
  }
}

