var FORM_TYPE = 'form';
var THANK_YOU_TYPE = 'thank_you';
var REQUEST_MESSAGE_TYPE = 'Request_Message_Type';
var REQUEST_MESSAGE_DIVISION = 'Request_Message_Division';
var PROMOTION_CODE = 'promotion_code';
var THANK_YOU_URL = 'thank_you_url';
function setTYC(doOverwrite) {
   if ( document.getElementById(THANK_YOU_URL) != 'undefined' ) {
      var tyURL= document.getElementById(THANK_YOU_URL).value;
      var retURL=tyURL;
      if(tyURL != '') {
         var pval=getURLParm(tyURL,'tyc');
         if((pval != '' && doOverwrite == 'true') || pval == '') {
            var rmt = getHiddenVal(REQUEST_MESSAGE_TYPE);
            var rmd = getHiddenVal(REQUEST_MESSAGE_DIVISION);
            if(rmt != '' && rmd != '') {
               var strArray = new Array();
               strArray = tyURL.split("?tyc=");
               retURL=strArray[0]+'?tyc='+rmd+rmt;
            }
         }
      }
    document.getElementById(THANK_YOU_URL).value = retURL
   }
}

function getURLParm(urlString,pname){  
   name = pname.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");  
   var regexS = "[\\?&]"+name+"=([^&#]*)";  
   var regex = new RegExp( regexS );  
   var ret = regex.exec(urlString);  
   if( ret == null )    
      return "";  
   else    
      return ret[1];
}

function getHiddenVal(hname) {
   var retval = '';
   if(hname != '' && document.getElementById(hname) != 'undefined' ) {
      var tmpObj = document.getElementById(hname);
      if(tmpObj){
         retval = tmpObj.value;
      }
   }
   return retval
}

function setOVars(pageType) {
if ( typeof( window[ 's' ] ) != "undefined" ) {
   switch (pageType) {
      case FORM_TYPE: 
         s.channel=getHiddenVal(REQUEST_MESSAGE_DIVISION);
         s.channel=upperFirst(s.channel);
	 s.prop7=getHiddenVal(PROMOTION_CODE);
	 s.prop7=formatDnis(s.prop7);
	 if(s.prop7!='') {s.eVar7=s.prop7}
	 s.prop9=s.channel
	 s.eVar9=s.prop9
	 var rmt=getHiddenVal(REQUEST_MESSAGE_TYPE);
	 if(rmt.length > 0) {
	    if(rmt.toUpperCase()=='C') {
	       s.prop10='Contact Form';
	       s.prop13='Contact'
	    } else if(rmt.toUpperCase()=='L') {
	       s.prop10='Lead Form';
	       s.prop13='Lead'
	    } else if(rmt.toUpperCase()=='S') {
	       s.prop10='Contact Form';
	       s.prop13='Contact'
	    }
	    s.eVar10=s.prop10;
	    s.eVar13=s.prop13;
         }
         break;
      case THANK_YOU_TYPE: 
         s.events='event1';
	 var pval = getURLParm(document.location.href,'tyc');
	 if(pval != '') {
	    var rmd = pval.substr(0, pval.length-1);
	    var rmt = pval.substr( pval.length-1);
	    if(rmt.length > 0) {
	 	if(rmt.toUpperCase()=='C') {
	 	   s.prop10='Contact Form Thank You';
	 	} else if(rmt.toUpperCase()=='L') {
	 	   s.prop10='Lead Form Thank You';
	 	} else if(rmt.toUpperCase()=='S') {
	 	   s.prop10='Contact Form Thank You';
	 	}
	     } 
	     s.channel = upperFirst(rmd);
         }
         break;
      default : break;
   }
}
}
function upperFirst(str) {
   var retval='';
   if(str && str.length > 0) {
     retval = str.substr(0,1).toUpperCase() + str.substr(1);
   }
   return retval
}

function formatDnis(dnis) {
   var retval='';
   if(dnis && dnis.length > 0) {
      if(dnis.indexOf("-") == -1) {
         if(dnis.length == 10) {
            retval=dnis.substr(0,3) + '-' + dnis.substr(3,3) + '-' + dnis.substr(6,4);
         }
      } else {
         retval = dnis;
      }
   }
   return retval;
}