function trim(str) {
  var s = str;
  var m = s.length - 1;
  var i = -1;
  while (m > i && (s.charAt(m) == " " || s.charCodeAt(m) == 10 || s.charCodeAt(m) == 13)) --m;
  if (m != (s.length-1)) s = s.slice(0, m+1);
  m = 0; i = s.length - 1;
  while (m < i && (s.charAt(m) == " " || s.charCodeAt(m) == 10 || s.charCodeAt(m) == 13)) ++m;
  if (m != 0) s = s.slice(m, s.length);
  return s;
}

function ValidNumber(thestring) {
  var point_number = 0;
  if(trim(thestring)==".") return false;
  for (i = 0; i < thestring.length; i++) {
    ch = thestring.substring(i, i+1);
    if(ch <= 9 || ch== ".") {
       if (ch == ".") {
        if (point_number > 0) {
          return false;
        }
        else {
          point_number++;
        }
      }

    } else {
      return false;
      
    }

  }
  return true;
}

function openURL(purl, pname, pwidth, pheight, pscroll) {
  var winwidth = parseInt(screen.width*0.85);
  var winheight = parseInt(screen.height*0.85); 
  var winname = "";
  var startx = 0;
  var starty = 0;
        var winscroll = (pscroll==null)?'1':pscroll;

  if (pname != null && pname != "") winname = pname;

  if (pwidth > 0) {
    winwidth = pwidth;
    if (screen.width > 2000) startx = parseInt((screen.width/2 - winwidth)/2);
    else startx = parseInt((screen.width - winwidth)/2);
  } else if (screen.width > 2000) {
    winwidth = parseInt(winwidth/2);
    startx = parseInt((screen.width/2 - winwidth)/2);
  } else {
    startx = parseInt((screen.width - winwidth)/2);
  }

  if (pheight > 0) {
    winheight = pheight;
    if (navigator.appName == "Netscape") winheight = parseInt(winheight * 0.78);
  }

  starty = parseInt((screen.height - winheight)/2); 

  window.open(purl,winname,"resizable=1,scrollbars="+winscroll+",width="+winwidth+",height="+winheight+",screenx="+startx+",screeny="+starty);

}


function insertPhoneHyphen(phoneField){
    var phoneNumber = phoneField.value
    var stringLength = phoneNumber.length
    var msg = "phone ok";

   

    if ( phoneNumber.search(/\d{3}-\d{4}/) == -1) {
        alert(msg);
        phoneField.focus();
        phoneField.select();
        return false;
    }else{
        alert("bad phone");
    }




    return true;
}


function moveTab(name, moveto, moveon){
	var len = name.value.length
	if (len == moveon) {
	  moveto.focus();
      if (moveto.type == 'text')  moveto.select();
	}
}

function setValueForTextFromCheckBox (ischecked, textobj) {
    if (ischecked) textobj.value = 'Y';
    else textobj.value = 'N';
}

function setValueForCheckBox (textid, checkboxid) {
    if (document.getElementById(textid).value=='Y') document.getElementById(checkboxid).checked = true;
    else  document.getElementById(checkboxid).checked = false;
}

function setValueForTextFromRadio (radiobuttonobj, textobj) {
    if (radiobuttonobj.checked) {
        textobj.value = radiobuttonobj.value;
    } else {
        textobj.value = '0';
    }
}

function setValueForRadioButtons (textid, radioname) {
    for (var i=0; i<radioname.length; i++) {
        if (radioname[i].value == document.getElementById(textid).value) {
            radioname[i].checked = true;
        }
    }
}

function setValueForRadioButtonsByValue (value, radioname) {
    for (var i=0; i<radioname.length; i++) {
        if (radioname[i].value == value) {
            radioname[i].checked = true;
        }
    }
}
