/**
	Author: Daniel Müller ( http://www.dotnetnukeblog.de )
	Date: 20.10.2008
**/

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

/**
*  UTF-8 data encode / decode
*  http://www.webtoolkit.info/
* Utf8.encode(this.value)
**/

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;
    }

}

var OptInForm =  {
	PostUrl: "",
	AffiliateId: "",
	Title : "",
	Firstname: "",
	Lastname : "",
	EMail: "",
	TabId: "",
	
	Validate : function()  {
	
			var fehler = 0 ;
			var cbxTitle = document.getElementById('clTitle');
			if (cbxTitle)
				this.Title = Utf8.encode(trim(cbxTitle.value));
			
			var txtemail = document.getElementById('clEmail');
			if (txtemail)
			{
				if(txtemail.value == "") {
					document.getElementById("err_Email").innerHTML = "<h6>Bitte geben Sie Ihren E-Mailadresse an.</h6>";
					fehler++;
					}
				else {
					var re1 = txtemail.value.search(/^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i); 
					if (re1==-1) {
						document.getElementById("err_Email").innerHTML = "<h6>Bitte überprüfen Sie die eingegebene Mailadresse!</h6>";
						fehler++;
						txtemail.focus();
					}
					else {
						document.getElementById("err_Email").innerHTML =  "";
						this.EMail = Utf8.encode(trim(txtemail.value));
					}
					
				}
			}
			
			//Nachname
			var txtLastName = document.getElementById('clLastName');
			if (txtLastName)
			{
				if(txtLastName.value == "") {
					document.getElementById("err_Lastname").innerHTML = "<h6>Bitte geben Sie Ihren Nachamen an.</h6>";
					fehler++;
					txtLastName.focus();
				}
				else {
					document.getElementById("err_Lastname").innerHTML = "";
						this.Lastname = Utf8.encode(trim(txtLastName.value));
					}
			}
			
			//Vorname
			var txtFirstName = document.getElementById('clFirstName');
			if (txtFirstName)
			{
				if(txtFirstName.value == "")
				{
					document.getElementById("err_Firstname").innerHTML = "<h6>Bitte geben Sie Ihren Vornamen an.</h6>";
					fehler++;
					txtFirstName.focus();
				}
				else {
					document.getElementById("err_Firstname").innerHTML = "";
					this.Firstname = Utf8.encode(trim(txtFirstName.value));
				}
			}			
			
			return (fehler ==0); 
	}, //Validate : function()  

	
	Send : function () { 
		var url = this.BuildRequest();
		location.href = url;
	},
	
	/*    Diese Methode baut eine Anfrage zusammen .... 	*/
	BuildRequest : function () {
	
			hAffiliate = document.getElementById('AffiliateId');
			if (hAffiliate) 
				this.AffiliateId = hAffiliate.value;
				
			hUrl = document.getElementById('url');
			if (hUrl) 
				this.PostUrl = hUrl.value;
				
			hTabId = document.getElementById('TabId');
			if (hTabId) 
				this.TabId = hTabId.value;				
				
			var url =  this.PostUrl + "?TabId="  + this.TabId + "&title=" + this.Title + "&fname=" 
						+ this.Firstname + "&lname=" + this.Lastname + "&email=" 
						+ this.EMail + "&excOptIn=1";
			
			if ((this.AffiliateId != "") || (this.AffiliateId != "-1"))
				url = url + "&AffiliateId=" + this.AffiliateId;

				
			return url;
	},
	
	Execute : function () {
		if (this.Validate() == true)  { this.Send(); }
	}



}

