/*
 * @class revosPlz
 * @copyright www.imagine.orange 
 * @package revosplz
 * @description
 * prototype class for magento extension revosplz
 *  
 */
var revosPlzClass = Class.create({
		
		
		
		initialize: function(){
			//alert('initializing...');
			this.currPoint = 0;
			this.searchUrl = null;
			this.skinUrl   = null;
			this.title   = null;
						
		},
		
		showVars: function(){
			alert(this.searchUrl + '<br />' + this.title[0] + '/' + this.title[1]);
		
		
		},
		doSearch: function(plz,titleIndex){
			$('searchResult').innerHTML = '';	
			Element.show('loadingMask');
			title = this.title[titleIndex]
			new Ajax.Request(this.searchUrl, 
					{ 	method: 'post', 
						parameters: 'plz='+plz, 
						onComplete: function (data){
							Element.hide('loadingMask');
							$('searchResult').innerHTML =  title + data.responseText;
						}
					}
			);
			
			return false;
		},
		
		doCallBack: function(){
			// save input field values
			var name = $('cb_name').value;
			var tel = $('cb_tel').value;
			// reset message fields
			$('callBackResult').innerHTML = '';	
			$('callBackError').innerHTML = '';
			// check params
			if ( name == 'Ihr Name' || name ==''){
				$('callBackError').innerHTML = "Bitte geben Sie Ihren g&uuml;ltigen Namen ein.";
				$('cb_name').focus();
			} else if ( tel == 'Ihre Tel.-Nummer' || tel ==''){
				$('callBackError').innerHTML = "Bitte geben Sie Ihre Telefonnummer ein.";
				$('cb_tel').focus();
			// do request if no errors 
			} else {
				
				Element.show('callBackLoadingMask');
				// alert('name='+$('cb_name').value+'&'+'tel='+$('cb_tel').value);
				new Ajax.Request('/index.php/revosplz/index/callBack', 
						{ 	method: 'post', 
							parameters: 'name='+name+'&'+'tel='+tel, 
							onComplete: function (data){
								Element.hide('callBackLoadingMask');
								$('callBackResult').innerHTML =  data.responseText;
								$('callBackResultBox').toggle();
								// alert(data.responseText);
							}
						}
				);
			}
			return false;
		},
		doOffer: function(){
			// save input field values
			var email = $('ia_email').value;
			
			// reset message fields
			$('offerResult').innerHTML = '';	
			$('offerError').innerHTML = '';
			// check params
			if ( email == 'Ihre E-Mail Adresse' || email =='' || !this.validateEmail(email)){
			    $('offerError').innerHTML = "Email Adresse ung&uuml;ltig.";
				$('ia_email').focus();
			// do request if no errors 
			} else {
				var prod = $('prodname').innerHTML;
				Element.show('offerLoadingMask');
				// alert('name='+$('cb_name').value+'&'+'tel='+$('cb_tel').value);
				new Ajax.Request('/index.php/revosplz/index/offer', 
						{ 	method: 'post', 
							parameters: 'email='+email+'&'+'prod='+prod, 
							onComplete: function (data){
								Element.hide('offerLoadingMask');
								$('offerResult').innerHTML =  data.responseText;
								$('offerResultBox').toggle();
								// alert(data.responseText);
							}
						}
				);
			}
			return false;
		},
		validateEmail: function(id) { 
			var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,4}$/; 
			return emailPattern.test(id); 

		},
		onBlur : function(point){
			//console.log('blur');
			if (this.currPoint != 0){
				$('overImg').src= this.skinUrl + this.currPoint + '.png';
			}
			return false;
		},
		
		onClick : function(point, plz){
			//console.log('click');
			$('overImg').src= this.skinUrl + point + '.png';
			this.currPoint = point;
			if(plz != undefined )this.doSearch(plz, 0);
			return false;
		},
		
		onHover : function(point){
			//console.log('hover');
			$('overImg').src= this.skinUrl + point + '.png';
			$('overImg').style.display = "block";
			return false;
		},
			
		submitForm: function(plzInputField){
			this.currPoint = 0;
			$('overImg').style.display = "none";
			return this.doSearch($(plzInputField).value, 1);
		}
			
});

// intanciate the class
var revosplz = new revosPlzClass();
