/*******************************************************************************
 * Copyright 2014 Federal Chancellery Austria
 * MOA-ID has been developed in a cooperation between BRZ, the Federal
 * Chancellery Austria - ICT staff unit, and Graz University of Technology.
 *
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 * http://www.osor.eu/eupl/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 *
 * This product combines work with different licenses. See the "NOTICE" text
 * file for details on the various modules and licenses.
 * The "NOTICE" text file is part of the distribution. Any derivative works
 * that you distribute must include a readable copy of the "NOTICE" text file.
 *******************************************************************************/
function oaBusinessService() {
	if ($('#OAisbusinessservice').attr('checked') == 'checked') {
        $('#oa_config_businessservice').css('display', "block");
		$('#oa_config_publicservice').css('display', "none");
		
	} else {
        $('#oa_config_businessservice').css('display', "none");
		$('#oa_config_publicservice').css('display', "block");
		
	}		
}

function oaSSOService() {
	if ($('#OAuseSSO').attr('checked') == 'checked') {
		
		$('#sso_bock').css('display', "block");
		
	} else {
		
		$('#sso_bock').css('display', "none");
		
	}
}
function oaStork() {
	if ($('#OAuseSTORKLogon').is(':checked')) {
		$('#stork_block').css('display', "block");
	} else {
		$('#stork_block').css('display', "none");
	}
}
function oaVIDP() {
	if ($('#oa_vidp_area').css('display') == "none") {
		$('#oa_vidp_area').css('display', "block");
		$('#button_vidp_show').css('display', "none");
		$('#button_vidp_hidden').css('display', "block");
	} else {
		$('#oa_vidp_area').css('display', "none");
		$('#button_vidp_show').css('display', "block");
		$('#button_vidp_hidden').css('display', "none");
	}
}
function oaSAML1() {
	if ($('#oa_saml1_area').css('display') == "block") {
		$('#oa_saml1_area').css('display', "none");
		$('#button_smal1_show').css('display', "block");
		$('#button_saml1_hidden').css('display', "none");
	} else {
		$('#oa_saml1_area').css('display', "block");
		$('#button_smal1_show').css('display', "none");
		$('#button_saml1_hidden').css('display', "block");
	}
}
function oaPVP2(){
	if ($('#oa_pvp2_area').css('display') == "block") {
		$('#oa_pvp2_area').css('display', "none");
		$('#button_pvp2_show').css('display', "block");
		$('#button_pvp2_hidden').css('display', "none");
	} else {
		$('#oa_pvp2_area').css('display', "block");
		$('#button_pvp2_show').css('display', "none");
		$('#button_pvp2_hidden').css('display', "block");
	}
}
function oaOAuth20(){
	if ($('#oa_oauth20_area').css('display') == "block") {
		$('#oa_oauth20_area').css('display', "none");
		$('#button_oauth20_show').css('display', "block");
		$('#button_oauth20_hidden').css('display', "none");
	} else {
		$('#oa_oauth20_area').css('display', "block");
		$('#button_oauth20_show').css('display', "none");
		$('#button_oauth20_hidden').css('display', "block");
	}
}
function oaFormCustom() {
	if ($('#formcustom_area').css('display') == "block") {
		$('#formcustom_area').css('display', "none");
		$('#formcustom_button_show').css('display', "block");
		$('#formcustom_button_hidden').css('display', "none");
	} else {
		$('#formcustom_area').css('display', "block");
		$('#formcustom_button_show').css('display', "none");
		$('#formcustom_button_hidden').css('display', "block");
		$("#formCustomPreview>iframe").attr("src", "./bkuFramePreview.action");
	}
}
function editOA(oaid){
	$('#selectOAForm_OAID').val(oaid);
	$('#selectOAForm').submit();
}
function userOA(userid){
	$('#selectUserForm_OAID').val(userid);
	$('#selectUserForm').submit();	
}
function oaLegacyService() {
	if ($('#OAislegacy').attr('checked') == 'checked') {
		
		$('#oa_config_sltemplates').css('display', "block");
		
	} else {
		
		$('#oa_config_sltemplates').css('display', "none");
		
	}
}
function AdminTarget() {
	if ($('#adminTarget').attr('checked') == 'checked') {
		
		$('#admin_target_area').css('display', "block");
		
	} else {
		
		$('#admin_target_area').css('display', "none");
		
	}
}
function HideAdminTarget() {
	$('#adminTarget').removeAttr("checked");
	$('#admin_target_area').css('display', "none");
}
function oaTargetSubSector() {
	if ($('#OAisTargetSubSector').attr('checked') == 'checked') {
		
		$('#wwgrp_OAtarget_subsector').css('display', "block");
		
	} else {
		
		$('#wwgrp_OAtarget_subsector').css('display', "none");
		
	}
}
function UseUserNamePassword() {
	if ($('#isusernamepasswordallowed').size() == 0) {
		$('#usernamepassword_area').css('display', "none");
		return;
	}
	
	if ($('#isusernamepasswordallowed').attr('checked') == 'checked') {
		if ($('#editUser_user_username').val() == "") {
			$('#editUser_user_username').removeAttr("disabled");
		}
		$('#usernamepassword_area').css('display', "block");
		
	} else {
		$('#editUser_user_username').prop("disabled", "disabled");
		$('#usernamepassword_area').css('display', "none");
		
	}
}
function sendVerificationMail() {
	$.ajax({
		xhr: function() {
			var xhr = new window.XMLHttpRequest();
		    return xhr;
		  },
		  type: "GET",
	      url: "servlet/sendVerificationMail.action",
		  data: "",
		  success: function (msg) {
			  alert(msg);
		  },
		  error: function () {
			  alert('Anfrage konnte nicht gesendet werden!');
			},
	});
}
function updateBKUFormPreview(module) {
	var value = $("#"+module).val();

	$("#formCustomPreview>iframe").attr("src", "./bkuFramePreview.action?module="+module+"&value="+encodeURIComponent(value.replace("#","")));
}

