// Java Script Document
$(document).ready(function(){
						   
	$("input[name=\"BillingStatus\"]").attr('checked') ? $("#BillingLegal").show() : $("#BillingLegal").hide();
	
	$("input[name=\"BillingStatus\"]").click(function(){
		$(this).attr('checked') ? $("#BillingLegal").show() : $("#BillingLegal").hide();
		$(this).attr('checked') ? UserDNINIF.enable() : UserDNINIF.disable();
	});
	
	if($('#CreateAccount').size() > 0){
		
		$('#CreateAccount input[name="EMail"]').keyup(function(){
			var UserEmail = new LiveValidation('EMail');
				UserEmail.add(Validate.Presence);
				UserEmail.add(Validate.Exclusion, {within:['Insertar E-Mail']});
				UserEmail.add(Validate.Email);
			if(LiveValidation.massValidate([ UserEmail ])){
				var xml_result;
				var url = 'includes/bc/';
				url += '?do=checkMail';
				url += '&rc_user_email='+$(this).val();
				url += '&sid='+Math.random();
				xml_result = new makeAJAX();
				xml_result.open('GET', url, true);
				xml_result.onreadystatechange = 
				function(){
					if(xml_result.readyState == 4){
						if(xml_result.status == 200){
							if(xml_result.responseText == 'OK'){
								show_alert('Ya existe un usuario con esta dirección de E-Mail');
								$('#CreateAccount input[name="EMail"]').val('Insertar E-Mail');
							}
						}else{
							alert("no existe url");	
						}
					}
				 }
				 xml_result.send(null);	
			}
		});
		
		$('#CreateAccount input[name="Send"]').click(function(){
		var UserFirstName = new LiveValidation('FirstName');
			UserFirstName.add(Validate.Presence);
			UserFirstName.add(Validate.Exclusion, {within:['Insertar Nombre']});
			
		var UserLastName = new LiveValidation('LastName');
			UserLastName.add(Validate.Presence);
			UserLastName.add(Validate.Exclusion, { within: ['Insertar Apellidos'] });
			
		var UserDNINIF = new LiveValidation('DNI/NIF');
			UserDNINIF.add(Validate.Presence);
			UserDNINIF.add(Validate.Exclusion, { within: ['Insertar DNI/NIF'] });
		
		$("input[name=\"BillingStatus\"]").attr('checked') ? UserDNINIF.enable() : UserDNINIF.disable();
		
		$("input[name=\"BillingStatus\"]").click(function(){
			$(this).attr('checked') ? UserDNINIF.enable() : UserDNINIF.disable();
		});
			
		var UserEmail = new LiveValidation('EMail');
			UserEmail.add(Validate.Presence);
			UserEmail.add(Validate.Exclusion, {within:['Insertar E-Mail']});
			UserEmail.add(Validate.Email);
			
		var UserTelephone = new LiveValidation('Telephone');
			UserTelephone.add(Validate.Numericality, {onlyInteger: true});
			
		var UserMobilephone = new LiveValidation('Mobilephone');
			UserMobilephone.add(Validate.Numericality, {onlyInteger: true});
			
		var UserFax = new LiveValidation('Fax');
			UserFax.add(Validate.Numericality, {onlyInteger: true});
			
		var Acou = new LiveValidation('acou');
			Acou.add(Validate.Acceptance);
			
		var AddressBookStreet = new LiveValidation('Street');
			AddressBookStreet.add(Validate.Exclusion, {within:['Insertar Calle']});
			AddressBookStreet.add(Validate.Presence);
			
		var AddressBookCP = new LiveValidation('PostCode');
			AddressBookCP.add(Validate.Presence);
			AddressBookCP.add(Validate.Numericality, { onlyInteger: true });
			AddressBookCP.add(Validate.Numericality, { minimum: 1000, maximum: 54999 });
			AddressBookCP.add(Validate.Exclusion, { within:['00000'] });
			
		var password = new LiveValidation('Password');
			password.add(Validate.Presence);
			password.add(Validate.Exclusion, {within:['password']});
			
		var password2 = new LiveValidation('Password2');
			password2.add(Validate.Presence);
			password2.add(Validate.Confirmation, { match: 'password'});
			password2.add(Validate.Exclusion, {within:['password']});
			
		var p = $('#CreateAccount select[name="ProvinciaId"]').val();
		var m = $('#CreateAccount select[name="MunicipioId"]').val();
			
		var SignUpOk = LiveValidation.massValidate([ UserFirstName, UserLastName, UserDNINIF, UserEmail, Acou, AddressBookStreet, AddressBookCP, password, password2 ]);
			
		if(!SignUpOk || !p || !m){
			var $alert 										= 'Revise los campos obligatorios';
			show_alert($alert);
		}else{
			document.getElementById('create_account').submit();
		}
	});

	}
	
	$("#product_form input[name=\"QuantityProduct\"]").keyup(function(){
		var QuantityCheck = new LiveValidation('QuantityProduct');
		QuantityCheck.add( Validate.Numericality, { onlyInteger: true } );
		QuantityCheck.add( Validate.Numericality, { minimum: 1, maximum: 999 } );
		if(LiveValidation.massValidate([ QuantityCheck ])){
			$("#product_form input[name=\"button\"]").attr('disabled', false);
		}else{
			$("#product_form input[name=\"button\"]").attr('disabled', true);
		}
	});						   
});

function divStatus(divId, status){
	var divId, status;
	if(status){
		document.getElementById(divId).style.visibility		= 'visible';
		document.getElementById(divId).style.display		= 'block';
		document.getElementById(divId).style.height			= '100%';
		document.getElementById(divId).style.width			= '100%';
	}else{
		document.getElementById(divId).style.visibility		= 'hidden';
		document.getElementById(divId).style.display		= 'none';
		document.getElementById(divId).style.height			= '0%';
		document.getElementById(divId).style.width			= '0%';
	}
}

