//javascript code used on moneyproblems.ca
function reloadForProvinces(form)
{
	var advisor_type=form.advisor_types.options[form.advisor_types.options.selectedIndex].value;
	
	self.location='Ask-a-Personal-Finance-Question.php?advisor_type=' + advisor_type;
}

function reloadForCities(form)
{
	var advisor_type=form.advisor_types.options[form.advisor_types.options.selectedIndex].value; 
	var province=form.provinces.options[form.provinces.options.selectedIndex].value; 
	
	self.location='Ask-a-Personal-Finance-Question.php?advisor_type=' + advisor_type + '&province=' + province ;
}

function reloadForOffices(form)
{
	var advisor_type=form.advisor_types.options[form.advisor_types.options.selectedIndex].value; 
	var province=form.provinces.options[form.provinces.options.selectedIndex].value;
	var city=form.cities.options[form.cities.options.selectedIndex].text;
		
	self.location='Ask-a-Personal-Finance-Question.php?advisor_type=' + advisor_type + '&province=' + province + '&city=' + city;			
}
	
function setCityAndSponsor(form, multiple_offices)
{
	var city="";
	
	//alert("multiple_offices: " + multiple_offices);
	
	if (multiple_offices)
	{
		city=form.offices.options[form.offices.options.selectedIndex].text
		showControl("nearest_office");
		//alert ("City var in setCityAndSponsor " + city);
	}
	else
	{
		city=form.cities.options[form.cities.options.selectedIndex].text;
		hideControl("nearest_office");
		//alert ("City var in setCityAndSponsor " + city);
	}
	
	var advisorType=form.advisor_types.options[form.advisor_types.options.selectedIndex].value;
	
	if(advisorType != "" && advisorType != "none")
	{
		form.advisor_type.value=advisorType;
		
		if(city != "" && city != "none" && city != "Select")
		{
			form.city.value = city;					
		}
		else
		{
			form.city.value = "";
		}
	}	
	else
	{
		form.advisor_type.value="";			
		form.city.value = "";			
	}
}

function setNearestOffice(form)
{
	var nearest_office=form.nearest_office.options[form.nearest_office.options.selectedIndex].value;
	form.city.value = nearest_office;
}

