/*
	Globular Keeper v0.1
	2009.07.17 -gBorelli
*/
function Globular(fatherDoc)
{ 
	this.login = '';
	this.login_name = '';
	this.whois_form = '';
	this.father = fatherDoc;
	this.alt_repos = Array( );
	this.alt_logged = Array( );
	this.alt_notlog = Array( );
	this.Funzionzs = Array( );
}

/**
* Raw cookie reading.
*/
Globular.prototype.readCookie = function( item ){
	//document.cookie;
	var ind=document.cookie.indexOf(item);
	if (ind==-1 || item=="") return ""; 
	var ind1=document.cookie.indexOf(';',ind);
	if (ind1==-1) ind1=document.cookie.length; 
	return unescape(document.cookie.substring(ind+item.length+1,ind1));
}

/**
* Alias to read the Cart Items .
* using this separate function permits you to override it and set a different tag around 
* the number of elements in the cart.
*/
Globular.prototype.getCartProducts = function( ){
	var numero = this.readCookie('numItems');
	if( numero.length < 1 )
		numero = '0';
	return '<strong>'+numero+'</strong>';
}

/**
* check if login has been done.
*/
Globular.prototype.checkLogged = function ( ){
	if( this.login == "" ){
		var tmp = this.readCookie('USERLOGGED');
		if( tmp ){
			this.login = true;
			this.login_name = tmp;
		} else {
			this.login = false;
		}
	}
	return this.login;
}

/**
* Process the instruction for the header.
*/
Globular.prototype.LoadHeader = function ( ) {
	this.checkLogged( );
	for(var i=0; i < this.alt_repos.length; i++){
		this.applyAlternative( this.father.getElementById( this.alt_repos[i] ) ,  i);
	}
	this.father.getElementById('hdCartNum').innerHTML = this.getCartProducts( );
	this.LoginBox( );
}

/**
*	Update internal list of items to modify in base of the login state
*/
Globular.prototype.setAlternative = function ( altId, logged, notlogged ){
	this.alt_repos.push( altId );
	this.alt_logged.push( logged );
	this.alt_notlog.push( notlogged );
}

/**
*	Use this function to expand the differences by type of object (changing links, changing classes etc.. )
*/
Globular.prototype.applyAlternative = function( object, iNum ){
	// if it's a LINK,choiches are for 
	// Note: you may want to change a class on an Anchor .. or similar.
	// in that case you have to create a parallel array to keep the information 
	// you want to change for every element.
	if( object.nodeName =='A' ){
		if(this.login) {
			object.href= this.alt_logged[iNum];
		} else {
			object.href= this.alt_notlog[iNum];
		}
		return;
	}
}

Globular.prototype.LoginBox = function ( ){
	// empty function to be overridden
	return false;
}

Globular.prototype.LoadWhoisPan = function( whois_id, choiche_id, tldarray, othergo, othertext) {
	var elem = this.father.getElementById( whois_id );
	elem.setAttribute('autocomplete','off'); //clean choiches
	var chem = this.father.getElementById( choiche_id );
	chem.innerHTML=''; // Clean childrens
	if(!elem || !chem){
		return;
	}
	var altern = 0;
	var dele = '';
	for( var i = 0; i < tldarray.length; i++ ){
		dele = document.createElement('li');
		if( altern ) {dele.setAttribute('class','whois_line' );altern = 0} else {altern = 1}
		dele.fatha = elem;
		dele.tld =tldarray[i];
		dele.currentDomain = '';
		dele.onclick = function ( ){
			this.fatha.value = this.currentDomain+'.'+this.tld;
			jQuery( this.parentNode ).slideUp( );
		}
		dele.setDomain = function( dom ){this.innerHTML = dom+'.'+this.tld;this.currentDomain = dom;}
		chem.appendChild( dele );
	}
	// Setup the "Others" li
	if( othertext.length > 2 ){
		dele = document.createElement('li');
		dele.gotolink = othergo;
		dele.innerHTML = othertext;
		dele.onclick = function ( ){ window.location= this.gotolink; }
		dele.setDomain = function( dom ){return;}
		chem.appendChild( dele );
	}
	elem.tlds = tldarray;
	elem.whoisChild = chem;
	elem.onblur = function( ){
		jQuery( this.whoisChild ).slideUp();
	}
	elem.onkeyup = function ( ){
		this.value=this.value.toLowerCase();
		if( this.value.length < 3 ){
			jQuery( this.whoisChild ).slideUp();
			return;
		}
		if( this.value.length > 62 ) this.value = this.value.substr( 0,62 );
		jQuery( this.whoisChild ).slideDown( );
		for( var i=0; i < this.tlds.length; i++){
			var re = new RegExp("\."+this.tlds[i].replace('.','\.')+"$","gi");
			if( re.test( this.value) ){
				jQuery( this.whoisChild ).slideUp();
				return;
			}
		}
		for( var i = 0; i < this.whoisChild.childNodes.length; i ++ ){
			this.whoisChild.childNodes[i].setDomain(this.value.replace( /^\-|[^\w\d\-\.]|(\-\-)+?|\-$|\.$/g ,""));
		}
	}
	elem.onclick= function(){
		for( var i=0; i < this.tlds.length; i++){
			if( this.value.length > 4 && this.value.search('.'+this.tlds[i]) != -1 ){
				this.value = this.value.replace( '.'+this.tlds[i] , "");
			}
		}
	}
	jQuery(chem).slideUp();
}

Globular.prototype.InstallWhoisForm = function ( formId, reg_link, tra_link ){
	this.whois_form = this.father.getElementById(formId);
	this.whois_destination = Array ( reg_link, tra_link );
}

Globular.prototype.whoisPost = function ( mode ){
	if(! this.whois_form )
		return;
	
	switch( mode ){
	case 'reg': default:
		this.whois_form.action= this.whois_destination[0];
	break;
	case 'tra':
		this.whois_form.action= this.whois_destination[1];
	break;
	}
	
	this.whois_form.submit();
	return false;
}

Globular.prototype.setAsync = function( tForm ){
	var mainform = this.father.getElementById( tForm );
	
	mainform.enroute = false;
	
	mainform.onsubmit = function(){
		if( this.enroute ) return false;

		
		this.addMessage = function(message){
			jQuery('.footer_feedback',this).html( message );
			jQuery('.footer_feedback',this).slideDown();
		}
		
		$.ajax({
			url: this.action,
			dataType: 'text',
			destination: this,
			data: jQuery(this).serialize(),
			success: function( data ){
				this.destination.enroute=false;
				this.destination.addMessage( data );
			 },
			type: 'post'
		});
		
		this.enroute = true;
		return false;
	}
}

Globular.prototype.addFunz = function( newFunz ){
	this.Funzionzs.push( newFunz );
}

Globular.prototype.execFunz = function( ){
	for( var i=0; i < this.Funzionzs.length;i++){
		this.Funzionzs[i]();
	}
}