// URL of cl_widget folder
var cl_widget_service = 'http://www.ratecity.com.au/widgets/cl/';

// ensure that cl_load_jquery is set to a valid value - true by default
if (typeof cl_load_jquery == 'undefined' ||  (cl_load_jquery != false && cl_load_jquery != true))
	cl_load_jquery = true;

//var cl_debug = true;
//var cl_dist_id = 1;

var success = { "error": true, "errorMsg": "Unable to contact widget host." };

function insertCLWidgetStyleDOM()
{
	var headID = document.getElementsByTagName("head")[0];         

	var cssNode = document.createElement('link');
	cssNode.type = 'text/css';
	cssNode.rel = 'stylesheet';
	cssNode.href = cl_widget_service + 'css/cl_widget.css';
	headID.appendChild(cssNode);
}

function insertWidgetStyleDom()
{
	insertCLWidgetStyleDOM();
}

function loadWidget()
{
	var headID = document.getElementsByTagName("head")[0];
	var newScript = document.createElement("script");

	newScript.type = "text/javascript";
	newScript.text = "renderWidget();";
	headID.appendChild(newScript);
} 

// this function will halt initialization of the cl_widget until
// jQuery has been loaded
function polljQuery()
{
	var headID = document.getElementsByTagName("head")[0];
		
	if (typeof jQuery == 'undefined'){
		window.setTimeout(polljQuery, 100);
	}
	else
	{
		authWidget();
	}
} 

function authWidget()
{
	// set this as the default json data in case we can't contact host

    if (typeof cl_dist_id == 'undefined')
        cl_dist_id = '';

	jQuery.ajax({ url: cl_widget_service + 'cl_auth.php',
						cache: false,
						type: 'GET',
						data: 'cl_dist_id=' + cl_dist_id + '&cl_domain=' + document.domain,
						dataType: "jsonp",
						success: function(json) { 
																			initWidget(json);
																		}
				});
}

// jQuery libraries loaded & available, render widget
function initWidget(authOk)
{
	// authentication
	var success = authOk;
	//var success = { "error": true, "errorMsg": "Car Loan widget authentication failed." };

	if (success.error == 1)
	{
		if (cl_debug == 1)
		{
			document.getElementById('cl_widget_container_div').innerHTML = '<p>' + success.errorMsg + '</p>';
		}
			
		return false;
	}
	else
	{
		insertWidgetStyleDom();
		loadWidget();
	}
}

function renderWidget()
{

	jQuery(document).ready( function () {
		jQuery.ajax({    url: cl_widget_service + "forms/frm_cl_widget.php", 
                cache: false,
                type: "GET",
                dataType: "jsonp",
                success: function(html) {
										init_form(html.data); 
										jQuery(document).ready( function () {jQuery('#cl_widget_container_div').css('display','block');});
										}
            });
				
	});

	document.close();
}
                
                
                
function init_form(markup) 
{ 
    jQuery('#cl_widget_container_div').html(markup); 
    jQuery('document').ready( function() 
            {
                jQuery('#search_button').click ( function(e) { set_search_button(e); });
            });
}

function set_search_button(e)
{
    e.preventDefault();
    var amount = jQuery('#amount').val();	
    var loan_type = jQuery('input:radio[name=loan_type]:checked').val();
    var state = jQuery('#state').val();
    var loan_term = jQuery('input:radio[name=loan_term]:checked').val();
    var domain = document.domain;

    var get_params  = 'p_amount=' + amount + '&p_loan_type=' + loan_type + 
                      '&p_state=' + state + '&p_loan_term=' + loan_term + '&p_domain=' + domain;	

    if (amount.length == null || typeof loan_type == 'undefined' || state.length  == null || typeof loan_term == 'undefined' )
    {
        alert('Please enter input for all fields');
        return false;
    }
		
		if (isNaN(amount))
		{
			alert('Please enter only numbers in the amount field.');
			return false;
		}


		if (amount <= 0 || amount > 10000000)
		{
			alert('Please enter a positive value no greater than 10000000');
			return false;
		}

    window.open(cl_widget_service + 'cl_search.php?' + get_params ,'Results','location=0,toolbar=0,menubar=0,scrollbars=yes,top=120,left=760,resizable=1,width=820,height=650');				
}



document.writeln("<!-- RateCity cl_widget start -->");
document.writeln("<div style=\"display:block\" id=\"cl_widget_container_div\" class=\"cl_widget_container\"></div>");
document.writeln("<!-- RateCity cl_widget end -->");

if (cl_load_jquery == 1)
{
	var headID = document.getElementsByTagName("head")[0];

	var newScript = document.createElement("script");

	newScript.type = "text/javascript";
	newScript.src = cl_widget_service + "js/jquery/jquery-1.4.1.js";
	headID.appendChild(newScript);

	polljQuery();
}
else
{
	if (typeof jQuery != 'undefined'){
		authWidget();
	}
	else
	{
			document.getElementById('cl_widget_container_div').innerHTML = '<p>cl_load_jquery disabled but no jQuery library is currently loaded</p>';
	}
}


