function disableSubmit(obj,index) {
  obj.disabled=true; 
  document.forms[index].submit();
}

var disableLink_obj;
function disableLink(obj) {
  disableLink_obj=obj;
  window.setTimeout("disableLink_obj.disabled=true",100); 
}

function submit(obj) {
 $(obj).submit();
}
var submitSelectAllList = new Array();
function addSubmitSelectAllEntry(obj){
	submitSelectAllList.push(obj);
};
function submitForm(obj,trigger) {
	if(Element.hasClassName(trigger,'buttonDisable')){
		return;
	}
	Element.addClassName(trigger,'buttonDisable');	
	Element.removeClassName(trigger,'buttonSubmit');
	for(var i = 0; i < submitSelectAllList.length ; i++){
		submitSelectAllList[i].selectAll();
	}	
    submit(obj);
}

 
function addSubmitParam(formId,param){
	if(/\?/.test($(formId).action)) {		
			$(formId).action = $(formId).action+'&'+param;
	}else{
		$(formId).action = $(formId).action+'?'+param;
	}
}


function getParentNodeByTag(element,tagName){
	var parentNode = element.parentNode;	
	if(parentNode.tagName.capitalize() == tagName.capitalize()){
		return parentNode;
	}else if (parentNode.tagName.capitalize() == 'BODY'.capitalize()){
		return; 
	}
	return getParentNodeByTag(parentNode,tagName)
}

function foobar(status) {
	if (document.getElementById('relative')) {
	if(document.getElementById('relative').checked == true || status == true){
		document.getElementById('matching_matchingType').disabled = "";
	}else{
		document.getElementById('matching_matchingType').selectedIndex = -1;
		document.getElementById('matching_matchingType').disabled = "disabled";
	}  		
	}
}     

var matchIds = new Array('PROPOSAL','UNKNOWN','NEGATIVE','ACCESSORY','UNASSESSABLE','RELATED','POSITIVE');
var onmouseoverHighlight = true;
var matchingParameters = new Array();
function changeMatchingStateForCurrentList(indexPos,oldState,newState) {
	if(newState == 'NO ACTION'){
		return;
	}
	onmouseoverHighlight = false;        
	for (var i = 0; i < indexPos.length ; i++) {
	   parameters = matchingParameters[indexPos[i]];
	   if( parameters.matchingStatus == oldState || (oldState == 'UNCONFIRMED' && parameters.confirmed == false)){
		   $('mymatchingEntry['+indexPos[i]+']').scrollTo();	       
		   $('mymatchingEntry['+indexPos[i]+']').className='auto_matching_state_change';
		   changeMatchingState(indexPos[i], newState);
		   $('mymatchingEntry['+indexPos[i]+']').className='list_results_tr';
	   }
	   
	   
	}

	if($('changeMatchingStateForCurrentList').type == 'select-one'){
		$('changeMatchingStateForCurrentList').value = 'NO ACTION';
	}

	$('changeMatchingStateForCurrentList').scrollTo();

	onmouseoverHighlight = true;
}
function changeMatchingState(indexPos, state) {
	
    if($('mymatchingReviewedStatus['+indexPos+']')){
    	$('mymatchingReviewedStatus['+indexPos+']').hide();
    }
    $('choice['+indexPos+']_' + state).checked = true;
    
    matchingParameters[indexPos].confirmed = true;    
    
    matchingParameters[indexPos].matchingStatus =state;
    
    new Ajax.Request('updateMatching.action',{asynchronous:false,parameters:matchingParameters[indexPos]});  
}      

function getCheckedParentProduct(productId, index) {
	var checkboxList = $$("input[name='parent-of-" + productId + "-" + index + "']");
	if (checkboxList.size() == 1) {
		return checkboxList[0].checked ? checkboxList[0].value : 0;
	}
	return 0;
}

function getCheckedChildProduct(productId, index) {
	var checkboxList = $$("input[name='child-of-" + productId + "-" + index + "[]']");
	for (i=0; i < checkboxList.length; i++) {
		if (checkboxList[i].checked == true) {
			return checkboxList[i].value;
		}
	}
	return 0;
}

function checkChildProduct(checkbox) {
	var checkboxList = $$("input[name='" + checkbox.name + "']");
	for (i=0; i < checkboxList.length; i++) {
		if (checkboxList[i].value != checkbox.value) {
			checkboxList[i].checked = false;
		}
	}
}

