// validate function for Canadian bankrutpcy city sites
function validate( form ){
  if ( form.contact.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 ( 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;
}
//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 = "Victoria";
	var script = "cityscript.js";
	var logo = "logo.gif";
	var domain = "victoria.net";
	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);
		}
	}
}

/* script used on bankruptcy evaluation form */
var panes = new Array();

function setupPanes(containerId, defaultTabId) {
		
  // go through the DOM, find each tab-container
  // set up the panes array with named panes
  // find the max height, set tab-panes to that height
  panes[containerId] = new Array();
  var maxHeight = 0; var maxWidth = 0;
  var container = document.getElementById(containerId);
  var paneContainer = container.getElementsByTagName("div")[0];
  var paneList = paneContainer.childNodes;
  for (var i=0; i < paneList.length; i++ ) {
    var pane = paneList[i];
    if (pane.nodeType != 1) continue;
    /*if (pane.offsetHeight > maxHeight) maxHeight = pane.offsetHeight;
    if (pane.offsetWidth  > maxWidth ) maxWidth  = pane.offsetWidth;*/
    panes[containerId][pane.id] = pane;
    pane.style.display = "none";
  }
    /*paneContainer.style.height = maxHeight + "px";
    paneContainer.style.width  = maxWidth + "px";*/
    document.getElementById(defaultTabId).onclick();
}

function showPane(paneId, activeTab) {
  // make tab active class
  // hide other panes (siblings)
  // make pane visible  
  //validation for each pane
	var errormessageBills = validateBillsEntries();
	var errormessageAsset = validateAssetsEntries();
	var errormessageIncome = validateIncomeEntries();
  
  if(paneId=="pane3" && (errormessageBills != ""))
  {
		if (errormessageBills != "")
		{
			alert(errormessageBills);
			return false;
		}
  }
  else if(paneId=="pane4" && (errormessageAsset != ""))
  {
	  if (errormessageAsset != "")
	  {
		  alert(errormessageAsset);
		  return false;
	  }

  }
  else if(paneId=="pane5" && (errormessageIncome != ""))
  {
	  if (errormessageIncome != "")
	  {
		  alert(errormessageIncome);
		  return false;
	  }
  }
  else
  {
	   //assign studentdebtonlyandyoung value
	   if(paneId=="pane3" && (errormessageBills == ""))
	  {
		document.frm.studentdebtonlyandyoung.value = "";
		var studentdebtonlyandyoung = checkStudentLoanDebt(); 
		//check if bill section entries are as prefered, if they aren't interupt the evaluation
		if(studentdebtonlyandyoung)
		{
			document.frm.studentdebtonlyandyoung.value = "yes";
		}			
	  }
	  if(paneId=="pane5" && (errormessageIncome == ""))
	  {
		  document.frm.nowage.value = "";
		  document.frm.lowincome.value = "";
		  var nowage = checkIncomeSources();	  
		  var lowincome = checkNetIncome();
	  
		  //check if income section entries are as prefered, if they aren't interupt the process of filling in the evaluation form
		  //by taking user to a different screen
		  if(nowage == true)
		  {
			  document.frm.nowage.value = "yes";	
		  }
		
		  if(lowincome == true)
		  {
			  document.frm.lowincome.value = "yes";	
		  }
	  }
	  
	  switch_tab(activeTab.name);
		for (var con in panes) {
		if (panes[con][paneId] != null) { // tab and pane are members of this container
		  var pane = document.getElementById(paneId);
		  pane.style.display = "block";
		  var container = document.getElementById(con);
		  for (var i in panes[con]) {
			var pane = panes[con][i];
			if (pane == undefined) continue;
			if (pane.id == paneId) continue;
			pane.style.display = "none"
		  }
		}
	  }
  }
  return false;   
}

function validateBillsEntries()
{	
	var numberofcheckeditems = 0;
	var studentloan = false;
	
	var errormessage = "";
	
	
	//checking if user either checked at least one checkbox or at least entered something into the "other debt" field.
	var frm = document.forms[0];
    var bills = document.getElementsByName('bills[]');
	
	for (var i=0; i < bills.length; i++)
	{
		if(bills[i].checked)
		{
			numberofcheckeditems += 1;
			
			if(bills[i].value == "Student loans")
			{				
				studentloan = true;				
				break;
			}
		}				
	}
	
	if(numberofcheckeditems == 0 && trim(document.frm.billsother.value) == "")
	{
		errormessage = "Please tell us what debts you have.";
	}	
	//if student debt is checked, check if age of the debt is specified.
	else if(studentloan == true && (document.frm.studentdebtage.value == "" || document.frm.studentdebtage.value == "select"))
	{
		errormessage ="Please tell us how old your student loan is.";
	}
	else if(document.frm.totaldebt.value == "" || document.frm.totaldebt.value == "select")
	{
		 errormessage ="Please estimate the amount of your total debt.";		 
	}	  
	
	return errormessage;
}

