

//------------------------------------------------------------------------------------
// Get parameter with given name from URL
//------------------------------------------------------------------------------------

function getUrlParamValue(_url, _paramName) {
	var url = new String(_url);
	var paramName = new String(_paramName);

	// alert("getUrlParamValue(" + url + ", " + paramName + ")");
	if ((url == null) || (paramName == null) || (paramName.length == 0))
		return "";
	// find parameter in URL
	var indexStart = url.indexOf(paramName + "=");
	if (indexStart < 0)
		return "";
	indexStart += paramName.length + 1;       // "="
	var indexEnd = url.indexOf("&", indexStart + 1);
	if (indexEnd < 0)
		indexEnd = url.length;

	// get parameter value
	return url.substring(indexStart, indexEnd);
}

//------------------------------------------------------------------------------
// trim the spaces enclosed the core of text
//------------------------------------------------------------------------------

function trim(value) {
	if (!value || (value == null))
		return "";
	if (value.length == 0)
		return "";

	var i = 0, startPos = 0, endPos = 0;
	while (i < value.length) {
		if (value.charAt(i) != ' ')
			break;
		i++;
	}
	if (i == value.length)
		return "";
	startPos = i;

	i = value.length - 1;
	while (i > 0) {
		if (value.charAt(i) != ' ') {
			endPos = i;
			break;
		}
		--i;
	}
	if (endPos == 0)
		endPos = startPos;
	return value.substring(startPos, endPos + 1);
}

//------------------------------------------------------------------------------------
// Check if given email address is valid.
// Valid address is not empty and contains one '@' and at least one '.' character after '@'
//------------------------------------------------------------------------------------
function checkEmailAddress(address) {
	if (trim(address) == "")
		return false;
	var index1 = address.indexOf("@");
	var index2 = address.indexOf(".", index1); // start after '@', address can have '.' before '@'
	if ((index1 < 0) || (index2 < 0))
		return false;
//	alert(address.substring(0, index1));
	if (trim(address.substring(0, index1)) == "")
		return false;
//	alert(address.substring(index1 + 1, index2));
	if (trim(address.substring(index1 + 1, index2)) == "")
		return false;
//	alert(address.substring(index2 + 1));
	if (trim(address.substring(index2 + 1)) == "")
		return false;

	return true;
}

//------------------------------------------------------------------------------------
// Check form in Contact element
//------------------------------------------------------------------------------------

function checkContactForm(form) {

  // check name
  form.f_visitor_name.value = trim(form.f_visitor_name.value);
  if (form.f_visitor_name.value.length == 0) {
    alert("Bitte geben Sie Ihren Namen ein.");
    form.f_visitor_name.focus();
    return false;
  }

  // check contact - email or tel or fax or address
  form.f_visitor_email.value = trim(form.f_visitor_email.value);
  form.f_visitor_tel.value = trim(form.f_visitor_tel.value);
  form.f_visitor_fax.value = trim(form.f_visitor_fax.value);
  form.f_visitor_address.value = trim(form.f_visitor_address.value);
  if ((form.f_visitor_email.value.length == 0) &&  (form.f_visitor_tel.value.length == 0) &&
      (form.f_visitor_fax.value.length == 0) && (form.f_visitor_address.value.length == 0)) {
    alert("Bitte geben Sie Ihre Kontaktdaten (E-Mail oder Telefon oder Fax oder Anschrift) ein.");
    form.f_visitor_email.focus();
    return false;
  }

	// check email if not empty
	if (form.f_visitor_email.value.length > 0) {
	  if (!checkEmailAddress(form.f_visitor_email.value)) {
      alert("Die eingegebene E-Mail-Adresse ist nicht korrekt.");
      form.f_visitor_email.focus();
      return false;
		}
	}

	// check text
  form.f_visitor_text.value = trim(form.f_visitor_text.value);
	if (form.f_visitor_text.value.length == 0) {
      alert("Bitte geben Sie einen Beschreibungstext ein.");
      form.f_visitor_text.focus();
      return false;
	}

	return true;
}