function section_select(div_id, back_window){
	var div_id, back_window, b_win, main, divs, div;
	if(back_window == 0){
		b_win ='main_window'; 
	}else{
		b_win ='msg_window';
	}
	main = document.getElementById(b_win);
	divs = main.getElementsByTagName('div');
	for(var i=0; i<divs.length; i++){
		div = main.getElementsByTagName('div')[i].getAttribute('id');
		if(div == div_id){
			main_start(1, b_win, 1);
			window_status(div,1);
		}else{
			window_status(div,0);
		}
	}
}

function showOrder(OrderId){
	doOrder(OrderId);
}

function MyAccountMenu(Option){
	var Option, i, Section = new Array();
	Section[0] = 'EditUserData';
	Section[1] = 'EditUserAddress';
	Section[2] = 'EditUserPassword';
	if(document.getElementById('ShowUserOrders')){Section[3] = 'ShowUserOrders';}	//Puede no existir pedido
	for(i=0; i<Section.length; i++){
		if(Section[i] == Option){
			divStatus(Section[i],1);
		}else{
			divStatus(Section[i],0);
		}
	}
}

function window_info(){
	var div_id, height;
	div_id					= 'main_container';	
	height 					= document.getElementById(div_id).offsetHeight + 30;		// Sumamos Padding/Margin
	return height;
}

function main_start(data, b_win, dimmer){
	var data, b_win, dimmer;
	if(dimmer){window_status('dimmer_show',data)}
	window_status(b_win ,data);
}

function window_close(back_window){
	var back_window;
	var mw_status 		= document.getElementById('main_window').style.visibility;
	var msw_status 		= document.getElementById('msg_window').style.visibility;
	var b_win			= back_window ? 'msg_window' : 'main_window';
	var main 			= document.getElementById(b_win);
	var divs 			= main.getElementsByTagName('div');
	for(var i=0; i<divs.length; i++){
		var div_id 		= main.getElementsByTagName('div')[i].getAttribute('id');
		var dimmer 		= (b_win =='msg_window' && mw_status == "visible" || b_win =='main_window' && msw_status == "visible") ? 0 : 1;
		window_status(div_id,0);
	}
	main_start(0, b_win, dimmer);
}

function window_status(divId,status){
	var status, divId;
	if(status){
		divStatus(divId, status);
		if(divId == 'dimmer_show')document.getElementById(divId).style.height	= window_info();	
	}else{
		divStatus(divId, status);
	}
}

function ContactUs(){
	section_select('ContactUs',1);
}

function AboutUs(){
	section_select('AboutUs',1);
}

function CDU(){
	section_select('CDU',1);
}

function show_alert(a){
	document.getElementById('msg_text').innerHTML = a;
	section_select('product_added',1);
}

function show_msg(a){
		document.getElementById('msg_text').innerHTML = a.getElementsByTagName('msg_text')[0].childNodes[0].nodeValue;
		section_select('product_added',1);
}

function product_select(product_ref){
	var product_ref;
	w_product_data(product_ref);
}

	
function w_product_data(product_ref){
	var product_ref, xml_result;
	var url = 'includes/bc/';
	url += '?do=product_XML';
	url += '&product_ref='+product_ref;
	url += '&sid='+Math.random();
	xml_result = new makeAJAX();
    xml_result.open('GET', url, true);
	xml_result.onreadystatechange = 
	function(){
		if(xml_result.readyState == 4){
			if(xml_result.status == 200){
				readDato(xml_result.responseXML);
			}else{
				alert("no existe url");	
			}
		}
	 }
     xml_result.send(null); 
}
	
function readDato(a) 
{ 
	 var Name 			= a.getElementsByTagName("product_name")[0].childNodes[0].nodeValue;
	 var DescriptionText= a.getElementsByTagName("product_description")[0].childNodes[0].nodeValue;
	 var Description 	= '<span style="text-align:justify">'+DescriptionText.replace(/-br-/g, "<br>")+'</span>';
	 var ProductComment	= a.getElementsByTagName("product_comment")[0].childNodes[0].nodeValue;
	 var ImageData		= a.getElementsByTagName("product_image")[0].childNodes[0].nodeValue;
	 var ImageBody		= '<img src="images/DATA/products/'+ ImageData +'" width="200" height="250" align="left"/>';
	 var ProductRef		= a.getElementsByTagName("product_ref")[0].childNodes[0].nodeValue;
	 var ProductPrice	= a.getElementsByTagName("product_price")[0].childNodes[0].nodeValue;
	 
	document.getElementById('msg_window_product_name').innerHTML 		= Name;
	document.getElementById('msg_window_product_description').innerHTML = Description;
	document.getElementById('msg_window_product_comment').innerHTML 	= ProductComment;
	document.getElementById('msg_window_product_image').innerHTML 		= ImageBody;
	document.getElementById('msg_window_product_ref').value				= ProductRef;
	document.getElementById('msg_window_product_price').innerHTML 		= ProductPrice;
	
	//METADATA
	var MetaTags 				= new Array();
	MetaTags['description']		= DescriptionText;
	MetaTags['keywords']		= Name;
	
	Meta(MetaTags);
	
	section_select('product_window',1);
}

function Meta(input){
	var input;
	$('meta[name="description"]').attr('content', input['description']);
	$('meta[name="keywords"]').attr('content', input['keywords']);
}

function formatText(Text){
	var TagList 		= new Array();
	TagList['-:']	= '<';
	TagList[':-']	= '>';
	while(TagList){
		alert('Tomat');	
	}
}