function checkStudentLoanDebt()
{
	var numberofcheckeditems = 0;
	var studentloan = false;
	var otherdebts = "";
	var restricted = false;
	
	//checking if student loans is the only option that is checked
	
	var frm = document.forms[0];
    var bills = document.getElementsByName('bills[]')
	
	for (var i=0; i < bills.length; i++)
	{
		if(bills[i].checked)
		{
			numberofcheckeditems += 1;
			
			if(bills[i].value == "Student loans")
			{				
				studentloan = true;				
				break;
			}
		}					
	}
	
	if(studentloan == true && numberofcheckeditems == 1)
	{
		//checking if there are other debts that user typed into the 'other debts' field
		otherdebts = trim(document.frm.billsother.value);	
		
		if(otherdebts == "")
		{
			//since the student debt is the only debt, check how old it is,
			//- based on the answer the next screen the user will see will be different.
			if(document.frm.studentdebtage.value == "less than 5 years")
			{
				restricted = true;
			}
		}
	}
	
	return restricted;
}


function validateAssetsEntries()
{
	var errormessage = "";	
	var isSelected = false;
	
	for (var i=0; i < document.frm.ownestate.length; i++)
	{
		if(document.frm.ownestate[i].checked)
		{
			isSelected = true;
			break;
		}
	}
	
	if(isSelected == false)
	{
		errormessage = "Please indicate whether or not you own any real estate.";	
	}
	
	return errormessage;
}


function validateIncomeEntries()
{	
	var errormessage = "";
	var numberofcheckeditems = 0;	
	
	//checking if user either checked at least one checkbox or at least entered something into the "other income" field.
	var frm = document.forms[0];
    var incometypes = document.getElementsByName('incometypes[]');
	
	for (var i=0; i < incometypes.length; i++)
	{
		if(incometypes[i].checked)
		{
			numberofcheckeditems += 1;
			break;
		}				
	}
	
	if(numberofcheckeditems == 0 && trim(document.frm.incometypeother.value) == "")
	{
		errormessage = "Please tell us what kinds of income you have.";
	}
	else if(document.frm.monthlyincome.value == "" || document.frm.monthlyincome.value == "select")
	{
		 errormessage = "Please estimate the amount of your total monthly net income.";		 
	}	  
	
	return errormessage;
}

function checkIncomeSources()
{
	var numberofcheckeditems = 0;
	var nowageincome = 0;
	var otherincome = "";
	
	var restricted = false;
	
	var frm = document.forms[0];
    var incometypes = document.getElementsByName('incometypes[]')
	
	//checking if "no-wage" income sources are the only options that are checked
	for (var i=0; i < incometypes.length; i++)
	{
		if(incometypes[i].checked)
		{
			numberofcheckeditems += 1;
			
			if(incometypes[i].value == "Social Assistance or Welfare" 
						   || incometypes[i].value == "Retirement pension"
						   || incometypes[i].value == "Child or spousal support")
			{
				nowageincome += 1;
			}
		}				
	}
	
	if(nowageincome == numberofcheckeditems)
	{
		//checking if there are other types of income that user typed into the 'other income' field
		otherincome = trim(document.frm.incometypeother.value);	
		
		if(otherincome == "")
		{	
			restricted = true;
		}
	}
	
	return restricted;
}

function checkNetIncome()
{
			
	var restricted = false;
	var netincome = document.frm.monthlyincome.value;
	
	//checking if net income is in the prefered range
	if(netincome == "$0 - $1,000")
	{
	 	restricted = true;
	}
	
	return restricted;
}