//------------------------------------------------------------------------------------
// Submit form in Contact element
//------------------------------------------------------------------------------------
function submitContactForm(form) {
  if (!checkContactForm(form)) {
    return;
  }
  form.f_contact_action.value = 'send_submit';
  form_contact.submit();
}

//------------------------------------------------------------------------------
// Set given {action} to {form}.{actionParamName} parameter. If javascript function
// beforeDoListAction(...) exists then this function is called. If beforeDoListAction(...)
// returns true then {action} is submited if false action is refused. If
// beforeDoListAction(...) doesn't exist then submit is called directly.
//------------------------------------------------------------------------------

function doListAction(form, actionParamName, action, idParamName, itemId) {
	if (window.beforeDoListAction) {
		if (!window.beforeDoListAction(form, action))
			return false;
	}

	eval("document." + form.name + "." + actionParamName + ".value = '" + action + "'");
	if (trim(idParamName) != '')
		eval("document." + form.name + "." + idParamName + ".value = '" + itemId + "'");

	form.submit();
	return true;
}

//------------------------------------------------------------------------------
// Set given {action} to {form}.{actionParamName} parameter. If javascript function
// beforeDoListAction(...) exists then this function is called. If beforeDoListAction(...)
// returns true then {action} is submited if false action is refused. If
// beforeDoListAction(...) doesn't exist then submit is called directly.
//------------------------------------------------------------------------------

function doListActionEx(form, actionParamName, action, idParamName_1, itemValue_1, idParamName_2, itemValue_2) {
	if (window.beforeDoListAction) {
		if (!window.beforeDoListAction(form, action))
			return false;
	}

	eval("document." + form.name + "." + actionParamName + ".value = '" + action + "'");
	if (trim(idParamName_1) != '') {
		eval("document." + form.name + "." + idParamName_1 + ".value = '" + itemValue_1 + "'");
	}
	if (trim(idParamName_2) != '') {
		eval("document." + form.name + "." + idParamName_2 + ".value = '" + itemValue_2 + "'");
	}

	form.submit();
	return true;
}

//------------------------------------------------------------------------------
// Set given {pageIndex} to {form}.f_next_page parameter. If javascript
// function beforeGoToListPage(...) exists then this function is called. If
// beforeGoToListPage(...) returns true then form is submited if false submit is refused.
// If beforeGoToListPage(...) doesn't exist then submit is called directly.
//------------------------------------------------------------------------------

function goToListPage(form, pageIndex) {
	if (window.beforeGoToListPage) {
		if (!window.beforeGoToListPage(form, pageIndex))
			return false;
	}

	form.f_next_page.value = pageIndex;
	form.submit();
	return false;
}

//------------------------------------------------------------------------------
// Check form parameter for HtDig search
//------------------------------------------------------------------------------

function checkSubmitSucheForm(form, errmsgMisingWords) {
	// check "words" parameter
	if (form.f_words_and.value.length > 0) {
		form.f_search_words.value = form.f_words_and.value;
		form.f_search_method.value = "and";
	} else {
		if (form.f_words_or.value.length > 0) {
			form.f_search_words.value = form.f_words_or.value;
			form.f_search_method.value = "or";
		} else {
			alert((errmsgMisingWords) ? errmsgMisingWords : "Words missing");
			form.f_words_and.focus();
			return false;
		}
	}
	// check "date" parameter
	var selectedDate = form.f_date[(form.f_date.selectedIndex >= 0) ? form.f_date.selectedIndex : 0].text;
	if (selectedDate == "seit gestern") {
		form.f_search_endday.value = "-1";
	} else if (selectedDate == "in der letzten Woche") {
		form.f_search_endday.value = "-7";
	} else if (selectedDate == "im letzten Monat") {
		form.f_search_endmonth.value = "-1";
	} else if (selectedDate == "im letzten Jahr") {
		form.f_search_endyear.value = "-1";
	}

	// check "matchesperpage"
	var selectedMatches = form.f_nrresults[(form.f_nrresults.selectedIndex >= 0) ? form.f_nrresults.selectedIndex : 0].text;
	// -- number is first, separated with space
	selectedMatches = selectedMatches.substring(0, selectedMatches.indexOf(" "));
	form.f_search_matchesperpage.value = selectedMatches;

	return true;
}