function controlVisibility(form)
{
	var advisor_type=form.advisor_types.options[form.advisor_types.options.selectedIndex].value;
	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") || (advisor_type == "Credit Counselor"))
	{
		hideControl("province_row");
		if (advisor_type == "Credit Counselor") 
		{
			showControl("credit_counsellor_row");		
		}
		else
		{				
			hideControl("credit_counsellor_row");		
		}	
		hideControl("city_row");
		hideControl("id_question1");
		hideControl("id_question2");
		hideControl("id_question3");
		hideControl("id_question4");
		hideControl("id_question5");
		hideControl("id_question6");
		hideControl("id_question7");
		return;
	}
  else
	{			
		hideControl("credit_counsellor_row");		
		showControl("province_row");		
	}		
	
	/*
	if(advisor_type == "Credit Counselor")
	{
		showControl("credit_counsellor_row");		
		return;
	}
	else
	{
		showControl("province_row");		
		hideControl("credit_counsellor_row");		
	}
	*/
	/*  i think this is cover with credit counselor case
	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");
	}	
	
	if(trim(city).length == 0 || city == "none")
	{
		//hideControl("nearest_office");		
	}	
}



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"; 
	}
}


// Opens the print window.
function printWindow() {
	bV = parseInt(navigator.appVersion);
	if (bV >= 4) window.print();
}

// Validates the link-to-us form.
function validate( form ){

  if ( form.sitename.value.length < 1 ) {
    alert( "Please enter your site name" );
    return false;
  }
  if ( form.siteurl.value.length < 1 ) {
    alert( "Please enter your site URL" );
    return false;
  }
  if ( form.sitedesc.value.length < 1 ) {
    alert( "Please enter a your site description" );
    return false;
  }
  if ( form.sitedesc.value.length > 400 ) {
    alert( "Your site description is too long.  There is a 400 character max." );
    return false;
  }
  if ( form.reciplink.value.length < 1 ) {
    alert( "Please enter your reciprocal link location." );
    return false;
  }
  
  return true;
}

//validates the ask a question form on city(sponsor) pages
function validateQuestion( form )
{
  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;
}

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 province = trim(form.provinces.options[form.provinces.options.selectedIndex].value);
	var city = trim(form.cities.options[form.cities.options.selectedIndex].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 province." );
		return false;
	}
	
	if(city.length < 1 || city == "none")
	{
		alert( "Please select a city." );
		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;
}


//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 validateCaliforniaCity( 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;
}

function validateblogquestion (form)
{
	if ( trim(form.subject.value).length < 1 ) {
    alert( "Please enter a title for your question." );
	form.subject.focus();
    return false;
  }
  if ( trim(form.message.value).length < 1) {
    alert( "Please enter your question." );
	form.message.focus();
    return false;
  }
}

//Opens an html page in a printer friendly version and pops up the print window at the same time
function openForPrinting()
{ 
	var display_setting="toolbar=yes, location=no, directories=no, menubar=yes,"; 
    	display_setting+="scrollbars=yes, width=800, height=600, left=50, top=5"; 
  
	var print_content=document.getElementById("workareaframe").innerHTML; 
  	var new_page_title=document.title;
  
  	var popup_window=window.open("","",display_setting); 
   	popup_window.document.open(); 
   	popup_window.document.write('<html><head>');
   	popup_window.document.title=new_page_title;
   	popup_window.document.write('<link href="http://www.moneyproblems.ca/moneyproblems.css" type="text/css" rel="stylesheet" />');
	popup_window.document.write('<link href="http://www.moneyproblems.ca/fat/styles-financial-ad-tool-prt.css" type="text/css" rel="stylesheet" />');
	popup_window.document.write('</head><body onLoad="self.print()"><style>div{text-align: left;} #workareaframe{width: 680px;} #workareacnt{width: 670px;}</style>');          
   	popup_window.document.write(print_content);
   	popup_window.document.write('</body></html>'); 
   	popup_window.document.close(); 		//closes the document for writing
   	popup_window.focus(); 
}

//opens google bookmark window; also, passes current page's url and title to the window
function googleBookmark() 
{
 	var bookmarkurl=document.URL; 
 	var bookmarktitle=document.title; 
 	var popupwindowurl = "http://www.google.com/bookmarks/mark?op=add&bkmk="+bookmarkurl+"&title="+bookmarktitle;
 
 	window.open(popupwindowurl,'','scrollbars=yes,resizable=yes, width=550, height=550, left=0, top=0');
}

function validateSearch(form)
{
	if ( trim(form.q.value).length < 1 ) 
	  {
		alert( "Please enter your search keyword/phrase." );
		return false;
	  }
	
	return true;
}

//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; 
} 

//display the page outside of the parent iframe
function displayOnTop(self)
{
	if (self != top) top.location = self.location;
}

//validate data entry on the financial health check form
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;
}


// ADDTHIS BUTTON
function addit_button_new()
{
	
addthis_pub             = 'moneyproblems'; 
addthis_logo            = 'http://www.moneyproblems.ca/images/addthis_button.gif';
addthis_logo_background = 'EFEFFF';
addthis_logo_color      = '666699';
addthis_brand           = 'Moneyproblems.ca';
addthis_options         = 'favorites, email,myspace, facebook, google, live,delicious, digg,technorati,myweb,ask,twitter,more';
document.write("<div id='prn'>");
document.write("<a href='http://www.addthis.com/bookmark.php' onmouseover=\"return addthis_open(this, '', '[URL]', '[TITLE]')\" onmouseout='addthis_close()' onclick='return addthis_sendto()'>Bookmark &amp; share this page</a>");
document.write("</div>");
document.write("<div class='imglink'><img src='http://www.moneyproblems.ca/images/add_plus.gif'   border='0' alt='' /></div>");
}

function addit_button()
{
	
addthis_pub             = 'moneyproblems'; 
addthis_logo            = 'http://www.moneyproblems.ca/images/addthis_button.gif';
addthis_logo_background = 'EFEFFF';
addthis_logo_color      = '666699';
addthis_brand           = 'Moneyproblems.ca';
addthis_options         = 'favorites, email,myspace, facebook, google, live,delicious, digg,technorati,myweb,ask,more';
document.write("<div id='prn'>");
document.write("<img src='images/addthis16.gif'   border='0' alt='' />&nbsp;<a href='http://www.addthis.com/bookmark.php' onmouseover=\"return addthis_open(this, '', '[URL]', '[TITLE]')\" onmouseout='addthis_close()' onclick='return addthis_sendto()'>Bookmark &amp; share this page</a>");
document.write("</div>");
}