/* ======================
Inbound Email Form Sign
Version: iefs 0.1
Build: 20080909
Instructions: 
- This script needs to be called at 
- the bottom of the closing body(</body>) tag for it
- to work.
========================= */

var IEFS = {
	search_pattern : '8y7hov3lVTOmJR5adizNZe2QfB1CngwrLKqP04WtYxk6jFD9cIbXSpHMGEAs', // do not change
	encrypt_pattern : 'hOgsW1BLmGrCjXPyqT5FYQ0JAZMn2DvibkElcax8K3eSRtzNwIVoHdp469f7', // do not change
	enable : true,
	clear_token: false,
	form : '',
	encryptString: function(string){
		var result='';
		for (var i=0; i < string.length; i++){			
			result += IEFS.encriptChar(string.substr(i,1),string.length, i);
		}
		return result;
	},	
	encriptChar: function(char, variable, a_index){		
		var index=0;		
		if(IEFS.search_pattern.indexOf(char) != -1){
			index = (IEFS.search_pattern.indexOf(char) + variable + a_index) % IEFS.search_pattern.length;
			return IEFS.encrypt_pattern.substr(index, 1);
		}		
		return char;
	},	
	deEncryptString: function(string){
		var result='';
		var i = string.length - 1;
		
		do {
			result += IEFS.deEncryptChar(string.substr(i,1),string.length, i);
		}  while (i--);
		return result;
	},	
	deEncryptChar: function(char, variable, a_index){
		var index=0;
		if(IEFS.encrypt_pattern.indexOf(char) != -1){
			if((IEFS.encrypt_pattern.indexOf(char) - variable - a_index) > 0){
				index = (IEFS.encrypt_pattern.indexOf(char) - variable - a_index) % IEFS.encrypt_pattern.length;
			}else{
				index = (IEFS.search_pattern.length) + ((IEFS.encrypt_pattern.indexOf(char) - variable - a_index) % IEFS.encrypt_pattern.length);
			}
			index = index % IEFS.encrypt_pattern.length;			
			return IEFS.search_pattern.substr(index, 1);
		}else{
			return char;
		}
	},	
	setForm: function(){
		if(document.hp_customer_email_form){
			IEFS.form = document.hp_customer_email_form;
		}else if(getElementByID('hp_customer_email_form_id')){
			IEFS.form = getElementByID('hp_customer_email_form_id');
		}
	},	
	OnBeforeFormSignup: null,
	OnPageLoad: null,
	signForm: function(){
		if(IEFS.OnBeforeFormSignup){
			IEFS.OnBeforeFormSignup();
		}
		if (IEFS.enable){
			var e = IEFS.form.email_address.value.replace('@','_a_T_').replace('.','_d_o_T_') + '_-_'+ IEFS.form.first_name.value + '_-_' + IEFS.form.last_name.value;
			IEFS.form.hpweb_one_time_token.value = IEFS.encryptString( escape( Utf8.encode( e ) ) );			
		}else{
			if(IEFS.clear_token){
				IEFS.form.hpweb_one_time_token.value = '';
			}
		}
		return true;				
	},	
	init: function() {
		
		IEFS.setForm();	
		if(IEFS.form){
			if(IEFS.OnPageLoad){
				IEFS.OnPageLoad();
			}
			if(IEFS.clear_token){
				IEFS.form.hpweb_one_time_token.value = '';
			}
			IEFS.form.hp_customer_email_submit.onclick = IEFS.signForm;
		} return true;
	}
}

var Utf8 = {

	// public method for url encoding
	encode : function (string) {
		
		string = string.replace(/\r\n/g,"\n");
		
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			} else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			} else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
		}

		return utftext;
	},

	// public method for url decoding
	decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			} else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			} else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}

		return string;
	}
}

IEFS.init();