// JavaScript Document

var seoFCKPath = "|uploads|seofiles";

function pageInit() {
initjsDOMenu();
}

function deleteConfirm(theForm)
{
var agree=confirm("Are you sure you wish to delete?");
if (agree)
	{
	theForm.isActive.value = 0;
	theForm.submit();
	return true ;
	}
else
	{
	return false ;	
	}
}

function fieldFocus(field,defaultVal) {
if (document.getElementById(field).value == defaultVal) {
	document.getElementById(field).value = "";
	}
}
function fieldBlur(field,defaultVal) {	
if (document.getElementById(field).value == "") {
	document.getElementById(field).value = defaultVal;
	}
}

function gotoPage(pg) {
window.location = pg;
}

function openPage(pg) {
newWin = window.open(pg,"print");
if (typeof newWin != "object") {
	gotoPage(pg);
	}
}

function checkZip(zipVal) {
var zipRE = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
//zipVal = document.getElementById(obj).value;
isValid = zipRE.test(zipVal);

if (isValid == false) {
	alert("Zipcode is invalid, please try again.");
	return false;
	}
}

function deleteRecord(frm,nm,act) {
var agree = confirm("Are you sure you wish to permanently delete the following:\n\n\t" + nm + "?");
if (agree) {
	if (act != "") {
		window.location = act;
		}
	if (frm != "pageForm") {
		return true;
		}
	}
else
	{
	return false ;	
	}
}

function printResults() {
window.print();	
}

function previewPage(formName,circuit) {
document.forms[formName].target = "previewWindow";
document.getElementById(formName).action = "/process.cfm?task=" + circuit;
document.getElementById(formName).submit();
}
function resetFormAction(formName) {
if (document.getElementById(formName).action != window.location) {
	document.getElementById(formName).target = "_self";
	document.getElementById(formName).action = window.location;
	document.getElementById(formName).submit();
	}
}
function populateSiteName() {
fName = document.getElementById("FirstName").value;
lName = document.getElementById("LastName").value;
document.getElementById("SiteName").value = fName + lName;
}

var activePanel = "";

function viewPanel(p) {
if (document.getElementById(activePanel)) {
	document.getElementById(activePanel).style.display = "none";
	}
if (document.getElementById(p)) {
	document.getElementById(p).style.display = "block";
	activePanel = p;
	}
}

function addToFavorites(url,title) {
if (window.sidebar) { // Mozilla Firefox Bookmark
	window.sidebar.addPanel(title,url,"");
	}
else if (window.external) { // IE Favorite
	window.external.AddFavorite(url,title);
	}
else if (window.opera && window.print) { // Opera Hotlist
	return true;
	}
else {
	alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark"); 
	}
}

//seo
function addWord(word,obj) {
currVal = document.getElementById(obj).value;
document.getElementById(obj).value = currVal + word;
}

function stripString(val) {
val = val.toLowerCase();
reSplit = /\W/;
splitStr = val.split(reSplit);
str = splitStr.join("-");
while (str.indexOf("--") != -1) {
	str = str.replace("--","-");
	}
return str.toLowerCase();
}

function createURL(val) {
retUrl = stripString(val);
document.getElementById("pageURLText").innerHTML = retUrl;
}