//show / hide image layers
function switch_tab(objname)
{
	if(objname == "panel1submit")
	{
			showLayer('toptab1on');
			hideLayer('toptab1off');
			
			showLayer('toptab2off');
			hideLayer('toptab2on');
			
			showLayer('toptab3off');
			hideLayer('toptab3on');
			
			showLayer('toptab4off');
			hideLayer('toptab4on');
			
			showLayer('toptab5off');
			hideLayer('toptab5on');
	}
	if(objname == "panel2submit")
	{
			showLayer('toptab1off');
			hideLayer('toptab1on');
			
			showLayer('toptab2on');
			hideLayer('toptab2off');
			
			showLayer('toptab3off');
			hideLayer('toptab3on');
			
			showLayer('toptab4off');
			hideLayer('toptab4on');
			
			showLayer('toptab5off');
			hideLayer('toptab5on');
	}
	if(objname == "panel3submit")
	{
			showLayer('toptab1off');
			hideLayer('toptab1on');
			
			showLayer('toptab2off');
			hideLayer('toptab2on');
			
			showLayer('toptab3on');
			hideLayer('toptab3off');
			
			showLayer('toptab4off');
			hideLayer('toptab4on');
			
			showLayer('toptab5off');
			hideLayer('toptab5on');
	}
	if(objname == "panel4submit")
	{
			showLayer('toptab1off');
			hideLayer('toptab1on');
			
			showLayer('toptab2off');
			hideLayer('toptab2on');
			
			showLayer('toptab3off');
			hideLayer('toptab3on');
			
			showLayer('toptab4on');
			hideLayer('toptab4off');
			
			showLayer('toptab5off');
			hideLayer('toptab5on');
	}
	if(objname == "panel5submit")
	{
			showLayer('toptab1off');
			hideLayer('toptab1on');
			
			showLayer('toptab2off');
			hideLayer('toptab2on');
			
			showLayer('toptab3off');
			hideLayer('toptab3on');
			
			showLayer('toptab4off');
			hideLayer('toptab4on');
			
			showLayer('toptab5on');
			hideLayer('toptab5off');
	}
}

//show hide layers used for panels
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"; 
	}
}

//validate bankruptcy evaluation form
function validateevaluation(form)
{
	if ( form.firstname.value.length < 1 ) {
		alert( "Please enter your first name." );
		form.firstname.focus();
		return false;
	}
	if ( form.lastname.value.length < 1 ) {
		alert( "Please enter your last name." );
		form.lastname.focus();
		return false;
	}
	if ( form.email.value.length < 1 && form.phone.value.length < 1) {
		alert( "Please enter your email or phone number for us to contact you." );
		form.phone.focus();
		return false;
	}
	if ( form.phone.value.length > 1 && form.bestcalltime.value.length < 1) {
		alert( "Please enter the best time for us to contact you." );
		form.bestcalltime.focus();
		return false;
	}
	if ( form.citiesselect.value == "Select a City")
	{
		alert( "Please select a city" );
		form.citiesselect.focus();
		return false;
	}
	if ( form.agreeterm.checked == false ) {
		alert( "Please check the terms and condition." );
		form.agreeterm.focus();
		return false;
	}
	return true;
}

function populate_cities()
{
	document.writeln("<select name=\"citiesselect\" class=\"quoteselect\">");
	document.writeln("<option value=\"Select a City\">Select a City</option>");
	document.writeln("<option value=\"Campbell River\">Campbell River</option>");
	document.writeln("<option value=\"Comox\">Comox</option>");
	document.writeln("<option value=\"Courtenay\">Courtenay</option>");
	document.writeln("<option value=\"Cranbrook\">Cranbrook</option>");
	document.writeln("<option value=\"Duncan\">Duncan</option>");
	document.writeln("<option value=\"East Kootenays\">East Kootenays</option>");
	document.writeln("<option value=\"Nanaimo\">Nanaimo</option>");
	document.writeln("<option value=\"Parksville\">Parksville</option>");
	document.writeln("<option value=\"Port Alberni\">Port Alberni</option>");
	document.writeln("<option value=\"Qualicum\">Qualicum</option>");
	document.writeln("<option value=\"Victoria\">Victoria</option>");
	document.writeln("</select>");
}

//show hide other city field
function showControl(divId)
{
	if (document.getElementById)
	{
		// this is the way the standards work
		document.getElementById(divId).style.visibility = "visible";		
		if(navigator.appName == "Netscape")
		{
			document.getElementById(divId).style.display = "table-row"; 
		}
		if(navigator.appName == "Microsoft Internet Explorer")
		{
			document.getElementById(divId).style.display = "block"; 
		}
	}
	else if (document.all)
	{
		// this is the way old msie versions work
		document.all[divId].style.visibility = "visible";		
		document.getElementById(divId).style.display = "table-row"; 
	}
	else if (document.layers)
	{
		// this is the way nn4 works
		document.layers[divId].visibility = "visible";		 
		document.getElementById(divId).style.display = "table-row"; 
	}
}

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"; 
	}
}
function exitiframe(self)
{
	if (self != top) top.location = self.location;
}
//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; 
}