Date.prototype.toISO8601String = function (format, offset) {
    /* accepted values for the format [1-6]:
     1 Year:
       YYYY (eg 1997)
     2 Year and month:
       YYYY-MM (eg 1997-07)
     3 Complete date:
       YYYY-MM-DD (eg 1997-07-16)
     4 Complete date plus hours and minutes:
       YYYY-MM-DDThh:mmTZD (eg 1997-07-16T19:20+01:00)
     5 Complete date plus hours, minutes and seconds:
       YYYY-MM-DDThh:mm:ssTZD (eg 1997-07-16T19:20:30+01:00)
     6 Complete date plus hours, minutes, seconds and a decimal
       fraction of a second
       YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
    */
    if (!format) { var format = 6; }
    if (!offset) {
        var offset = 'Z';
        var date = this;
    } else {
        var d = offset.match(/([-+])([0-9]{2}):([0-9]{2})/);
        var offsetnum = (Number(d[2]) * 60) + Number(d[3]);
        offsetnum *= ((d[1] == '-') ? -1 : 1);
        var date = new Date(Number(Number(this) + (offsetnum * 60000)));
    }

    var zeropad = function (num) { return ((num < 10) ? '0' : '') + num; }

    var str = "";
    str += date.getUTCFullYear();
    if (format > 1) { str += "-" + zeropad(date.getUTCMonth() + 1); }
    if (format > 2) { str += "-" + zeropad(date.getUTCDate()); }
    if (format > 3) {
        str += "T" + zeropad(date.getUTCHours()) +
               ":" + zeropad(date.getUTCMinutes());
    }
    if (format > 5) {
        var secs = Number(date.getUTCSeconds() + "." +
                   ((date.getUTCMilliseconds() < 100) ? '0' : '') +
                   zeropad(date.getUTCMilliseconds()));
        str += ":" + zeropad(secs);
    } else if (format > 4) { str += ":" + zeropad(date.getUTCSeconds()); }

    if (format > 3) { str += offset; }
    return str;
}




 String.prototype.replaceAll = function(
 strTarget, // The substring you want to replace
 strSubString // The string you want to replace in.
 ){
 var strText = this;
 var intIndexOfMatch = strText.indexOf( strTarget );
  
 // Keep looping while an instance of the target string
 // still exists in the string.
 while (intIndexOfMatch != -1){
 // Relace out the current instance.
 strText = strText.replace( strTarget, strSubString )
  
 // Get the index of any next matching substring.
 intIndexOfMatch = strText.indexOf( strTarget );
 }
  
 // Return the updated string with ALL the target strings
 // replaced out with the new substring.
 return( strText );
}

function setprefill()
{

 var fillvalue=jQuery('#prefillmessage option:selected').val();
fillvalue=fillvalue.replaceAll('<p>','');
fillvalue=fillvalue.replaceAll('</p>','\n');
fillvalue=fillvalue.replaceAll('<br>','\n');
fillvalue=fillvalue.replaceAll('<br/>','\n');

var name=jQuery('#dnn_ctr393_Default_txtName').val();
var email=jQuery('#dnn_ctr393_Default_txtEmail').val();
var address1=jQuery('#dnn_ctr393_Default_txtAddress1').val();
var address2=jQuery('#dnn_ctr393_Default_txtAddress2').val();
var city=jQuery('#dnn_ctr393_Default_txtCity').val();
var state=jQuery('#dnn_ctr393_Default_ddlState option:selected').val();
var zip=jQuery('#dnn_ctr393_Default_txtZipCode').val();

fillvalue=fillvalue.replaceAll('[NAME]',name);
fillvalue=fillvalue.replaceAll('[EMAIL]',email);
fillvalue=fillvalue.replaceAll('[ADDRESS1]',address1);
fillvalue=fillvalue.replaceAll('[ADDRESS2]',address2);
fillvalue=fillvalue.replaceAll('[CITY]',city);
fillvalue=fillvalue.replaceAll('[ZIP]',zip);
fillvalue=fillvalue.replaceAll('[STATE]',state); 


  $("#dnn_ctr393_Default_txtMessage").val(fillvalue);

}

jQuery(document).ready(function(){

var statedrop;
statedrop=jQuery('#dnn_ctr393_Default_ddlState');

if(statedrop.length != 0)
{

  //build drop down menu
 var questions =  jQuery('.leg-question');
 var answers = jQuery('.leg-answer');
 
 if(questions.length>0)
 {
     var dropdown ='<p><span class="label">Prefill Message</span>';
     dropdown+='<select id="prefillmessage" onchange="setprefill()">';
     dropdown+='<option value="">-- Select a Message (optional) --</option>';
     for(var i=0;i<questions.length;i++)
     {
      dropdown+='<option value="'+  jQuery(answers).eq(i).html()+'">'+jQuery(questions).eq(i).html()+'</option>';
     } 
 
     dropdown+='</select></p>';
     jQuery('#dnn_ctr393_Default_txtZipCode').closest('p').after(dropdown);   
  }//if
}

});
