// controllo host e settaggio action della form di abbonamento 
var host = location.href;
if (host.indexOf(".stage")){
	var actionForm = "https://selfcare.tiscali.it/unit/selfcare/it_selfcare";
}
else{
	var actionForm = "https://selfcare.tiscali.it/unit/selfcare/it_selfcare";
}
// fine controllo host e settaggio action della form di abbonamento 

function _clear(element,label){
	if ($(element).val()==label)
		$(element).val('');
	else if ($(element).val()=='')
		$(element).val(label);	
	$(element).css("border","1px solid #e1e1e1");
}

var checkForm = new Object;
checkForm.prefix=function(e){
	var value = e.val();	
	if (value.length < 2 || value.length > 4 || !value.match(/^0/) || value.match(/\D/)){
		this.red(e);
		return "Verifica di aver inserito correttamente il prefisso.<br />";
	}else
		return '1';
}
checkForm.number=function(e){
	var value = e.val();
	if (value.length > 9 || value.match(/\D/) || value.length < 4){
		this.red(e);
		return "Verifica di aver inserito correttamente il tuo numero di telefono.<br />";
	}else
		return '1';
}
checkForm.clientNumber=function(e){
	var value = e.val();
	if (value=='' || value.match(/\D/) || value.length != 9 ){
		this.red(e);
		return "Il numero cliente inserito non è corretto.<br />";
	}else
		return '1';
}
checkForm.email=function(e){
	var value = e.val();
	if (value=='' || !value.match(/@tiscali.it$/)){
		this.red(e);
		return "L'indirizzo e-mail inserito non è corretto.<br />";
	}else
		return '1';
}
checkForm.psw=function(e){
	var value = e.val();
	if (value==''){
		this.red($("input[name='hideField']"));
		this.red(e);
		return "Inserisci la password di posta elettronica.<br />";
	}else
		return '1';
}
checkForm.red=function(e){
	e.css("border","1px solid red");
}

function upgrade(tipo){
	$("#errMsg").css({'display':'block'});
	if (tipo == 'ADSL'){
		var check=0;
		check = checkForm.email($("input[name='UserID']"));
		check += checkForm.psw($("input[name='Password']"));			
		if (check.length!=2){
			errDisplay(check);
			return false;
		}
	}else if (tipo=='ADSL_VOICE' || tipo=='VOICE'){
		var check=0;
		check = checkForm.clientNumber($("input[name='accountNumber']"));
		check += checkForm.prefix($("input[name='CLI_PREFIX']"));
		check += checkForm.number($("input[name='CLI_NUMBER']"));			
		if (check.length!=3){
			errDisplay(check);
			return false;
		}
	}else return false;
	submit_utente=true;

	var cBodyH = document.getElementById("cBody").offsetHeight;
	var boxTcH= document.getElementById("boxSp").offsetHeight;
	var hFlash = 66;
	boxTcHPad  = ((boxTcH-hFlash)/2);	
	boxTcH	   = (boxTcH - boxTcHPad);	

	$("#container").css({'visibility':'visible','background':'#FFF','height':cBodyH});
	$("#layer").css({'visibility':'visible','width':986,'height':boxTcH,'padding-top':boxTcHPad});		
	document.getElementById('adsl').action=actionForm;
	//document.adsl.action="http://u2ie-ecare-it.r10.tiscali.com:8080/unit/selfcare/it_selfcare";
	//document.adsl.action="http://ecare-0-it-dev.ecare.tiscali.sys/unit/selfcare/it_selfcare";
	setTimeout("document.getElementById('adsl').submit()",500);
}

function errDisplay(m){
	m=m.replace(/1/g,"");
	$("#errMsg > p").html(m).fadeIn('fast');
	setTimeout('$("#errMsg > p").fadeOut("slow");',7000);
}


