function buildRoundedCornerTable(table_contents) {
	var html = '<div class="box_903">';
	html += '<div class="top"></div>';
	html += '<div class="body">' + table_contents + '</div>';
	html += '<div class="bottom"></div>';
	html += '</div>';

	return html;
}

// buildTabableTable
// Creates the html code for tabable table
function buildTabableTable(tab_type, max_count) {
	var html = '<div id="'+tab_type+'_tabs" class="tabs"></div>';
	html += '<div class="rounded box_903">';
	html += '<div class="top"></div>';
	html += buildPreQuestionsContent();
	for (var i=1; i<=max_count; i++) {
		html += '<div id="'+tab_type+'_'+i+'" class="body" style="display:none;"></div>';
	}
	html += '</div>';

	return html;
}
function buildQCPTabableTable(tab_type, max_count) {
	var html = '<div id="'+tab_type+'_tabs" class="tabs"></div>';
	html += '<div class="rounded box_863" style="clear:both">';
	html += '<div class="top"></div>';
	html += buildPreQuestionsContent();
	for (var i=1; i<=max_count; i++) {
		html += '<div id="'+tab_type+'_'+i+'" class="body" style="display:none;"></div>';
	}
	html += '<div class="bottom"></div>';
	html += '</div>';

	return html;
}
function addTab(tab_type, max_tabs) {
	var number_tabs = document.forms[form_name].elements["number_"+tab_type+"s"].value;		// before adding tab
	if (number_tabs < max_tabs) {
		document.forms[form_name].elements["number_"+tab_type+"s"].value++;
		buildTabs(tab_type);
		number_tabs = document.forms[form_name].elements["number_"+tab_type+"s"].value;		// after adding tab
		var tabObj = document.getElementById(tab_type+"_"+number_tabs);
		var build_method = "build"+tab_type.substr(0,1).toUpperCase()+tab_type.substr(1)+"Questions";
		tabObj.innerHTML = eval(build_method+"(number_tabs)");
	}
	
	changeTab(tab_type, number_tabs);
	initShowHide();		// this function must be implemented in the individual .js files

	if (number_tabs == max_tabs && (tab_type == "vehicle" || tab_type == "driver")) {
		disappear("add_"+tab_type+"_button");
	}
	if (number_tabs > 1 && (tab_type == "vehicle" || tab_type == "driver")) {
		appear("remove_"+tab_type+"_button");
	}
	if (tab_type == "vehicle" || tab_type == "driver") {
		calculate_tab_type(tab_type);
	}
}
function removeTab(tab_type, max_tabs) {
	var current_tab_num = document.forms[form_name].elements["current_"+tab_type+"_num"].value;	 // tab we are going to remove
	var number_tabs = document.forms[form_name].elements["number_"+tab_type+"s"].value;	 		// before removing tab
	var new_tab_label_array = new Array();

	// we can not remove a tab if there is only 1
	if (number_tabs > 1) {

		clearTabErrorList(tab_type, current_tab_num);
		clearTabErrors(current_tab_num);

		// copy contents of right tab to tab to be removed (current_tab_num)
		// update tab label with right tab label
		for (var i=current_tab_num; i<number_tabs; i++) {
			copyTab(tab_type, (parseInt(i)+1), i);
			var tab_label = document.getElementById(tab_type+"_"+(parseInt(i)+1)+"_label").innerHTML;
			if (tab_type == "vehicle") {
				if (document.forms[form_name].elements["vehicle_model_"+(parseInt(i)+1)].value == "") {
					tab_label = default_tab_label + " " + i;
				}
			} else if (tab_type == "driver") {
				if (document.forms[form_name].elements["first_name_"+(parseInt(i)+1)].value == "") {
					tab_label = default_tab_label + " " + i;
				}
			}
			updateTabLabel(tab_label, tab_type, i);
		}
		if (current_tab_num < number_tabs) {
			if (tab_type == "vehicle"){
				clearModels(current_tab_num);
				clearModels(number_tabs);
			}
		}

		for (i=0; i<var_fdat_list.length; i++) {
			if(fdat_value_hash[var_fdat_list[i]+"_"+number_tabs] !=  undefined){
				fdat_value_hash[var_fdat_list[i]+"_"+number_tabs] = "";
			}
		}

		// change current selected tab
		// if we delete the last tab, make last-1 the selected tab otherwise the selected tab stays the same
		if (current_tab_num == number_tabs) {
			changeTab(tab_type, parseInt(current_tab_num)-1);
		}

		// we need to decrease the number of tabs in order to use buildTabs properly
		document.forms[form_name].elements["number_"+tab_type+"s"].value--;
		buildTabs(tab_type);

		// show/hide add/remove buttons according to how many tabs there are
		disappear(tab_type+"_"+number_tabs);
		number_tabs = document.forms[form_name].elements["number_"+tab_type+"s"].value;
		if (number_tabs == 1 && (tab_type == "vehicle" || tab_type == "driver")) {
			disappear("remove_"+tab_type+"_button");
		}
		if (number_tabs < max_tabs && (tab_type == "vehicle" || tab_type == "driver")) {
			appear("add_"+tab_type+"_button");
		}
	}
	if (tab_type == "vehicle" || tab_type == "driver") {
		calculate_tab_type(tab_type);
	}
}