function createPageName(objId,idx,spn) {
stateObj = document.getElementById("stateId");
stateVal = "";
prodVal = "";
pName = "";
url = "";

if (stateObj) {
	stateOpt = stateObj.selectedIndex;
	stateVal = stateObj.options[stateOpt].text;
	}
if (stateOpt != 0) {
	pName = stateVal;
	url = "/" + stripString(stateVal);
	}	

prodObj = document.getElementById("productId");
if (prodObj) {
	prodOpt = document.getElementById("productId").selectedIndex;
	prodVal = document.getElementById("productId").options[prodOpt].text;
	
	prodStr = prodVal.replace("insurance","");
	prodStr = prodStr.split("-").join(" ");

	if (prodOpt != 0) {
		pName = pName + " " + prodStr + " Insurance";
		url = url + "/" + stripString(prodVal);
		}
	}

if (document.getElementById("pageTypeId").value == "4") {
	pName = pName + " Agents";
	url = url + "/agents";
	}

if (document.getElementById("pageTypeId").value == "2") {
	createURL(stripString(document.getElementById("pageName").value));
	url = url + "/" + stripString(document.getElementById("pageName").value);
	//url = "/" + url
	}

if (idx == 0) {
	if (objId == "stateId") {
		val = "State";
		}
	if (objId == "productId") {
		val = "Insurance";
		}
	}

if (document.getElementById("pageNameText")) {
	document.getElementById("pageNameText").innerHTML = pName;
	document.getElementById("pageName").value = pName;
}

document.getElementById("pageURL").value = url.toLowerCase();
}

function updateMenu(val,idVal) {
url = "process.cfm?task=menus.update&sortOrder=" + val + "&menuId=" + idVal;
makeServerCall(url);
}
function deleteMenu(parentId,subId) {
url = "process.cfm?task=menus.delete&menuId=" + parentId + "&subMenuId=" + subId;
makeServerCall(url);
}
function makeServerCall(url) {
$.ajax({
	type: "GET",
	url: url,
	success: function(){ return true }
	});
}
var returnData = "";
function getServerData(url,objId,type) {
$.get(
	url, 
	function(data) {
		$(objId).html(data);
		if (type == "rep") {
			$("#pageLoader").hide();
			$("#representativeFormBlock").show();
			}
		if (type == "state") {
			$("#countryLoader").hide();
			if (data != "") {
				$("#stateItem").show();
				}
			}
		if (type == "industry") {
			$("#industryLoader").hide();
			if (data != "") {
				$("#applicationItem").show();
				}
			}
	});
}
function selectPage(idx,selObj,targetObj) {
objVal = "#"+targetObj;
objValSelected = "#" + selObj + " :selected";
txt = $(objValSelected).text();
$(objVal).val(txt);
}
//representative form
function checkForm() {
var invalidMsg = "";
if ($('#countryId').val() == "0") {
	invalidMsg += 'Country is required.\n';
	}
if ($('#industryOfferingId').val() == "0") {
	invalidMsg += 'Industry is required.\n';
	}
if ($('#applicationId').val() == "0" || $('#applicationId').val() == "") {
	invalidMsg += 'Application is required.';
	}
if (invalidMsg != "") {
	alert("Please fix the following errors to continue:\n\n"+invalidMsg);
	return false;
	}
}
function getRep() {
	country = $('#countryId').val();
	state = $('#stateId').val();
	industry = $('#industryOfferingId').val();
	app = $('#applicationId').val();
	if (state == null) {
		state = 0;
		}
	$("#representativeSearchBlock").hide();
	$("#pageLoader").show();
	url = "/contact-representative-results/" + country + "/" + state + "/" + industry + "/" + app;
	getServerData(url,'#representativeContactBlock','rep');
}
function getStateOptions() {
	_countryId = $('#countryId').val();
	$("#countryLoader").show();
	url = "/country-states/" + _countryId;
	getServerData(url,'#stateId','state');
}
function getIndustryApplications() {
	_industryOfferingId = $('#industryOfferingId').val();
	$("#industryLoader").show();
	url = "/industry-applications/" + _industryOfferingId;
	getServerData(url,'#applicationId','industry');
}
function previewURL(urlObj) {
var urlValue = $(urlObj).val();
if (urlValue != "") {
	$.Boxen.open(this, {urlAttribute: null, url:urlValue, modal:true});
	}
}
function changeProduct(val) {
if (val == "1") {
	$("#productTypeRow").show();
	}
else {
	$("#productTypeId").selectedIndex = 0;
	$("#productTypeRow").hide();
	}
}