function displayMatching(papid) {
    if (document.getElementById('hide1-'+papid).style.display == "none") {
    	document.getElementById('hide1-'+papid).style.display = "inline";
    	document.getElementById('hide2-'+papid).style.display = "none";
    	document.getElementById('hide3-'+papid).style.display = "none";
    } else {
    	document.getElementById('hide1-'+papid).style.display = "none";
    	document.getElementById('hide2-'+papid).style.display = "";
    	document.getElementById('hide3-'+papid).style.display = "";
    }
} 

function onmouseoverEffect(obj) {
	if(obj.src) {
		obj.src = obj.src.sub('_off','_on');
	} 
}
function onmouseoutEffect(obj) {
	if(obj.src) {
		obj.src = obj.src.sub('_on','_off');
	} 
}
function activateGeneratedButton() {
	$('generatedButton').src = $('generatedButton').src.sub('_deactivated','_off');
	$('generatedButton').setStyle("cursor: pointer;");
}

function checkUploadConditions() {
	if($('reviewBadge1').checked || $('reviewBadge2').checked || $('opinionBadge1').checked ){
		$('sendButton').src = $('sendButton').src.sub('_deactivated','_off');
		$('sendButton').addClassName('clickable');
	}else{
		$('sendButton').src = $('sendButton').src.sub('_off','_deactivated');
		$('sendButton').removeClassName('clickable');
	}	
}

function sendUploadForm(){
	if($('reviewBadge1').checked || $('reviewBadge2').checked || $('opinionBadge1').checked ){
		submit('uploadForm');
	}
}

var productId = 0;
var productLink = '';
var productName = '';
var timeMillis = 0;

function updateCodeForVersion1(code){
   code = code.gsub('##PRODUCT_ID##',productId);
   code = code.gsub('##PARTNER_ID##',partnerId);
   code = code.gsub('##TIME_MILLIS##',timeMillis);
   code = code.gsub('##WIDTH##',parseInt($F('v1_width')));
   code = code.gsub('##PRODUCT_LINK##',productLink);
   code = code.gsub('##PRODUCT_NAME##',productName);
   return code;
}
var resizeUrlLocale = '';
function updateCodeForVersion2(code,display){
   code = code.gsub('##PRODUCT_ID##',productId);
   code = code.gsub('##PARTNER_ID##',partnerId);
   code = code.gsub('##TIME_MILLIS##',timeMillis);
   code = code.gsub('##PRODUCT_LINK##',productLink);
   code = code.gsub('##PRODUCT_NAME##',productName);
   code = code.gsub('##FONTSIZE##',$F('v2_fontsize'));
   code = code.gsub('##FONTFAMILY##',$F('v2_fontfamily'));   
   width = parseInt($F('v2_width'));
   if( width < 400){
   	width = 400;
   }
   if(!display){
      code = code.gsub('##RESIZE_URL##',$F('v2_resizeURL'));
   	  code = code.gsub('##WIDTH##',parseInt($F('v2_width')));
   }else{
      code = code.gsub('##RESIZE_URL##',resizeUrlLocale);
   	 
   	  if(width > 500){
   	  	code = code.gsub('##WIDTH##',500);
   	  }else{
   	  	code = code.gsub('##WIDTH##',width);
   	  }
   }
   return code;
}      
var cssUrlLocale = '';
function updateCodeForVersion3(code,display){
   code = code.gsub('##PRODUCT_ID##',productId);
   code = code.gsub('##PARTNER_ID##',partnerId);
   code = code.gsub('##TIME_MILLIS##',timeMillis);
   code = code.gsub('##PRODUCT_LINK##',productLink);
   code = code.gsub('##PRODUCT_NAME##',productName);
   width = parseInt($F('v3_width'));
   if( width < 400){
   	width = 400;
   }
   if(!display){
      code = code.gsub('##RESIZE_URL##',$F('v3_resizeURL'));
      code = code.gsub('##CSS_URL##','');
      code = code.gsub('##WIDTH##',parseInt($F('v3_width')));
   }else{
      code = code.gsub('##RESIZE_URL##',resizeUrlLocale);
      code = code.gsub('##CSS_URL##',cssUrlLocale);
   	 
   	  if(width > 500){
   	  	code = code.gsub('##WIDTH##',500);
   	  }else{
   	  	code = code.gsub('##WIDTH##',width);
   	  }
   }
   return code;
}      

