/* ################################## */
/*                                    */
/* Samenmais                          */
/* Global Javascript (Top Level)      */
/*                                    */
/* Author: Matt Milburn               */
/* matt@wavemotionstudios.com         */
/* Last Updated: 10.26.2009           */
/*                                    */
/* ################################## */

function window_init() {
	try {
		var pageTracker = _gat._getTracker("UA-4045878-1");
		pageTracker._initData();
		pageTracker._trackPageview();
	} catch (e) {}
	
	/* ---------- */
	
	fix_height();
	try {window.onresize = fix_height}
	catch (e) {document.body.onresize = fix_height}
	
	/* ---------- */
	
	if (document.images) {
		var preloaded = new Array();
		var my_images = new Array(
			"images/btn_home_2.gif",
			"images/bg_btn_2.gif",
			"images/bg_nav_a_2.gif",
			"images/btn_ordernow_2.gif",
			"images/btn_freedemo_2.gif",
			"images/btn_turnover_2.gif",
			"images/btn_buynow_2.gif",
			"images/btn_report_2.gif",
			"images/btn_fullreport_2.gif"
		);
		
		for (var i = 0; i < my_images.length; i++) {
			preloaded[i] = new Image();
			preloaded[i].src = my_images[i];
		}
	}
	
	/* ---------- */
	
	if (Prototype.Browser.IE) {
		suckerfish();
		
		$$("input[type='radio']", "input[type='checkbox']").each(function(t) {
			t.addClassName("noborder");
		});
	}
	
	/* ---------- */
	
	$$("a[rel='control_modal_img']").each(function(t) {
		new Control.Modal(t, {className: "control_window", fade: true, overlayOpacity: .75, fadeDuration: 1});
	});
	$$("a[rel='control_modal_win']").each(function(t) {
		new Control.Modal(t, {width: 720, height: 480, className: "control_window", iframe: true, fade: true, overlayOpacity: .75, fadeDuration: 1});
	});
}

function fix_height() {
	try {
		$("body_main").style.minHeight = (window.innerHeight - 390) + "px"; //Gecko
	} catch (e) {
		if (typeof(document.body.style.maxHeight) != "undefined")
			$("body_main").style.minHeight = (document.documentElement.clientHeight - 390) + "px"; //IE7
		else
			$("body_main").style.height = (document.documentElement.clientHeight - 390) + "px"; //IE6
	}
}

function suckerfish() {
	if ($("navigation"))
		$A($("navigation").getElementsByTagName("li")).each(function(t) {
			t.onmouseover = function() {$(this).addClassName("ie6_hover")};
			t.onmouseout = function() {$(this).removeClassName("ie6_hover")};
		});
}

function rollover(t, i, n) {
	/*
	//prefix_label_n.ext
	var a = t.src.split("."); // [prefix_label_n, ext]
	var b = a[0].split("_"); // [prefix, label, n]
	b[2] = b[2] == 1 ? 2 : 1;
	a[0] = b.join("_");
	t.src = "images/" + a.join(".");
	*/
	t.src = "images/btn_" + i + "_" + n + ".gif";
}

function validate_fields(f) {
	for (var i = 0; i < f.length; i++) {
		if (!$(f[i]))
			return false;
		if (($(f[i]).type == "text" || $(f[i]).type == "hidden") && $F(f[i]) == "")
			return false;
		if (($(f[i]).type == "checkbox" || $(f[i]).type == "radio") && $F(f[i]) == null)
			return false;
		if ($(f[i]).tagName == "SELECT" && $F(f[i]) == "NULL")
			return false;
		if ($(f[i]).tagName == "TEXTAREA" && $F(f[i]) == "")
			return false;
	}
	
	return true;
}

function commatize(n) {
	var c = 0;
	var a = n.toString().split(".");
	var t = "";
	
	for (var i = a[0].length - 1; i >= 0; i--) {
		if (c == 3) {
			t += "," + a[0].charAt(i);
			c = 1;
		} else {
			t += a[0].charAt(i);
			c++;
		}
	}
	
	t = t.split("").reverse().join("");
	
	return t + "." + a[1];
}

function decommatize(s) {
	return s.replace(",", "");
}



/* ---------------------------------------------------------------------------------------------------- */



function submit_login() {
	var fields = new Array("login_user", "login_pass");
	
	if (validate_fields(fields))
		document.sam_form.submit();
	else
		alert("Please provide a username and password");
}

function submit_contact() {
	var fields = new Array("contact_name", "contact_email", "contact_subject", "contact_message", "captcha_validation");
	
	if (validate_fields(fields))
		document.sam_form.submit();
	else
		alert("Please fill out all fields");
}

function submit_consultants_app() {
	var fields = new Array("name", "company_name", "ssn_tid", "address", "phone", "email", "website", "country", "contact_time_start", "contact_time_end", "contact_time_days");
	var agreements = new Array("accept_contractor", "accept_terms", "accept_process");
	var a = new Array("business", "consulting", "insurance", "sales", "edu", "hr", "other");
	
	if ($F("applying_as_2") != null) fields.push("applying_as_proof");
	if ($F("representing_others_2") != null) fields.push("representing_others_specify");
	
	for (var i = 0; i < a.length; i++) {
		if ($F("exp_" + a[i]) != null) {
			fields.push("exp_" + a[i] + "_time");
			fields.push("exp_" + a[i] + "_place");
		}
	}
	
	if ($F("exp_business") != null) fields.push("exp_business_specify");
	if ($F("exp_sales") != null) fields.push("exp_sales_specify");
	if ($F("exp_other") != null) fields.push("exp_other_specify");
	
	if (validate_fields(agreements))
		if (validate_fields(fields))
			document.sam_form.submit();
		else
			alert("Please fill out ALL fields.");
	else
		alert("You must accept the agreement of this application before continuing.");
}

function toggle_tabs(t) {
	var l = t.id.replace(/[0-9]+/, "");
	var n = t.id.replace(/[^0-9]+/, "");
	
	var i = 1;
	while ($(l + i)) {
		if (i == n) {
			$(l + i).addClassName("tab_focus");
			$(l + i + "_content").show();
		} else {
			$(l + i).removeClassName("tab_focus");
			$(l + i + "_content").hide();
		}
		
		i++;
	}
}