//script for open a pop up window for confidential callback
function openPopUpWindow(url,winName,features) {
  window.open(url,winName,features);
}

function validate( form ){
  if ( form.contactname.value.length < 1 ) {
    alert( "Please enter your name so that we can get back to you with a response to your question." );
    return false;
  }
  if ( form.contact.value.length < 1 && form.contactphone.value.length < 1) {
    alert( "Please enter an email address or phone number so that we can get back to you with a response to your question." );
    return false;
  }
  if ( form.message.value.length < 1 ) {
    alert( "Please enter a message or question.  Our team is pleased to help." );
    return false;
  }
  return true;
}

//print page function that opens the print window.
function printWindow() {
	bV = parseInt(navigator.appVersion);
	if (bV >= 4) window.print();
}

//open a pop up window for email an article
function openPopUpWindow(url,winName,features) {
  window.open(url,winName,features);
}

//email an article function
function emailanarticle()
{
	var url=document.URL; 
	var title=document.title; 
	var city = "America";
	var script = "bankruptcyscript.js";
	var logo = "logo3.jpg";
	var domain = "america.com";
	var popupwindowurl = "/emailanarticle.php?url="+url+"&city="+city+"&script="+script+"&logo="+logo+"&domain="+domain; 
	openPopUpWindow(popupwindowurl,'','scrollbars=no,resizable=yes,width=450,height=350');
}

function validateemailanarticle( form )
{	
	if ( form.sendername.value.length < 1 ) {
		alert( "Please enter your name." );
		form.sendername.focus();
		return false;
	}
	if ( form.receivername.value.length < 1 ) {
		alert( "Please enter receiver's name." );
		form.receivername.focus();
		return false;
	}
	if ( form.receiveremail.value.length < 1 ) {
		alert( "Please enter receiver's email." );
		form.receiveremail.focus();
		return false;
	}

	return true;
}

//bookmark page function
function bookmarkus() 
{
	if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) 
	{
		var url=document.URL;
		var title=document.title;
		
		window.external.AddFavorite(url,title);
	}
	else 
	{
		var msg = "Press CTRL+D to add a bookmark to this site";
		if(navigator.appName == "Netscape")
		{
			alert(msg);
		}
	}
}
function validateblogquestion(form)
{
	/*
	if ( form.state.value == "State")
	{
		alert( "Please select the State so that we can get back to you with your bankruptcy question" );
		form.state.focus();
		return false;
	}
	*/
	if ( form.subject.value.length < 1 ) 
	{
		alert( "Please enter a subject of your question." );
		form.subject.focus();
		return false;
	}
	if ( form.message.value.length < 1 ) 
	{
		alert( "Please enter a question.  Our team is pleased to help." );
		form.message.focus();
		return false;
	}
	return true;
}

//used in bankruptcy-evaluation.htm
function reloadForProvinces(form)
{
	var advisor_type=form.advisor_types.options[form.advisor_types.options.selectedIndex].value;
	
	self.location='bankruptcy-evaluation.htm?advisor_type=' + advisor_type;
}

function reloadForCities(form)
{
	//var advisor_type=form.advisor_types.options[form.advisor_types.options.selectedIndex].value; 	
	var advisor_type= form.advisor_type.value; //delete this later after add debt consolidation and credit conseling sections
	
	var province=form.provinces.options[form.provinces.options.selectedIndex].value; 
	
	self.location='bankruptcy-evaluation.htm?advisor_type=' + advisor_type + '&province=' + province ;
}
	
function setCityAndSponsor(form)
{
	var sponsor=form.cities.options[form.cities.options.selectedIndex].value;
	
	//var advisorType=form.advisor_types.options[form.advisor_types.options.selectedIndex].value;
	var advisorType= form.advisor_type.value; //delete this later after add debt consolidation and credit conseling sections
	
	var city=form.cities.options[form.cities.options.selectedIndex].text;
	
	if(advisorType != "" && advisorType != "none")
	{
		form.advisor_type.value=advisorType;
		
		if(sponsor != "" && sponsor != "none")
		{
			form.city.value = city;		
		}
		else
		{
			form.city.value = "";
		}
	}	
	else
	{
		form.advisor_type.value="";			
		form.city.value = "";			
	}	
}