function attiva(ik){
	
	if (ik){c = chIk();}
	if((ik) && (c)){
		document.getElementById('adsl').ticket.value=document.getElementById('ticket_ik_new').value+";INTERNET_KEY_UT=Chiavetta Internet Mobile|59.00";
	}
	
submit_utente=true;
var cBodyH = document.getElementById("cBody").offsetHeight;
var boxTcH= document.getElementById("boxSp").offsetHeight;
var hFlash = 66;
boxTcHPad  = ((boxTcH-hFlash)/2);	
boxTcH	   = (boxTcH - boxTcHPad);	
$("#container").css({'visibility':'visible','background':'#FFF','height':cBodyH});
$("#layer").css({'visibility':'visible','width':986,'height':boxTcH,'padding-top':boxTcHPad});		
document.getElementById('adsl').action=actionForm;
//document.adsl.action="http://u2ie-ecare-it.r10.tiscali.com:8080/unit/selfcare/it_selfcare";
//document.adsl.action="http://ecare-0-it-dev.ecare.tiscali.sys/unit/selfcare/it_selfcare";
setTimeout("document.getElementById('adsl').submit()",500);
}

function flash(percorso,width,height,id)
{
	var flashvars = {};
	flashvars.labelString = "stiamo elaborando la tua richiesta";
	var params = {};
	var attributes = {};
	attributes.id = "flashID";
	swfobject.embedSWF("/img/flash/loading.swf", "myAlternativeContent", "644", "66", "9.0.0", false, flashvars, params, attributes);	
	
} 


/* nasconde layer e container al rilascio della pagina */
function empty(){
		$("#container").css({'visibility':'hidden'});
		$("#layer").css({'visibility':'hidden'});		
};

/* switch hw no-hw */
function changeRow(id,name, formname){
	var sel = eval("document.getElementById('"+formname+"')."+name);					
	for(var i=0,a;a=sel[i];i++) {
		if(a.checked == true){
			eval("document.getElementById('"+id+"').className='rowSel'");		
		}
		else eval("document.getElementById('row"+i+"').className='rowNoSel'");		
	}
 }

 function sendCB(){
 	if (document.getElementById('callback').num_tel.value == "")	alert ("Attenzione!\nVerifica di aver inserito correttamente il numero telefonico.");
	else document.getElementById('callback').submit();
 }
 // ripulisce gli input delle pagine upgrade
function clearInput(type,val){
		if(type =='numero'){
			if (val=='numero')document.getElementById('adsl').CLI_NUMBER.value='';
		}		
		else if(type =='prefisso'){
			if (val=='prefisso')document.getElementById('adsl').CLI_PREFIX.value='';
		}
		else if(type =='accountNumber'){
			if (val=='numero cliente')document.getElementById('adsl').accountNumber.value='';
		}
		else if(type =='email'){
			if (val=='e-mail')document.getElementById('adsl').UserID.value='';
		}
		else if(type =='psw'){
			document.getElementById('adsl').Password.type='password';
			document.getElementById('adsl').Password.value='';
		}
}
function changefieldAdsl(){
		document.getElementById('adsl').hideField.style.display='none';
		document.getElementById('adsl').Password.style.display='';
		document.getElementById('adsl').Password.focus();
}

function expandSitofono(){
	var padTop = getWindowHeight2();
	var bodyH = getWindowHeight();
	var cBodyH = document.getElementById("cBody").offsetHeight;
	marTop  = (padTop+((bodyH/3)))+56;
//	alert (marTop);
	bodyW = document.documentElement.clientWidth;
	marLeft  = 172;	
	boxTcH	   = 100;	 
	maxBody = "100%";

//	$("#container").css({'visibility':'visible','height':cBodyH+50,'background':'#000','width':maxBody});
	$("#contSitofono").css({'visibility':'visible','min-height':boxTcH,'margin-top':5,'left':20});
}

function getWindowHeight() {
 var windowHeight = 0;
 if (typeof(window.innerHeight) == 'number') {
  windowHeight = window.innerHeight;
 }
 else {
  if (document.documentElement && document.documentElement.clientHeight) {
   windowHeight = document.documentElement.clientHeight;
  }
  else {
   if (document.body && document.body.clientHeight) {
    windowHeight = document.body.clientHeight;
   }
  }
 }
 return windowHeight;
}

