var fullpath = String(window.location);
var first = fullpath.substring(0, fullpath.indexOf("//"));  //first is set to http: or https:
var rest = fullpath.substring(first.length+2); //rest is set to webdev/pa/billing/whatever/yadda/yadda
var domain = rest.substring(0, rest.indexOf("/")); //domain is set to webdev or secureindia or what have you
var path = first + "//" + domain;

function Submit_Lead() {
	var frm = document.forms['pacom'];
	var str = "";
	$("input",frm).each(function(i) {

		if(this.value) str += this.id + "=" + this.value + "&";

	});
	str = str.substring(0,str.length-1);

	$.ajax({
		type:		"POST",
		url:		"/scripts/go_lead_ajax.php",
		data:		str,
		dataType:	"json",
		success:	updateLeadForm
	});

}

function updateLeadForm(json) {

	if(json.success) {
		hbx_lead(); //call the hbx_lead function which 'submits' the form through hbx
		window.location = "/pa/thank_you";
	}

	if(json.errors) {
		$("#lead_errors").show();
		$("#lead_errors").html("");

		var error_string = "";

		if(typeof json.errors == "object") { //this is an array of error messages
			json.errors.foreach(function(v,k) {
				error_string += "<strong>"+v+"</strong><br/>\n";
			});
		} else {
			error_string = "<strong>"+json.errors+"</strong><br/>\n";
		}

		$("#lead_errors").html(error_string);

	} else $("#lead_errors").hide();

}

function clearPinkBG(obj) {
	obj.style.background = '';
}

function onLoads() {

	try{
		//setShipping();
		checkScrollPosition2('', 2);
	} catch(e) { }

}

// /pa/affiliates
$(document).ready(function() {
	if($("#affiliates #errors").get(0)) {
		$("#affiliates").show();
	} else {
		$("#affiliates").hide();
	}
});

function toggleAffiliateForm() {
	$("#affiliates").toggle();
}

$(document).ready(function() {
	$('#formToggle').click(function() {
		$('#main_lead_form').toggle();
	});
});
