/**
 * @author alex
 */

var INDEX = 0;

function initPage()
{   		

		if ($("searchbox-long") !== null) {
   		 	if (quicksearch !== null){
   		 	
   		 		$("searchbox-long").setStyle({color: "black"});
   		 	    $("searchbox-long").value = quicksearch;
				
   		 	}
   		 	else{
   		 		 $("searchbox-long").value = "Search with your postcode";
   		 	}
        	Event.observe("searchbox-long", 'focus', function(){
        
            	this.value = "";
            	this.setStyle({
               	 color: "black"
            	})
            
        	});
        
        	Event.observe("searchbox-long", 'blur', function(){
        
            	if (this.value === "") {
                	this.setStyle({
                	    color: "#919191"
               		 });
               	 	this.value = "Search with your postcode";
           		 }
       	    });
    	 }  
  		 if ($("quicksearch") !== null) {
        	
        	Event.observe("searchbox-long", 'keypress', function(event){
        
            	if (event.keyCode === Event.KEY_RETURN){
                	
                	redirectToSearch();
               		Event.stop(event);   
               		
           		}
            
        	});
			
  			Event.observe("quicksearch", 'click', function(){
   				
   				redirectToSearch();
   				
           });  
    }
    
}

function loadPics(direction){
	if (direction) {
		INDEX = (INDEX + 1) % 6; 
	}
	else{
		INDEX -= 1;
		if (INDEX < 0) {INDEX = 5;}
	}
	
	var y_p = INDEX * 200;
	var pos = (y_p + 1) + "px 0px";
	$('rightcontainer').setStyle({backgroundPosition: pos});
	$('leftcontainer').setStyle({backgroundPosition: pos});
	
}

function redirectToSearch(){
	
	$("spinnerd").setStyle({visibility: "visible"}); 
	var code = "";
   	if ($("searchbox-long") !== null){
  		code = ($("searchbox-long").value);
  		code = code.replace(/\s+/g,'');
  	}
  	if (is_postcode(code)){
   		var redirecturl = "card/useit/quicksearch/" + code;
   		Fudel.redirect(redirecturl);
   			//

   	}
   	else{
   		alert("It seems that the postcode you entered is incorrect.");
		$("spinnerd").setStyle({visibility: "hidden"}); 
   	}
   	
}

function is_postcode(value){
	
	var stripped = value.replace( /\s/g, "" );
	return (stripped.match(/[a-zA-Z][0-9][0-9][a-zA-Z][a-zA-Z]/) !== null || stripped.match(/[a-zA-Z][0-9][0-9][0-9][a-zA-Z][a-zA-Z]/) !==null || stripped.match(/[a-zA-Z][a-zA-Z][0-9][0-9][a-zA-Z][a-zA-Z]/) !==null || stripped.match(/[a-zA-Z][a-zA-Z][0-9][0-9][0-9][a-zA-Z][a-zA-Z]/) !==null || stripped.match(/[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][a-zA-Z]/) !==null || stripped.match(/[a-zA-Z][a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][a-zA-Z]/) !==null);
}