function userOnLoad() {
	UseUserNamePassword();
	return true;
}
function oaOnLoad() {
	oaBusinessService();
	oaSSOService();
	oaLegacyService();
	oaStork();
	AdminTarget();
	oaTargetSubSector();
	
//	oaVIDP();
	
	$(".colorfield").each(
		function() {
			$(this).ColorPicker({
			color : $(this).val(),
			onSubmit : function(hsb, hex, rgb, el) {
			    $(el).val(hex);
			    updateBKUFormPreview($(el).attr("id"));
			}
		    });
		}		
	);
	
	return true;
}
function newPeps() {
	var target = $(document.getElementById("stork_pepslist").getElementsByTagName("tr")).last()[0];
	var clone = target.cloneNode(true);
	var lastindex;
	try {
        lastindex = parseInt(clone.innerHTML.match(/name="storkconfig.cpepslist\[(\d*)\]/)[1]);
    } catch (err) {
        lastindex = 1;
    }
	var nextindex = lastindex + 1;
	clone.innerHTML = '<tr>\
  <td>\
    <div id="wwgrp_loadGeneralConfig_storkconfig_cpepslist_'+nextindex+'__countryCode" class="wwgrp">\
      <div id="wwctrl_loadGeneralConfig_storkconfig_cpepslist_'+nextindex+'__countryCode" class="wwctrl">\
        <input id="loadGeneralConfig_storkconfig_cpepslist_'+nextindex+'__countryCode" class="textfield_short" type="text" value="" name="storkconfig.cpepslist['+nextindex+'].countryCode"></div>\
      </div>\
    </td>\
    <td>\
      <div id="wwgrp_loadGeneralConfig_storkconfig_cpepslist_'+nextindex+'__URL" class="wwgrp">\
        <div id="wwctrl_loadGeneralConfig_storkconfig_cpepslist_'+nextindex+'__URL" class="wwctrl">\
          <input type="text" name="storkconfig.cpepslist['+nextindex+'].URL" value="" id="loadGeneralConfig_storkconfig_cpepslist_'+nextindex+'__URL" class="textfield_long"></div>\
      </div>\
    </td>\
    <td>\
      <div id="wwgrp_loadGeneralConfig_storkconfig_cpepslist_'+nextindex+'__supportsXMLSignature" class="wwgrp">\
        <div id="wwctrl_loadGeneralConfig_storkconfig_cpepslist_'+nextindex+'__supportsXMLSignature" class="wwctrl">\
          <input type="checkbox" name="storkconfig.cpepslist['+nextindex+'].supportsXMLSignature" value="true" checked="checked" id="loadGeneralConfig_storkconfig_cpepslist_'+nextindex+'__supportsXMLSignature">\
          <input type="hidden" id="__checkbox_loadGeneralConfig_storkconfig_cpepslist_'+nextindex+'__supportsXMLSignature" name="__checkbox_storkconfig.cpepslist['+nextindex+'].supportsXMLSignature" value=""></div>\
      </div>\
    </td>\
    <td>\
    <input type="button" value="löschen" onclick="this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);"></td>\
  </tr>';
	target.parentNode.appendChild(clone);
}
function newStorkAttribute() {
	var target = $(document.getElementById("stork_attributelist").getElementsByTagName("tr")).last()[0];
	var clone = target.cloneNode(true);
	var lastindex = parseInt(clone.innerHTML.match(/name="storkconfig.attributes\[(\d)/)[1]);
	var nextindex = lastindex + 1;
	clone.innerHTML = clone.innerHTML.replace(/name="storkconfig.attributes\[\d/g, 'name="storkconfig.attributes[' + nextindex);
	clone.innerHTML = clone.innerHTML.replace(/(.*" value=").*?(".*)/g, '$1$2');
	target.parentNode.appendChild(clone);
}
function newAp() {
	var target = $(document.getElementById("stork_aplist").getElementsByTagName("tr")).last()[0];
	var clone = target.cloneNode(true);
	var lastindex = parseInt(clone.innerHTML.match(/name="storkOA.attributeProviderPlugins\[(\d)/)[1]);
	var nextindex = lastindex + 1;
	clone.innerHTML = clone.innerHTML.replace(/name="storkOA.attributeProviderPlugins\[\d/g, 'name="storkOA.attributeProviderPlugins[' + nextindex);
	clone.innerHTML = clone.innerHTML.replace(/(.*" value=").*?(".*)/g, '$1$2');
	target.parentNode.appendChild(clone);
}