var previewCode ='';
function updatePreview(code){
	previewCode = code;
	if($('preview').visible()){
		$('preview').update(previewCode);
	}
}

function showOrHidePreview(){
	if($('preview').visible()){
		$('preview').hide();
	}else{
		$('preview').update(previewCode);
		$('preview').show();
	}
}
function getSelectionId(text, li) {
	var path = Importer.path.replace(/js/,'');
	new Ajax.Updater('productView',path+'/badge/product/view.action',{evalScripts:true,asynchronous:false,parameters:{productId:li.id}});
	if($('productOpinionCountWarning')){
		if($F('productOpinionCount') == 0){
			$('productOpinionCountWarning').show();	
		}else{
			$('productOpinionCountWarning').hide();
		}
	}
}

function generated(){
	var trackId
	if($('reviewBadge1').checked)
	{
		trackId = 3;
		$('tr3').update($('tr3_v1').innerHTML);
		$('v1CodeDisplay').value =updateCodeForVersion1($F('v1Code'));	
		updatePreview(updateCodeForVersion1($F('v1Code')));
		$('tr3').show();
		$('tr3_preview').show();
		$('tr4').show();
	}else if ($('reviewBadge2').checked){
		trackId = 4;
		$('tr3').update($('tr3_v2').innerHTML);
		$('v2CodeDisplay').value = updateCodeForVersion2($F('v2Code'),false);
		updatePreview(updateCodeForVersion2($F('v2Code'),true));
		$('tr3').show();
		$('tr3_preview').show();
		$('tr4').show();
	}else if($('opinionBadge1').checked){
		trackId = 6;
		$('tr3').update($('tr3_v3').innerHTML);
		$('v3CodeDisplay').value = updateCodeForVersion3($F('v3Code'),false);
		updatePreview(updateCodeForVersion3($F('v3Code'),true));
		$('tr3').show();
		$('tr3_preview').show();
		$('tr4').show();
	}
	
	if(piwikTracker && trackId){
		piwikTracker.trackGoal(trackId);
	}
	
}

function onGetProductComplete(){
	$('tr2').show();
	if($('reviewBadge1').checked) {
		$('v1CodeDisplay').value =updateCodeForVersion1($F('v1Code'));	
		updatePreview(updateCodeForVersion1($F('v1Code')));	
	} else if ($('reviewBadge2').checked) {
		$('v2CodeDisplay').value = updateCodeForVersion2($F('v2Code'),false);
		updatePreview(updateCodeForVersion2($F('v2Code'),true));
	} else if($('opinionBadge1').checked) {
		$('v3CodeDisplay').value = updateCodeForVersion3($F('v3Code'),false);
		updatePreview(updateCodeForVersion3($F('v3Code'),true));
	}
}


function setSubmitDisableOrEnable(id,enable) {
	if(enable) {
		$(id).show();
		$(id+"_disable").hide();
	} else {
		$(id+"_disable").show();
		$(id).hide();
	}
}

function acceptedBadgeTos(badgeType,num){
	$(badgeType+'TosAcceptedTrue').disable();
	$(badgeType+'TosAcceptedFalse').disable();
	for ( var i = 1; i <= num; i++) {
		$(badgeType+i).enable();
		$(badgeType+'Title'+i).removeClassName('disabled');
		$(badgeType+'TosMsg'+i).hide();
	}
	
	var path = Importer.path.replace(/js/,'');
	new Ajax.Request(path+'/badge/registration/update/tos.action',{asynchronous:false,parameters:{badgeType:badgeType}});
}

/*
 * for tracking
 * script = 1 == compact version
 * script = 2 == fazit complete version
 */
function finishGoal(script)
{

}


function changeMachtingStatus(ele){
	if(ele.value == 351){
		$('matchingStatus').hide();
		$('matchingStatus').disable();
		$('matchingStatusForTestberichte').show();
		$('matchingStatusForTestberichte').enable();
	}else{
		$('matchingStatusForTestberichte').hide();
		$('matchingStatusForTestberichte').disable();
		$('matchingStatus').show();
		$('matchingStatus').enable();
	}
}