function controlVisibility(form)
{
	//var advisor_type=form.advisor_types.options[form.advisor_types.options.selectedIndex].value;
	var advisor_type= form.advisor_type.value; //delete this later after add debt consolidation and credit conseling sections
	
	var province=form.provinces.options[form.provinces.options.selectedIndex].value;
	var city=form.cities.options[form.cities.options.selectedIndex].value;
	
	if(trim(advisor_type).length > 0 && advisor_type != "none")
	{
		showControl("province_row");		
	}
	else
	{
		hideControl("province_row");		
	}
	
	if(trim(province).length > 0 && province != "none")
	{
		showControl("city_row");		
	}
	else
	{
		hideControl("city_row");		
	}
}

function showControl(divId)
{
	if (document.getElementById)
	{
		// this is the way the standards work
		document.getElementById(divId).style.visibility = "visible";		
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		document.all[divId].style.visibility = "visible";		
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		document.layers[divId].visibility = "visible";		 
	}
}

function hideControl(divId)
{
	if (document.getElementById)
	{
		// this is the way the standards work
		document.getElementById(divId).style.visibility = "hidden";
		document.getElementById(divId).style.display = "none"; 
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		document.all[divId].style.visibility = "hidden";
		document.getElementById(divId).style.display = "none"; 
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		document.layers[divId].visibility = "hidden";
		document.getElementById(divId).style.display = "none"; 
	}
}

//removes leading and trailing spaces in the string passed in.
function trim( string_to_trim )
{ 
  while (string_to_trim.charAt(0) == " ")
  { 
    // remove leading spaces 
    string_to_trim = string_to_trim.substring(1); 
  } 
  
  while (string_to_trim.charAt(string_to_trim.length - 1) == " ")
  { 
    // remove trailing spaces 
    string_to_trim = string_to_trim.substring(0,string_to_trim.length - 1); 
  } 
  
  return string_to_trim; 
} 

function validateGeneralQuestionForm(form)
//validates the ask a question form on the general question form
{
	//var advisor_type = trim(form.advisor_types.options[form.advisor_types.options.selectedIndex].value);
	var advisor_type= form.advisor_type.value; //delete this later after add debt consolidation and credit conseling sections
	
	var province = trim(form.provinces.options[form.provinces.options.selectedIndex].value);
	var city = trim(form.cities.options[form.cities.options.selectedIndex].value);
	var othercity = trim(form.othercity.value);
	
	if(advisor_type.length < 1 || advisor_type == "none")
  	{
		alert( "Please select advisor type." );
		return false;
	}
		
	if(province.length < 1 || province == "none")
	{
		alert( "Please select a state." );
		return false;
	}
	
	if(city.length < 1 || city == "none")
	{
		alert( "Please select a city." );
		return false;
	}
	
	if(city == "Other" && othercity.length <1)
	{
		alert( "Please enter specify a city name.");
		return false;
	}
	
	if ( trim(form.name.value).length < 1 ) 
	{
		alert( "Please enter your name so that we can get back to you with an answer to your question." );
		return false;
	}
	
	if ( trim(form.contact.value).length < 1 ) 
	{
		alert( "Please enter your email address or phone number so that we can get back to you with an answer to your question." );
		return false;
	}
	
	if ( trim(form.message.value).length < 1 ) 
	{
		alert( "Please enter your question or message." );
		return false;
	}				
				
	return true;
}

//validate "Other" selection drop down
function validate_select(obj)
{
	if(obj.name == "cities")
	{
		if(obj.value == "Other")
		{
			showLayer('othercitydiv');
		}
		else
		{
			hideLayer('othercitydiv');
		}
	}
}

function showLayer(layerid)
{
	if (document.getElementById)
	{
		// this is the way the standards work
		document.getElementById(layerid).style.visibility = "visible";
		document.getElementById(layerid).style.display = "block"; 
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		document.all[layerid].style.visibility = "visible";
		document.getElementById(layerid).style.display = "block"; 
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		document.layers[layerid].visibility = "visible";
		document.getElementById(layerid).style.display = "block"; 
	}
}

function hideLayer(layerid)
{
	if (document.getElementById)
	{
		// this is the way the standards work
		document.getElementById(layerid).style.visibility = "hidden";
		document.getElementById(layerid).style.display = "none"; 
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		document.all[layerid].style.visibility = "hidden";
		document.getElementById(layerid).style.display = "none"; 
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		document.layers[layerid].visibility = "hidden";
		document.getElementById(layerid).style.display = "none"; 
	}
}