//------------------------------------------------------------------------------
// Call HtDig search page with parameters
//------------------------------------------------------------------------------

function doHtDigSearch(searchPageUrl, restrictTo, form) {
	if (!form.f_search_text.value)
		form.f_search_text.value = "";
		
	lnk = searchPageUrl + "?f_action=search&f_search_words=" + escape(form.f_search_text.value)
    + ((restrictTo != "") ? "&f_search_restrict=" + restrictTo : "");
	window.location.href = lnk;
	return false;
}

//------------------------------------------------------------------------------
// Open AssetManagement window as popup.
// @param smPagesFolder site man. pages folder start and finished with delimiter "/"
// @param imageId string representing image ID
//------------------------------------------------------------------------------
var ASSET_TYPE_IMAGE  = "image"; // Image file
var ASSET_TYPE_LINK   = "link";  // Link content (set of the files with the same sense)
var ASSET_TYPE_TEXT   = "text";  // Text content created by author
var ASSET_TYPE_HTML   = "html";  // HTML code content
var ASSET_TYPE_FILE   = "file";  // Download file

function callAm(smPagesFolder, assetType, assetId, callbackId) {
	var win, viewParams, initParams, winName, leftPosition, winHeight;

	initParams = 'f_opener_type=admin&f_asset_type=' + assetType;
	viewParams = ( ((trim(assetId) != "") && (eval(assetId) > 0)) ? "&f_action=view&f_asset_id=" + assetId : "&f_action=list" );
	viewParams += ( (trim(callbackId) != "") ? "&f_callback_id=" + trim(callbackId) : "");
	winName = 'am_admin';
	leftPosition = screen.width - 539;
	winHeight = ( (screen.width >= 800) ? 700 : 550 );

	win = window.open(smPagesFolder + 'am.html?' + initParams + viewParams, winName,
		'width=519, height=' + winHeight + ', top=10, left=' + leftPosition + ', scrollbars=no, menubar=no');

	win.opener = self;
	win.focus();
}

//------------------------------------------------------------------------------
// Open category selection window
//------------------------------------------------------------------------------

function callSelectCategoryWindow(smPagesFolder, mySiteId, contentType, callbackId, selectedCategories) {
	if (!selectedCategories || (trim(selectedCategories) == "all")) {
		selectedCategories = "";
	}

	var win = window.open(smPagesFolder + "public/select_categories.html?f_mysite_id=" + mySiteId
			+ "&f_content_type=" + contentType + "&f_callback_id=" + callbackId
			+ "&f_selected_categories=" + selectedCategories,
		"PublicSelectCategories", "toolbar=no, location=no, directories=no, status=no, menubar=0, scrollbars=yes, "
			+ "resizable=yes, top=10, left=" + ((screen.width - 542) / 2) + ", width=542, height=600");

	if (win != null) {
		win.opener = self;
		win.focus();
	}
}

//------------------------------------------------------------------------------
// Call 72h AG mysite search by zipcode page with parameters
//------------------------------------------------------------------------------

function doH72AGSearch(searchPageUrl, zipcode) {
    if (!zipcode) {
        zipcode = "";
    }
    var lnk = searchPageUrl + "?f_action=search&f_h72agsearch_page_size=20"
        + "&f_h72agsearch_zipcode=" + zipcode + "&esn=search_h72ag&edn=body";
    window.location.href = lnk;
    return false;
}


// =============================================================================


