var $j = jQuery.noConflict();

$j(function(){
	if($j('#belongsToFellow').is('table')){
		crtlSocio();
	}
	if($j('#goToSchoolBooks').is('table')){
		schoolSubmit();
	}
	if($j('#goToBooks').is('table')){
		getEncryptedData();	
	}	
	if($j('#iFramePage').is('table')){
		setIframeUrl();
		$j("#iFrame").load(function(){
		checkQueryStringHash();
		});
	}	
});
function crtlSocio(){
	var codicesocio = $j('#codicesocio').val();
	var codicetessera = $j('#codicetessera').val();
	$j.ajax({
	  type: "POST",
	  url: '/include/lib/crtl_socio_call.php',
	  data: "codicesocio="+codicesocio+"&codicetessera="+codicetessera+"&control=yes",
	  success: function(msg) {
	    if(msg == 1){
		//	alert(typeof(msg));
			window.location.replace('home.php?sett=libri&srvtp=lbrsc&pag=no_socio');
		} else {
			window.location.replace(msg);
		}
	  }
	});
};
var elWidth = function(){
	$j("#iFrame").load(function(){
		el = window.frames[0].document.getElementById("panelSearch");
		alert(el.width);
	});
};
function getEncryptedData(){
		var enUid;
		var ui;
		var pvc;
		var enPvc;
		var enDebug;
		var userId = $j('#userId').val();
		var pvCode = $j('#pvCode').val();
		var debug = $j('#debug').val();
		if(userId){
			$j.ajax({
				type: "POST",
				url: "/include/lib/encrypt.php",
				dataType:"json",
				data: "userId="+userId+"&pvCode="+pvCode+"&debug="+debug,
				success: function(msg){
					jData = msg;
					if(!jData.wrong){
						uid = jData.userId;
						enUid = jData.uiDec;
						pvc = jData.pvCode;
						enPvc = jData.pvDec;
						enDebug = jData.debugDec;
					} else {
						alert(jData.wrong);
					}
			//		alert('enUid: "'+enUid+'" ui: "'+uid+'" enPvc: "'+enPvc+'" pvc: "'+pvc+'" enDebug: "'+enDebug+'" debug: "'+jData.debug.trim()+'"');
					sendToIframe(enUid, enPvc, enDebug);
				}
			});
		} else {
			alert('Il servizio potrebbe essere indisponibile. Riprova in seguito');
		}
		return false;
};
function sendToIframe(uid, pvc, debug){
	var cipherMode = "CBC";
	$j('#getData').click(function(){
		if(uid){
			this.href = 'home.php?sett=libri&srvtp=lbrnr&idCliente=' + uid + '&IdNegozio=' + pvc+'&debug='+debug+'&cat=cop&cipherMode='+cipherMode;
		} else {
			alert('A problem occured please retry');
		}
	});
	return true;
};
function setIframeUrl(){
	var encrypted = queryStringGet('idCliente');
	var cipher = queryStringGet('cipherMode');
	var debug = queryStringGet('debug');
	var pvc = queryStringGet('IdNegozio');
//	var url = 'http://gdo.edicolibri.it/VerifyUser.aspx?CipherMode=CBC&IdNegozio='+pvc+'&IdCliente='+encrypted+'&debug='+debug+'&cat=cop';
	var url = 'http://gdo.edicolibri.it?IdNegozio='+pvc+'&IdCliente='+encrypted+'&debug='+debug+'&cat=cop';
	$j("#iFrame").attr('src', url);
	$j("#iFrame").load(function(){	
		$j("#loadingContainer").hide();
	});
};
function checkQueryStringHash(){
	var checkID = setInterval(function() {	
	if (window.location.hash) {
		getResponseFromIframe();
		clearInterval(checkID);
		}
	}, 100);
};
function getResponseFromIframe() {
//	alert('Credenziali passate. Hash is '+window.location.hash);
	var hash = window.location.hash;
	var quantity = hash.split('cart')[1];
	$j('.BOARD_libri_lavagna_txt > span').text(quantity);
//	alert('Credenziali passate. Hash is '+quantity);
};
var queryStringGet = function ( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
};
String.prototype.trim = function() {
   return this.replace(/^\s+|\s+$/g,"");
};
///*SCHOOL BOOKS*////
function showSecondRequest(formData, jqForm, options) { 
    var queryString = $j.param(formData); 
 /*   alert('About to submit: \n\n' + queryString);*/
    return true; 
};
// post-submit callback 
function showResponse(responseText, statusText, xhr, $form)  {
  /*  alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
        '\n\nStai per essere redirezionato allo shop dei libri scolastici.');*/
 		window.location.replace(responseText);
};
function schoolSubmit(){
	var options = {
        beforeSubmit:  showSecondRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
    }; 
    $j('#autenticate').ajaxForm(options);
};