function getWindowHeight2() {
	if (top.document.all) {
		clientH = top.document.body.clientHeight;
		clientW = top.document.body.clientWidth;
		if (top.document.documentElement.clientHeight!=0 && top.document.documentElement.clientWidth!=0 && (window.navigator.appName.indexOf('Opera') == -1) && (navigator.userAgent.indexOf('Mac') == -1)) {
			clientH = top.document.documentElement.clientHeight;
			clientW = top.document.documentElement.clientWidth;
		}
	} else {
		clientH = top.innerHeight;
		clientW = top.innerWidth;
		if (top.document.doctype) {
			if (top.document.doctype.publicId.indexOf('XHTML')>-1 && top.document.documentElement.clientHeight!=0 && top.document.documentElement.clientWidth!=0) {
				clientH = top.document.documentElement.clientHeight;
				clientW = top.document.documentElement.clientWidth;
			}
		}
	}
  	if( typeof( window.pageYOffset ) == 'number' ) {
    	scrOfY = window.pageYOffset;
  	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    	scrOfY = document.body.scrollTop;
  	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    	scrOfY = document.documentElement.scrollTop;
  	}
	else{scrOfY = 0;}
	var tOp=(clientH/2)-(500/2);
	var	selH="";
	return (scrOfY);

}

function clLayer(id){
//	$("#container").css({'visibility':'hidden'});
	$(id).css({'visibility':'hidden'});
}

function _clear(element,label){
	if ($(element).val()==label)
		$(element).val('');
	else if ($(element).val()=='')
		$(element).val(label);	
	$(element).css("border","1px solid #e1e1e1");
}

function errore(id){
	var msg = new Array(7);
	msg[3] = msg[7] ="Verifica di aver compilato correttamente i campi evdenziati in rosso.";
	msg[1] = msg[2] ="Verifica di aver compilato correttamente i campi evdenziati in rosso.";
	
	$("#msg_errore").html(msg[id]).fadeIn("fast");
	$("input[type='text']").css('border','1px solid #f00');			
	
	setTimeout('$("#msg_errore").fadeOut("slow");',7000);
	//setTimeout('$("input[type=\'text\']").css(\'border\',\'\');',15000);
}

$(document).ready(function () {
$("#dettaglio_costi").click(function(){
		var _url="dettaglio_costi/dettaglio_costi.html?"+Math.random()*100000;
		if ($("#content_dettaglio_costi").is(":hidden")) {
			$("#content_dettaglio_costi").load(_url);
			$("#tit_content_dettaglio_costi").css({'display':''});
			$("#content_dettaglio_costi").slideDown('fast');
			$("#dettaglio_costi").css({'display':'none'});
		}
		else {
			$("#content_dettaglio_costi").slideUp('fast');
		}
	});
	
});

var chk 	=Array ("chk0","chk1","chk2");
var chk_t 	=Array ("ticket_ik_0","ticket_ik_1","ticket_ik_2");
var chk_p 	=Array ("price_ik_0","price_ik_1","price_ik_2");

function ikMove(id){
	document.getElementById('ticket_ik_new').value=document.getElementById('adsl').ticket.value;
	for (i in chk){
		if (id==chk[i]){
			document.getElementById('ticket_ik_new').value+=";INTERNET_KEY="+document.getElementById(chk_t[i]).value+"|"+document.getElementById(chk_p[i]).value;
		}
		else{
			document.getElementById(chk[i]).checked=false;
		}
	}
	
}

function chIk (){
	for (i in chk){
		if (document.getElementById(chk[i]).checked==true) return "1";
	}
}

function showDetailsSp(ch){
	if (ch == 'on'){
		$("#dettagliOff").css({'display':'none'});
		$("#dettagliOn").slideDown("slow");
	}
	else  {
		$("#dettagliOn").slideUp("fast");
 		$("#dettagliOff").css({'display':''});
	}
}
