// JavaScript Document
 function validate() {
    //to field not empty
    //text not empty
    //valid number in to field
    //to field can contain only 1 number

    var toField =  document.forms["SMSform"].to.value;
    var bodyField =  document.forms["SMSform"].body.value;

	toField = toField.replace(/\D+/g,'');

    if( toField.length == 0 ){
      alert("To field cannot be empty!");
      return false;
    }
	if(toField.length == 10){
		toField = '1'+toField;
	}
	if(toField.length != 11){
		alert("Please enter a valid phone number.");
		return false;
	}

    if( bodyField.length == 0 ){
      alert("Message cannot be empty!");
      return false;
    }

    if( toField.indexOf("0") == 0){
      alert("To field cannot start with 0");
      return false;
    }

    var pattern = /^\d+$/;
    if( ! toField.match( pattern ) ){
	  alert("To field must contain only numbers");
	  return false;
    }
	document.forms["SMSform"].to.value = toField;
    document.forms["SMSform"].submit();

  }//end validate
  
/*document.write("<form action='http://www.ipipi.com/SmsExternalAction.do' METHOD='post' name='SMSform'>")
document.write("<input type='hidden' name='userid' value='")
document.write(id)
document.write("'>")

document.write("<select name='body'><option value='4405 Main St, Philadelphia PA 19127 215-487-3333'>Manayunk</option><option value='201 E Lancaster Ave, Wayne PA 19087 610-989-9869'>Wayne</option><option value='22 S Main St, Doylestown PA 18901 267-327-4868'>Doylestown</option><option value='1733 Chestnut St. Philadelphia PA 19103'>Center City</option></select><br />");
//document.write("<input type='hidden' name='body' cols='50' rows='3' maxlength='150' value='4405 Main St, Philadelphia PA 19127 215-487-3333. 201 E Lancaster Ave, Wayne PA 19087 610-989-9869. 22 S Main St, Doylestown PA 18901 267-327-4868.'>");

document.write("<input type='text' name='to' size='14' maxlength='20'>")
document.write("<br />");
document.write("Area Code + Phone Number (e.g. 610-555-2929 or just 6105552929)")

document.write("<input type='hidden' name='prepend' size='14' maxlength='8' value='Beans Beauty'>");
document.write("<br />");
document.write("<input type='button' value='Send Text!' onclick='validate();' style='background:#f0d5f8; cursor:pointer; height:22px;'>");

document.write("</form>")*/