function calculate_tab_type(tab_type) {
        var numbers = document.forms[form_name]["number_"+tab_type+"s"].value;
        var add_tab_message = FindDOM("add_tab_message");
        var max_tab_type_text = FindDOM("max_"+tab_type+"_text");
	var max_tab_types = eval("max_"+tab_type+"s");
        if(numbers >= max_tab_types) {
                max_tab_type_text.style.display = "inline";
		add_tab_message.style.display = "none";
        }else {
                max_tab_type_text.style.display = "none";
		add_tab_message.style.display = "inline";
        }
}

function copyTab(tab_type, from_tab_num, to_tab_num) {

	for (var i=0; i <= var_fdat_list.length - 1; i++) {
		var_from_element = var_fdat_list[i] + '_' + from_tab_num;
		var_to_element = var_fdat_list[i] + '_' + to_tab_num;
		if (var_from_element.indexOf("vehicle_model") >= 0) {
			copyModelList(from_tab_num, to_tab_num);
		} else if (var_from_element.indexOf("date") >= 0) {
			var year = ""; var month = ""; var day = "";
			if (typeof document.forms[form_name].elements["year_"+var_from_element] != "undefined") {
				year = document.forms[form_name].elements["year_"+var_from_element].value;
			}
			if (typeof document.forms[form_name].elements["month_"+var_from_element] != "undefined") {
				month = document.forms[form_name].elements["month_"+var_from_element].value;
			}
			if (typeof document.forms[form_name].elements["day_"+var_from_element] != "undefined") {
				day = document.forms[form_name].elements["day_"+var_from_element].value;
			}
			fdat_value_hash[var_to_element] = year + "/" + month + "/" + day;
			fdat_value_hash[var_from_element] = "";
		} else {
			if (typeof document.forms[form_name].elements[var_from_element] != "undefined") {
				fdat_value_hash[var_to_element] = "";
				if ((document.forms[form_name].elements[var_from_element].length > 1) && 
					(document.forms[form_name].elements[var_from_element].type != "select-one")) {
					for (var j=0; j <= document.forms[form_name].elements[var_from_element].length - 1; j++) {
						if (document.forms[form_name].elements[var_from_element][j].checked) {
							fdat_value_hash[var_to_element] = document.forms[form_name].elements[var_from_element][j].value;
							fdat_value_hash[var_from_element] = "";
						}
					}
				} else {
					fdat_value_hash[var_to_element] = document.forms[form_name].elements[var_from_element].value;
					fdat_value_hash[var_from_element] = "";
				}
			}
		}
	}

	copyTabErrors(from_tab_num, to_tab_num);
	copyTabErrorList(tab_type, from_tab_num, to_tab_num);

	rebuildTab(tab_type, to_tab_num);
	initShowHide();

}
function buildTabs(tab_type) {
	var content = "";
	var current_tab_num = document.forms[form_name].elements["current_"+tab_type+"_num"].value;
	var number_tabs = document.forms[form_name].elements["number_"+tab_type+"s"].value;
	for (var i=1; i<=number_tabs; i++) {
		var additional_js = tab_type == "register_save" ? "registerSave('" + form_name + "', 'action_type', '"+i+"', '"+current_tab_num+"');" : "";
		var tabLabelObj = document.getElementById(tab_type+"_"+i+"_label");
		var default_tab = (tab_type == "allocation") ? default_tab_label : default_tab_label+' '+i;
		var tab_label_text = (tabLabelObj == null || tabLabelObj.innerHTML == "") ? default_tab : tabLabelObj.innerHTML;
		var tab_error_class = (var_error_list["error_"+i]) ? "_error" : "";
		var label_class = (tab_type == "allocation") ? "label" : "label_link";
		var left_class = (i == 1) ? "tab_left" : "tab_short_left";
		content += '<div id="'+tab_type+'_tab_'+i+'" onclick="changeTab(\''+tab_type+'\', '+i+');'+additional_js+'" ';
		content += 'class="tab';
		if (i == number_tabs) {
			content += ' last_tab';
		}
		if (i == current_tab_num) {
			content += ' tab_on">';
		} else {
			content += '">';
		}
		content += '<div class="'+left_class+'"></div>';
		content += '<div class="tab_body">';
		content += '<div id="'+tab_type+'_'+i+'_label" class="tab_content '+tab_type+'_label'+tab_error_class+'">';
		content += '<a class="'+label_class+'">'+tab_label_text+'</a>';
		content += '</div>';
		content += '</div>';
		content += '<div class="tab_right"></div>';
		content += '</div>';
	}
	var tabsObj = document.getElementById(tab_type+"_tabs");
	var aeropoints = aeroplan ?
		'<div class="aeroplan"><div class="content"><img onclick="window.open(\'http://www.greypower.com/aeroplan\', \'new_window\')" border="0" src="/pics/aeroplan.gif" style="cursor:pointer;" /></div></div>' +
		'<div class="aeroplan"><div class="content">You are eligible for <span style="font-weight:bold;" id="aeroplan_point"></span>&nbsp;<span style="font-weight:bold;">Aeroplan Miles.</span></div></div>'
		: '';
	tabsObj.innerHTML = content + aeropoints;

	/*
	var number_tab_type_label = document.getElementById("number_"+tab_type+"s_label");
	if (number_tab_type_label != null) {
		var lang = document.forms[form_name].website_id.value;
		var disp = '';
		var tab_type_lang = '';
		if (lang == "french") {
			disp = "Cette soumission est faite pour ";
			if (tab_type == "driver")
				tab_type_lang = "conducteur"; 
			else 
				tab_type_lang = "v&eacute;hicule";
		} else {
			disp = "Your quote includes ";
			tab_type_lang = tab_type;
		}
		if (number_tabs == 1) {
			number_tab_type_label.innerHTML = disp+number_tabs+' '+tab_type_lang+'.';
		} else {
			number_tab_type_label.innerHTML = disp+number_tabs+' '+tab_type_lang+'s.';
		}
	}
	*/
}
function changeTab(tab_type, num) {
	var number_tabs = document.forms[form_name].elements["number_"+tab_type+"s"].value;
	for (var i=1; i<=number_tabs; i++) {
		if (i == num) {
			selectTab(tab_type, i);
			appear(tab_type+"_"+i);	
		} else {
			unselectTab(tab_type, i);
			disappear(tab_type+"_"+i);	
		}
	}
	document.forms[form_name].elements["current_"+tab_type+"_num"].value = num;
}
function selectTab(tab_type, num) {
	var tabObj = document.getElementById(tab_type+"_tab_"+num);
	addClass(tabObj, "tab_on");
}
function unselectTab(tab_type, num) {
	var tabObj = document.getElementById(tab_type+"_tab_"+num);
	removeClass(tabObj, "tab_on");
}
function updateTabLabel(newLabel, tab_type, num) {
	var label_class = (tab_type == "allocation") ? "label" : "label_link";
	var tabLabelObj = document.getElementById(tab_type+"_"+num+"_label");
	newLabel = (newLabel == "") ? tab_type+" "+num : newLabel;
	//newLabel = truncatedLabel(newLabel, 25);
	tabLabelObj.innerHTML = "<a class='"+label_class+"'>"+newLabel+"</a>"; 
	updateErrorLabel(newLabel, tab_type, num);
}
function updateErrorLabel(newLabel, tab_type , num) {
	var errorLabelObj = document.getElementById(tab_type+"_error_label_"+num);
	if (errorLabelObj != null) {
		errorLabelObj.innerHTML = newLabel+":";
	}
}
function buildRemoveTabButton(tab_type, max_tabs) {
	var number_tabs = document.forms[form_name].elements["number_"+tab_type+"s"].value;
	var hide_remove_button = (number_tabs > 1) ? "" : "style=\"display:none;\"";
	var content = "";
	var add_tab_message = FindDOM("add_tab_message");
	add_tab_message.innerHTML = "Want to add another "+tab_type+" on this policy?";
	add_tab_message.style.display = number_tabs < max_tabs ? "inline" : "none";
	content += '<div class="remove_buttons">';
	content += '<div id="remove_'+tab_type+'_button" onclick="removeTab(\''+tab_type+'\', '+max_tabs+');" '+hide_remove_button+'></div>';
	if(tab_type == "vehicle" || tab_type == "driver") {
		content += '<div id="max_'+tab_type+'_text" style="display:none;">If you would like a quote on more than '+max_tabs+' '+tab_type+'s, please contact us at '+gp_toll_free_number+'</div>';
	}
	content += '<div class="spacer"></div>';
	content += '</div>';
	return content;	
}
function buildAddTabButton(tab_type, max_tabs) {
	var number_tabs = document.forms[form_name].elements["number_"+tab_type+"s"].value;
	var hide_add_button = (number_tabs < max_tabs) ? "" : "style=\"display:none;\"";
	return '<input id="add_'+tab_type+'_button" type="image" src="/pics/button_add_'+tab_type+'.gif" alt="Click to add a '+
		tab_type+'" onclick="addTab(\''+tab_type+'\', '+max_tabs+');return false;" '+hide_add_button+'/>';
}
function truncatedLabel(label, max_length) {
	if(label.length > max_length){
		label = label.substr(0,max_length-3)+"...";
	}
	return label;
}
function rebuildTab(tab_type, num) {
	var tabObj = document.getElementById(tab_type+"_"+num);
	var build_method = "build"+tab_type.substr(0,1).toUpperCase()+tab_type.substr(1)+"Questions";
	tabObj.innerHTML = eval(build_method+"(num)");
}
function copyTabErrorList(tab_type, from_tab_num, to_tab_num) {
	var toErrorList = document.getElementById(tab_type+"_errors_"+to_tab_num);
	errorArray[to_tab_num] = errorArray[from_tab_num];
	if (var_error_list["error_"+to_tab_num] == 1) {
		toErrorList.innerHTML = buildErrorList(tab_type, to_tab_num);	
	}
	clearTabErrorList(tab_type, from_tab_num);
	appear(tab_type+"_errors_"+to_tab_num);
}
function clearTabErrorList(tab_type, num) {
	var errorList = document.getElementById(tab_type+"_errors_"+num);
	errorArray[num] = {};
	errorList.innerHTML = "";
	disappear(tab_type+"_errors_"+num);
}
function clearTabErrors(num) {
	var pattern = "_"+num+"$";
	for (var error in var_error_list) {
		if (error.match(pattern)) {
			var_error_list[error] = "";
		}
	}
}
function copyTabErrors(from_tab_num, to_tab_num) {
	// Copy error highlighting up
	var patternFrom = "_"+from_tab_num+"$";
	var patternTo = "_"+to_tab_num+"$";
	// Clear all the to_driver errors
	clearTabErrors(to_tab_num);
	// Copy up all the from_driver errors
	for (var error in var_error_list) {
		if (error.match(patternFrom)) {
			var new_error = error.substr(0,error.length-2)+"_"+to_tab_num
			var_error_list[new_error] = var_error_list[error];
			var_error_list[error] = "";
		}
	}
}

// this function works specifically for my account->register save page
function registerSave(frm_name, elem_name, tab_num, ori_tab_num) {
	addFormElement(frm_name, elem_name, tab_num);
	var current_num = document.forms[frm_name]["current_register_save_num"].value;
	var error_num   = tab_num == 1 ? 2 : 1;
	var fields	  = tab_num == 1 ? new Array("email_2", "password_2") : new Array("email_1", "password_1", "password_confirm_1");
	disappear("register_save_errors_" + error_num);
	for(var i = 0; i < fields.length; i++) {
		if(fields[i] != "email_1") {
			document.forms[frm_name][fields[i]].value = "";
		}
	}
}