//scripts used on debt consolidation loan calculator (debt-consolidation-loan-calculator.htm)
function loancalculator()
{	
	if(validatecalculator())
	{
		var frm=document.calculatorform;
		
		var loanbalance = parseFloat(frm.loanbalance.value);
		var annualinterestrate = parseFloat(frm.annualinterestrate.value/100);
		var monthlyinterestrate = annualinterestrate/12;
		var termofloan = parseFloat(frm.termofloan.value);
		var termofloanyears = termofloan/12;
		
		//format termofloanyears to two decimal number
		termofloanyearsString = termofloanyears + "";
		if(termofloanyearsString.indexOf('.') != -1)
		{
			termofloanyears = termofloanyears.toFixed(2);
		}
		
		var monthlypayment = Math.floor((loanbalance*monthlyinterestrate)/(1-Math.pow((1+monthlyinterestrate),(-1*termofloan)))*100)/100;
		
		//display result
		frm.monthlypayment.value = monthlypayment.toFixed(2);
		
		//display report
		var data = "You currently have $" + loanbalance.toFixed(2) + " in debts. If you get a consolidation loan at a " + annualinterestrate*100 + "% interest rate, and repay the loan in equal monthly payments over " + termofloan + " months, (or " + termofloanyears + " years) your monthly payment will be $" + monthlypayment.toFixed(2);
		frm.displayrpt.value = data;
	}
	else
	{
		document.calculatorform.monthlypayment.value = "";
		document.calculatorform.displayrpt.value = "";
	}
}

function validatecalculator()
{
	var frm=document.calculatorform;
	var loanbalance = frm.loanbalance.value;
	var annualinterestrate = frm.annualinterestrate.value;
	var termofloan = frm.termofloan.value;
	
	var numTypeLoanbalance = parseFloat(loanbalance);
	var numTypeRate = parseFloat(annualinterestrate);
	var numTypeTermOfLoan = parseFloat(termofloan);

	if (loanbalance.indexOf(',') != -1) 
	{
		alert( "Please enter a Loan Balance number without the ',' sign." );
		frm.loanbalance.focus();
		return false;
	 }
	 if (isNaN(numTypeLoanbalance))
	{
		alert("Please enter a valid number for Loan Balance without '$' or ',' sign.");
		frm.loanbalance.focus();
		return false;
	}
	if (isNaN(numTypeRate))
	{
		alert("Please enter a valid number for Annual Interest Rate.");
		frm.annualinterestrate.focus();
		return false;
	}
	if(eval(annualinterestrate) < 1 || eval(annualinterestrate) > 100)
	{
		alert("Please enter an annual interest rate between 1 and 100");
		frm.annualinterestrate.focus();
		return false;
	}
	if (isNaN(numTypeTermOfLoan))
	{
		alert("Please enter a valid number for Term of Loan.");
		frm.termofloan.focus();
		return false;
	}
	if(eval(termofloan)>60)
	{
		alert("The maximum Term of Loan is 60 months.");
		frm.termofloan.focus();
		return false;
	}
	if(eval(termofloan)<0)
	{
		alert("You have entered an invalid number for Term of Loan");
		frm.termofloan.focus();
		return false;
	}
	return true;
}

//scripts used on credit counseling calculator (credit-counseling-calculator.htm)
function creditcalculator()
{	
	if(validatecreditcalculator())
	{
		var frm=document.creditcalculatorform;
		
		var unsecureddebts = eval(frm.unsecureddebts.value);
		var numofmonthpayment = eval(frm.numofmonthpayment.value);
		
		var creditmonthlypayment = unsecureddebts/numofmonthpayment;
		
		frm.creditmonthlypayment.value = creditmonthlypayment.toFixed(2);
	}
	else
	{
		document.creditcalculatorform.creditmonthlypayment.value = "";
	}
}

function validatecreditcalculator()
{
	var frm=document.creditcalculatorform;
	var unsecureddebts = frm.unsecureddebts.value;
	var numofmonthpayment = frm.numofmonthpayment.value;
	
	var numTypeDebts = parseFloat(unsecureddebts);
	var numTypeMonth = parseFloat(numofmonthpayment);

	if (unsecureddebts.indexOf(',') != -1) 
	{
		alert( "Please enter a Unsecured Debts number without the ',' sign." );
		frm.unsecureddebts.focus();
		return false;
	 }
	 if (isNaN(numTypeDebts))
	{
		alert("Please enter a valid number for your total unsecured debts without '$' or ',' sign.");
		frm.unsecureddebts.focus();
		return false;
	}
	if (isNaN(numTypeMonth))
	{
		alert("Please enter a valid number for Number of Month you will be making payments.");
		frm.numofmonthpayment.focus();
		return false;
	}
	if(eval(numofmonthpayment)>60)
	{
		alert("The maximum Number of Month you will be making payments is 60 months.");
		frm.numofmonthpayment.focus();
		return false;
	}
	if(eval(numofmonthpayment)<0)
	{
		alert("You have entered an invalid number for Number of Month you will be making payments");
		frm.numofmonthpayment.focus();
		return false;
	}
	return true;
}