function submitNavigation(lineStart){
	addSubmitParam('matching','navigation=true&lineStart='+lineStart);
	for(var i = 0; i < submitSelectAllList.length ; i++){
		submitSelectAllList[i].selectAll();
	}	
	submit('matching');
}

var semanticBox;
var autocompleterMap = new Hash();
var activeClassMap = new Hash();
var currentIndexPosForSemanticBox;
function openSemanticBox(triger,indexPos){
	if(!semanticBox){
		semanticBox = $('semanticBox');
	}
	currentIndexPosForSemanticBox = indexPos;
	semanticBox.hide();
	Element.clonePosition(semanticBox,triger,{setWidth:false,setHeight:false});
	var path = Importer.path.replace(/js/,'');
	new Ajax.Request(path+'/semanticEditor.action',{asynchronous:false,parameters:matchingParameters[indexPos],
		method: 'get',
		onSuccess: function(transport) {
		semanticBox.down('.content').update(transport.responseText);
		semanticBox.down('.offerName').innerHTML = triger.up('td').down('.offerName').innerHTML;
		semanticBox.show(); 
		}});
}
function addSemanticToken(){
	var lastEntry =$$('li.semanticEditorMapEntry').last();
	var newEntry = '<li class="semanticEditorMapEntry">'+lastEntry.innerHTML+'</li>';	
	var index;
	newEntry = newEntry.gsub(/semanticEditorMap\[(\d+)\]/, function(match){index=parseInt(match[1])+1 ;return 'semanticEditorMap[' + index +']'});	
	lastEntry.up('ul').insert(newEntry,'bottom');
	creatAutocompleter('semanticEditorMap['+index+'][autocomplete]');
	activeClassMap.set('semanticEditorMap['+index+']',activeClassMap.get('semanticEditorMap['+(index-1)+']'));
	switchObjectSelector($('semanticEditorMap['+index+'][semanticClassId]'));
	Element.down('semanticBox','.middle').scrollTop = Element.down('semanticBox','.middle').scrollHeight;
	
}
function closeSemanticBox(){
	autocompleterMap = new Hash();
	activeClassMap = new Hash();
	$('semanticBox').hide();
}
function saveSemanticBox(){
	$('semanticTokensForm').request(
			{onComplete: onCompleteSave.curry(currentIndexPosForSemanticBox)}		
	);
}
function onCompleteSave(transport){
	var arg =$A(arguments);
	Element.update('semanticTarget['+arg[0]+']',arg[1].responseText);
	closeSemanticBox();
}
function creatAutocompleter(id){
	if(!autocompleterMap.get(id)){
	 var path = Importer.path.replace(/js/,'');
	 autocompleterMap.set(id,new Ajax.Autocompleter(id, id+'[choices]', path+'/search.action',{paramName: "searchStr",minChars: 2,indicator: id+'[indicator]',callback:beforSearch,afterUpdateElement:afterUpdateElement}));
	}
}
function beforSearch(obj,querystring){
	Element.hide(obj.id+'[selected]');
	$(obj.id+'[hidden]').value = '';
	var id = obj.id.sub('autocomplete','semanticClassId');
	return querystring+'&searchType='+$(id).value;
}	

function afterUpdateElement(obj, li) {	
	$(obj.id+'[hidden]').value = li.id;
	Element.show(obj.id+'[selected]');
}
function switchObjectSelector(obj){	
	var id = obj.id.sub(/\[semanticClassId\]/,'');
	if(!activeClassMap.get(id)){
		activeClassMap.set(id,5);
	}
	var activeClass = activeClassMap.get(id);
	var newClass = obj.value;
	if(activeClass == newClass){
		return;
	}
	$(id+'[autocomplete][hidden]').value = '';
	if(activeClass == 1 || activeClass == 2){
		Element.hide(id+'[autocomplete]');
		$(id+'[autocomplete]').value = '';
		Element.hide(id+'[autocomplete][selected]');
	}else{
		Element.hide(id+'[objectSelector]['+activeClass+']');
	}
	
	if(newClass == 1 || newClass == 2){
		Element.show(id+'[autocomplete]');
	}else{
		Element.show(id+'[objectSelector]['+newClass+']');
		$(id+'[autocomplete][hidden]').value = $(id+'[objectSelector]['+newClass+']').value;
	}
	activeClassMap.set(id,newClass);
}


