
// JavaScript Document


function startup()
{
	ajax("0", "0", "0"); 
}

function changeFormElements(ac) 
{
	var control = document.getElementById("cform").elements;
	
	for(var i=0; i<control.length; i++) 
	{
		if(control[i].type.match(/select/i) || control[i].type.match(/radio/i) || control[i].type.match(/checkbox/i)) 
		{
			control[i].disabled = ac;
		}
	}
	
}

function noMixing(item_type_id, item_id, item_id_value)
{
	ajax(item_type_id, item_id, item_id_value);
}

function pic(item_type, new_image_url)
{
	var i = document.getElementById("change_pic_"+item_type);
	i.src = new_image_url;
	//alert(i.src);
	valign("singleImage", "rightTopBox");
}

function reset_pic(item_type, new_image_url)
{
	var i = document.getElementById("change_pic_"+item_type);
	i.src = new_image_url;
	//alert(i.src);
}	


function unobstructive_login(email_address, password, action)
{
	//alert('Item Type ID: '+item_type_id);
	//alert('Item ID: '+item_id);
	//alert('Value: '+item_id_value);
	//document.getElementById("Panel1").style.display="none";
	//document.getElementById("unobstructive_login_holder").style.display="none";
	
	var xmlHttp;
	try
	  {
		  // Firefox, Opera 8.0+, Safari
		  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
		  // Internet Explorer
		  try
			{
				xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
			}
		  catch (e)
			{
				try
				  {
					  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
				  }
				catch (e)
				  {
					  alert("Your browser does not support AJAX!");
					  return false;
				  }
			}
	  }
	
	//If action is send email then get email address value from text box =============
	if(action=='create')
	{
		var email_address = document.getElementById("create_email").value;
		var password = document.getElementById("create_password").value;
		//alert(email_address);
	}
	
	if(action=='login')
	{
		var email_address = document.getElementById("login_email").value;
		var password = document.getElementById("login_password").value;
		//alert(email_address);
	}
	
	if(action=='forgot_password')
	{
		var email_address = document.getElementById("login_email").value;
	}
	//If action is send email then get email address value from text box =============
	
	
	
	// each time the readystate changes...
	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4 && xmlHttp.status == 200)
		{
			var changeDiv = document.getElementById("unobstructive_login_holder");
			document.getElementById("Panel1").style.display="block";
			document.getElementById("unobstructive_login_holder").style.display="block";
			changeDiv.innerHTML=xmlHttp.responseText;
			
			//If the reply back was Logged in Then uopdate button
			if(xmlHttp.responseText == 'Logged In')
			{
				//alert('logged in');
				
				//Add Hidden Field into form for PHP to detect what we done
				document.getElementById("unobstructive_login_button").innerHTML = '<input name="javascript_add" type="text" value="1" />';
				document.priceTableActions.submit();
			}
		}
	}
	 
	// Random number because  explorer caches the result if its already pulled that link and doesnt bring new content so this creates
	// a unique url every time.
	var randomnumber=Math.floor(Math.random()*100010);

	//Now buid full url
	var url =  "/inc/ajax/unobstructive_login/login.php";
	//alert(url);
	
	var params = "unique="+randomnumber+"&email="+email_address+"&password="+password+"&action="+action;
	//alert(params);
	
	xmlHttp.open("POST",url,true);
	
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	
	xmlHttp.send(params);
}

$().ready(function(){
	$.ajaxSetup({
   		cache: false,
		error:function(x,e){
			if(x.status==0){
				alert('Offline!\n Please Check Network Connectivity.');
			}else if(x.status==404){
				alert('Requested URL not found.');
			}else if(x.status==500){
				alert('Internel Server Error.');
			}else if(e=='parsererror'){
				alert('Error.\nParsing JSON Request failed.');
			}else if(e=='timeout'){
				alert('Request Time out.');
			}else {
				alert('Unknow Error.\n'+x.responseText);
			}
		}
	});
});



function compare(items, mode, view_mode, item_type_name)
{
	// Loading
	$("#item_compare_modal").modal("show");
	$("#item_compare_modal_changer").html("Loading....");
		
	// New Content
	$.get(
		"/inc/ajax/configurator/compare.php",
		{system_id: 2806, items : items, mode : mode, view_mode : view_mode},
		function(data) { 
			$("#item_compare_modal_changer").html(data);
		}, "html"
	);		
}

function limit_compare(mode, view_mode, item_type_name)
{
	var items_array = $('input.limit_compare_checkboxes:checkbox:checked').map(function() {
        return this.value;
    }).get();
    
    var items = items_array.join("|");
	compare(items, mode, view_mode, item_type_name)
}

function item_info(item_id)
{
	// Loading
	$("#item_info_modal").modal("show");
		
	// New Content
	$.get(
		"/inc/ajax/configurator/item_info.php",
		{system_id: 2806, item_id : item_id},
		function(data) { 
			$("#item_info_modal_changer").html(data);
		}, "html"
	);
}

