var cntt = 1;
var cnfg = 1;

function init_ie6_fixes(){
	if (!/MSIE (5\.5|6\.)/.test(navigator.userAgent)) return;
	else{
		try{
			h = document.getElementsByTagName('head')[0];
		    var q = document.createElement('script');
			q.src = '/includes/js/iepngfix_tilebg.js';
			q.type = 'text/javascript';
			q.id = "jqueryelement";
			h.appendChild(q);
		} catch (e){
			alert(e);
		}
	}
}

init_ie6_fixes()

function toggle_tr(show,hide)
{
	var blk = "table-row";
	if(document.all) blk = "block";		
	
	var cssObj1 = {
		display : blk
	}
	
	var cssObj2 = {
		display : "none"
	}
	
	if(show) $("tr."+show+"").css(cssObj1);
	if(hide) $("tr."+hide+"").css(cssObj2);
	
}

// popup code  
var UTIL = UTIL || {};
UTIL.popup = UTIL.popup || {};
UTIL.popup.open = function (href, params)
{
	// defaults
	var defaultParams = {
		"width":       "800",   // Window width
		"height":      "600",   // Window height
		"top":         "0",     // Y offset (in pixels) from top of screen
		"left":        "0",     // X offset (in pixels) from left side of screen
		"directories": "no",    // Show directories/Links bar?
		"location":    "no",    // Show location/address bar?
		"resizeable":  "yes",   // Make the window resizable?
		"menubar":     "no",    // Show the menu bar?
		"toolbar":     "no",    // Show the tool (Back button etc.) bar?
		"scrollbars":  "yes",   // Show scrollbars?
		"status":      "no"     // Show the status bar?
	};
	var windowName = params["windowName"] || "new_window";
	var i, useParams = "";
	for (i in defaultParams)
	{
		useParams += (useParams === "") ? "" : ",";
		useParams += i + "=";
		useParams += params[i] || defaultParams[i];
	}
	return window.open(href, windowName, useParams);
};

$(document).ready(function(){
	
	$('body').removeClass('no_JS');
	
	$('.testimonial-toggler').click(function(){
	
		if(!(cntt % 2)){
			$('#tt-heading').removeClass('active').html('Testimonials <span>(click here)</span>');
			$('#team-wrap').show();
			$('#testimonial-wrap').hide();
		}
		else{
			$('#tt-heading').addClass('active').html(' ');
			$('#team-wrap').hide();
			$('#testimonial-wrap').show();
		}
		cntt++;
		
	});
	
	$('#faqgls-button').click(function(event){
		event.preventDefault();
		if(!(cnfg % 2)){
			newpos = '0px 0px';
			$('#faq-wrapper').show();
			$('#glossary-wrapper').hide();
		}
		else{
			newpos = '0px -28px';
			$('#faq-wrapper').hide();
			$('#glossary-wrapper').show();
		}
		cnfg++;
		$(this).css({backgroundPosition: newpos});
	});
	
	$('#glossary-wrapper>p').click(function () {
		$(this).find('span').toggle();
	});
	
	$('#faq-wrapper>p').click(function () {
		$(this).find('span').toggle();
	});
	
	$('select[name=x13]').change(function() {
		if($(this).val() == "2")
		{
			toggle_tr('cashout-row','streamline-row');
		}
		else if($(this).val() == "3")
		{
			toggle_tr('streamline-row','cashout-row');
		}
		else{
			toggle_tr('','cashout-row');
			toggle_tr('','streamline-row');
		}
	});
	
	$('#lkbutton').click(function(event){
		event.preventDefault();
		var errmsg = '';
		if($('select[name=x13]').val() == "2"){
			if($('#x13a').val() == '') errmsg = "Cashout Amount is a required field\n";
		}
		if($('#x16').val() == '') errmsg = errmsg + "Loan Amount is a required field\n";
		if($('#x17').val() == '') errmsg = errmsg + "Property Value is a required field\n";
		if($('#x14').val() == '') errmsg = errmsg + "Zip Code is a required field\n";
		if(errmsg == ''){
			$('#lkform').submit();
		}
		else{
			alert(errmsg);
		}
	});
	
	$('div.badge-apply-now').hover(
		function(){
			$(this).css({'position':'relative'});
			$(this).append('<span style="position:absolute;top:62px;left:197px;display:block;width:28px;height:29px;"><a href="https://www.secureloandocs.com/apply.php?id=65157157" style="display:inline;margin:0;padding:0;"><img src="images/dbover.png" border="0" /></a></span>');
		},
		function(){
			$(this).find('span:last').remove();
		}
	);
	
	$("a.popup").each(function (i){
		$(this).click(function(event) {
			event.preventDefault();
			var params = {width:660,height:700,top:document.documentElement.clientHeight/2-350,left:document.documentElement.clientWidth/2-330};
			if ($(this).attr("target")){
				params.windowName = $(this).attr("target");
			}
			var windowObject = UTIL.popup.open(this.href, params);
			$(this).data("windowObject", windowObject);
		});
	});
	
	$("#x10").mask("(999) 999-9999");

});