//used on financial-health-check.htm
function validateHealthCheck(form)
{
	if(validateRadioGroup(form.liveinus))
	{
		alert("Please tell us if you live in the US or not.");
		form.liveinus[0].focus();
		return false;
	}
	if(validateRadioGroup(form.ownhouse))
	{
		alert("Please tell us if you own a house or not.");
		form.ownhouse[0].focus();
		return false;
	}
	if(form.numcardslimit.value == null || form.numcardslimit.value == "")
	{
		alert("Please tell us how many of your credit cards have reached their credit limit.");
		form.numcardslimit.focus();
		return false;
	}
	if(isNaN(form.numcardslimit.value) || form.numcardslimit.value < 0)
	{
		alert("Please enter a valid number.");
		form.numcardslimit.focus();
		return false;
	}
	if(validateRadioGroup(form.payminimum))
	{
		alert("Please tell us if you pay only the minimum or less on any of your credit cards each month");
		form.payminimum[0].focus();
		return false;
	}
	if(validateRadioGroup(form.jugglebills))
	{
		alert("Please tell us if you juggle other bills to make the minimum monthly payments on credit cards");
		form.jugglebills[0].focus();
		return false;
	}
	if(validateRadioGroup(form.paydayloan))
	{
		alert("Please tell us if you have gotten a payday loan in the last year.");
		form.paydayloan[0].focus();
		return false;
	}
	if(validateRadioGroup(form.paylatefee))
	{
		alert("Please tell us if you have been paying late fees on overdue credit or store card bills");
		form.paylatefee[0].focus();
		return false;
	}
	if(validateRadioGroup(form.cashadvances))
	{
		alert("Please tell us if you use cash advances from one credit card to make payments on another");
		form.cashadvances[0].focus();
		return false;
	}
	if(validateRadioGroup(form.collectagency))
	{
		alert("Please tell us if a debt collection agency has contacted you about an unpaid bill");
		form.collectagency[0].focus();
		return false;
	}
	if(validateRadioGroup(form.receiveletter))
	{
		alert("Please tell us if you have recently received a court notice or a letter about bailiffs or a sheriff coming to your home?");
		form.receiveletter[0].focus();
		return false;
	}
	return true;
}

//check if a radio button is checked, if not checked return true
function validateRadioGroup(obj)
{
	var checked = 0;
	var returnval = true;
	
	for (var i=0; i<obj.length; i++)  
	{ 
		if (obj[i].checked) 
		{
			checked +=1 ;
			returnval = false;
		}
	}
	
	return returnval;
}

//image rollover script
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		steps_rollover_Layer_2_over = newImage("/steps_rollover_Layer-2-over.jpg");
		steps_rollover_Layer_5_over = newImage("/steps_rollover_Layer-5-over.jpg");
		steps_rollover_Layer_6_over = newImage("/steps_rollover_Layer-6-over.jpg");
		preloadFlag = true;
	}
}

function preloadImages1() {
	if (document.images) {
		steps1_rollover_Layer_5_over = newImage("/steps1_rollover_Layer-5-ove.jpg");
		steps1_rollover_Layer_6_over = newImage("/steps1_rollover_Layer-6-ove.jpg");
		preloadFlag = true;
	}
}

function preloadImages2() {
	if (document.images) {
		steps2_rollover_Layer_2_over = newImage("images/steps2_rollover_Layer-2-ove.jpg");
		steps2_rollover_Layer_6_over = newImage("images/steps2_rollover_Layer-6-ove.jpg");
		preloadFlag = true;
	}
}

function preloadImages3() {
	if (document.images) {
		steps3_rollover_Layer_2_over = newImage("images/steps3_rollover_Layer-2-ove.jpg");
		steps3_rollover_Layer_5_over = newImage("images/steps3_rollover_Layer-5-ove.jpg");
		preloadFlag = true;
	}
}