function ajax(item_type_id, item_id, item_id_value)
{
	//Get Current Raid to avoid empty raid box between config and raid ajax requests. Use to populate raid until raid ajax replaces it
	var prevRaid = $("#raid_config_hold").html();
	
	changeFormElements(1);
	$.get(
		"/inc/ajax/configurator/show_configurator-bootstrap.php",
		{system_id: 2806, item_type_id : item_type_id, item_id : item_id, item_id_value : item_id_value},
		function(data) { 
			$('#change_configurator').html(data);
			$("#raid_config_hold").html(prevRaid);
			
			//On first AJAX call there is not yet a right column, in that case assume a hieght of 500 for rightCol
			if($("#rightCol").outerHeight()==0){
				rh = 500
			} else {
				rh = $("#rightCol").outerHeight();
			}
			
			// Chnage right menu offset top
			stop_offset_top = $("#close_sticky_anchor").offset().top - rh;
			//alert(stop_offset_top);
			//alert(rh);
			check_filter_scroll();
			
			var sockets_html = $('#abs_sockets').html();
			$('#change_sockets_repeat .container').html(sockets_html);
			
			changeFormElements(0);
			getPricing();
			
			openRaid();
		}, "html"
	);
}

function change_system_specs()
{
	$.get(
		"/inc/ajax/specifications/system_specifications-bootstrap.php",
		{system_id: 2806},
		function(data) { 
			$('#change_system_specs').html(data); 
			theTableResult = data;
			//document.getElementById("email_system_specs").innerHTML = "<img onClick=\"email_system_specs('ask_email', 'graham');\" src=\"/img/configurator/user_price_box/images/email_quotation.jpg\" />";
		}, "html"
	);
}

function email_system_specs_bootstrap()
{
	if($("#email_email_address").val()=='')
	{
		$("#email_quote_return").html('<div class="alert alert-danger"><strong>Email Address Required.</strong> Please complete</div>');
		$("#email_email_group").addClass("has-error");
	}
	else
	{	
		$("#email_email_group").removeClass("has-error");
	
		$("#email_quote_return").html('<p><i class="fa fa-spinner fa-spin fa-large"></i> &nbsp; Emailing your quote...</p>');
		
		var form_vals = $("#email_quote_form").serialize();
		//alert(form_vals);
		
		var changeDiv = document.getElementById("email_system_specs");
		var content = encodeURIComponent(theTableResult);
		var raid = encodeURIComponent(raidConfigurationVar);
		//alert(content);
		//alert(raid);
	
		var params = form_vals+"&parent_url=&system_id=2806&email_quotation_specs="+content+"&raid="+raid+"&send_action=send_email";
		//alert(params);
		
		$.post(
			"/inc/ajax/specifications/email_specs-bootstrap.php",
			params,
			function(data) { 
				$("#email_quote_return").html(data);
			}, "html"
		);
		
	}
}

function output_pdf_bootstrap()
{
    if($("#pdf_email_address").val()=='')
	{
		$("#create_pdf_return").html('<div class="alert alert-danger"><strong>Email Address Required.</strong> Please complete</div>');
		$("#pdf_email_group").addClass("has-error");
	}
	else
	{	
		$("#create_pdf_return").html('');
		
        $("#pdf_progress").show();
        $("#pdf_progress .progress-bar").animate({width: "100%"}, 2000);
		
		var form_vals = $("#create_pdf_form").serialize();
		//alert(form_vals);
		
		var changeDiv = document.getElementById("email_system_specs");
		var content = encodeURIComponent(theTableResult);
		var raid = encodeURIComponent(raidConfigurationVar);
		//alert(content);
		//alert(raid);
	
		var params = form_vals+"&parent_url=&system_id=2806&email_quotation_specs="+content+"&raid="+raid+"&send_action=send_email&asc=";
		//alert(params);
		
		$.post(
			"/inc/ajax/specifications/create_pdf-bootstrap.php",
			params,
			function(data) { 
				$("#create_pdf_return").html(data);
				$("#pdf_progress").hide();
				$("#pdf_progress .progress-bar").animate({width: "0%"}, 1);
			}, "html"
		);
	}
}

function ask_question()
{
	$("#contact_return").html('<p><i class="icon-spinner icon-spin icon-large"></i> &nbsp; Sendign your Enquiry...</p>');
	
	var form_vals = $("#contact_form").serialize();
	//alert(form_vals);
	
	var params = form_vals+"&parent_url=&system_id=2806";
	//alert(params);
	
	$.post(
		"/inc/ajax/specifications/contact.php",
		params,
		function(data) { 
			$("#contact_return").html(data);
		}, "html"
	);
}

function open_buy_now_modal()
{
    $("#payModal").modal("show");

    var content = encodeURIComponent(theTableResult);
    var raid = encodeURIComponent(raidConfigurationVar);
    //alert("Content: (length:"+content.length+". Str: "+ content);
    //alert(raid);

    var params = "parent_url=&system_id=2806&email_quotation_specs="+content+"&raid="+raid+"&asc="
    //alert("(Length:"+params.length+") "+params);
    
    /* AJAX call to save variables into session array (too big to GET) and call out secure iframe including those variables */
    $.post("/inc/ajax/specifications/buy_now_iframe_changer.php", params, function(data) {
      $("#buy_now_iframe_holder").html(data);
    });
}

/*
    ## Moved to buy_now_secure_form iframe
function buy_now()
{
	if($("#buy_email_address").val()=='')
	{
		$("#buy_now_return").html('<div class="alert alert-danger"><strong>Email Address Required.</strong> Please complete</div>');
		$("#buy_email_group").addClass("has-error");
	}
	else
	{
		$("#buy_email_group").removeClass("has-error");
		
		$("#buy_now_return").html('<p><i class="icon-spinner icon-spin icon-large"></i> &nbsp; Saving your order...</p>');
		
		var form_vals = $("#buy_now_form").serialize();
		//alert(form_vals);
		
		var changeDiv = document.getElementById("email_system_specs");
		var content = encodeURIComponent(theTableResult);
		var raid = encodeURIComponent(raidConfigurationVar);
		//alert(content);
		//alert(raid);
	
		var params = form_vals+"&parent_url=&system_id=2806&email_quotation_specs="+content+"&raid="+raid+"&asc="
		alert(params);
		
		$.post(
			"https://www.broadberry.co.uk/inc/ajax/specifications/buy_now.php",
			params,
			function(data) { 
				$("#buy_now_return").html(data);
			}, "html"
		);
	}
}
*/


function openRaid()
{
	$.get(
		"/inc/ajax/configurator/raid-bootstrap.php",
		{system_id: 2806},
		function(msg) { 
			var tosplit = msg;
			var substr = tosplit.split("--++--");
			var showUser = substr[0];
			var hideUser = substr[1];
			
			$("#raid_config_hold").html(showUser);
			raidConfigurationVar = hideUser;
		}, "html"
	);	
}

function raid(action, variable1, variable2)
{
	//alert(action);
	//alert(variable1);
	//alert(variable2);
	
	if(action=="add_volume") 
	{
		var var_string = "system_id=2806&action="+action+"&qty="+variable1+"&hdds="+variable2;
	}
	
	if(action=="add_to_volume") 
	{
		var var_string = "system_id=2806&action="+action+"&qty="+variable1+"&hdds="+variable2;
	}
	
	if(action=="delete_volume")
	{
		var var_string = "system_id=2806&action="+action+"&delete_volume="+variable1;	
	}
	
	if(action=="select_raid")
	{
		$("#hotspare_modal").modal("hide");
		var var_string = "system_id=2806&action="+action+"&volume="+variable1+"&raid_level="+variable2;
	}
	
	if(action=="add_hotspare")
	{
		var var_string = "system_id=2806&action="+action+"&hdds="+variable2;
	}
	
	if(action=="save_hotspare")
	{
		var var_string = "system_id=2806&action="+action+"&volume="+variable1+"&hdds="+variable2;
	}
	
	if(action=="remove_global_hotspare")
	{
		var var_string = "system_id=2806&action="+action+"&global_hotspare_id="+variable1;
	}
	
	//alert(var_string);
	
	$.ajax({
	  type: "GET",
	  url: "/inc/ajax/configurator/raid-bootstrap.php",
	  data: var_string
	}).done(function( msg ) {
	  
		var tosplit = msg;
		var substr = tosplit.split("--++--");
		var showUser = substr[0];
		var hideUser = substr[1];
		
		$("#raid_config_hold").html(showUser);
		raidConfigurationVar = hideUser;
	});
}


function getPricing()
{
	$.get(
		"/inc/ajax/configurator_prices_bootstrap.php",
		{system_id: 2806},
		function(data) { 
			$('#change_prices').html(data); 
			change_system_specs();
		}, "html"
	);
}

function powerAjax()
{
	$.get(
		"/inc/ajax/configurator/power.php",
		{system_id: 2806},
		function(data) { 
			$('#change_power').html(data); 
		}, "html"
	);
}

function pricingSize(size)
{
	document.getElementById("priceSizeIcon").src = '/img/configurator/user_price_box/images/wait.png';
	$.get(
		"/inc/ajax/configurator_prices_bootstrap.php",
		{system_id: 2806, size: size},
		function(data) { 
			$('#change_prices').html(data); 
		}, "html"
	);
}

function leasing()
{
	$('#configurator_tabs a[href="#pane5"]').tab('show');
	scroll_to('#configurator_tabs');
}

function price_guarantee()
{
	tabSwap('tab_order');
	$('html, body').animate({
		scrollTop: $('#price_guarantee').offset().top
	 }, 1000);
}

function jump_configure()
{
	tabSwap('tab_config');
	$('html, body').animate({
		scrollTop: $('#jump_configure').offset().top
	 }, 1000);
}

function jump_marketing()
{
	tabSwap('tab_marketing');
	$('html, body').animate({
		scrollTop: $('#tab_marketing').offset().top
	 }, 1000);
}


