
//
// fills the second rubric selection box depending on the item selected in the first ("market") box
//
// @autor Christian A Vogl
//

var ajaxurl = "/onetz/";
var newopts = "";
var preset_branch = "";

function changeRubricBox(idx,label) {

    if (arguments.length > 2) {
	var formname = arguments[2];
    } else var formname = "form_submit_offer";

    // first, empty the box

    eval ("var nr = document." + formname + ".f_rubric.length");
    if (nr > 0) {
	for (i = nr-1; i>=0; i--) eval("document." + formname + ".f_rubric.options[i] = null");
    }


    try {
	eval ("var subnr = document." + formname + ".f_subrubric.length");
    } catch(error) {
	var subnr = 0;
    }

    if (subnr > 0) {
	for (i = nr-1; i>=0; i--) eval ("document." + formname + ".f_subrubric.options[i] = null");
	// default entry
	newEntry = new Option(label,'',true,true);
	eval ("document." + formname + ".f_subrubric.options[0] = newEntry");
    }

    // initialize some variables
    var j = 0;
    var place = 0;

    // default entry
    newEntry = new Option(label,'',true,true);
    eval ("document." + formname + ".f_rubric.options[0] = newEntry");


    // which items children to display?

    // (1) get the parent
    eval ("var the_value = document." + formname + ".f_rubric_type.options[idx].value");

    // (2) children are held in a variable "opts<id_of_parent>" or in "newopts" (latter one via AJAX)
    if (arguments.length > 3) {
	var the_var = arguments[3];
    } else var the_var = 'opts' + the_value;

    //alert(the_var);

    var the_vals = eval(the_var);
    the_vals = the_vals.replace(/&#252\;/g,"ü");
    the_vals = the_vals.replace(/&auml\;/g,"ä");
    the_vals = the_vals.replace(/&#228\;/g,"ä");
    the_vals = the_vals.replace(/&#214\;/g,"Ö");
    the_vals = the_vals.replace(/&#246\;/g,"ö");
    the_vals = the_vals.replace(/&#196\;/g,"Ä");
    the_vals = the_vals.replace(/&#223\;/g,"ß");
    //the_vals = the_vals.replace(/&#189\;/g,"-1\/2");

    // (3) children are given as a comma-separated list: "rubricid_0,rubricname_0,rubricid_1,rubricname_1, ..."
    the_options = the_vals.split(',,,');

    // any children at all?
    the_length = the_options.length;

    if (the_length > 1) {

	// (4) loop 'em!
	for (i=0; i<the_length; i++) {

	    j = i;
	    i++;
	    place = 1 + (j / 2) ;

	    // (4a) new option, not default, not selected
	    newEntry = new Option(the_options[i],the_options[j]);
	    eval ("document." + formname + ".f_rubric.options[place] = newEntry");

	}

    } 

}



function changeSubrubricBox(idx) {

    // first, empty the box

    var nr = document.form_submit_offer.f_rubric.length;
    if (nr > 0) {
	for (i = nr-1; i>=0; i--) document.form_submit_offer.f_subrubric.options[i] = null;
    }

    // initialize some variables
    var j = 0;
    var place = 0;

    // default entry
    newEntry = new Option('Bei Bedarf wählen','',true,true);
    document.form_submit_offer.f_subrubric.options[0] = newEntry;


    // which items children to display?

    // (1) get the parent
    var the_value = document.form_submit_offer.f_rubric.options[idx].value;

    // (2) children are held in a variable "subopts<id_of_parent>"
    var the_var = 'subopts' + the_value;
    var the_vals = eval(the_var);
    the_vals = the_vals.replace(/&#252\;/g,"ü");
    the_vals = the_vals.replace(/&auml\;/g,"ä");
    the_vals = the_vals.replace(/&#228\;/g,"ä");
    the_vals = the_vals.replace(/&#214\;/g,"Ö");
    the_vals = the_vals.replace(/&#246\;/g,"ö");
    the_vals = the_vals.replace(/&#196\;/g,"Ä");
    the_vals = the_vals.replace(/&#223\;/g,"ß");
    //the_vals = the_vals.replace(/&#252\;/g,"ü");

    // (3) children are given as a comma-separated list: "rubricid_0,rubricname_0,rubricid_1,rubricname_1, ..."
    the_options = the_vals.split(',,,');

    // any children at all?
    the_length = the_options.length;

    if (the_length > 1) {

	// (4) loop 'em!
	for (i=0; i<the_length; i++) {

	    j = i;
	    i++;
	    place = 1 + (j / 2) ;

	    // (4a) new option, not default, not selected
	    newEntry = new Option(the_options[i],the_options[j]);
	    document.form_submit_offer.f_subrubric.options[place] = newEntry;

	}

    } 

}


function addMsgRubric() {

    var new_val = document.form_submit_offer.f_rubric.options[document.form_submit_offer.f_rubric.selectedIndex].value;
    if (new_val == "") return false;

    var new_val_len = new_val.length;

    var the_values = document.form_submit_offer.f_rubric_list.value;
    var the_texts = document.form_submit_offer.f_rubric_list_text.value;
    var the_values_len = the_values.length;
    var extended_new_val = new RegExp(" " + new_val + " ","i"); 

    if (the_values.substring(0,new_val_len) == new_val || the_values.substring(the_values_len - new_val_len, the_values_len) == new_val || the_values.search(extended_new_val) != "-1") return false;

    if (the_values == "") {
	the_values = new_val;
    } else {
	the_values = the_values + " " + new_val;
    }

    if (the_texts == "") {
	the_texts = document.form_submit_offer.f_rubric.options[document.form_submit_offer.f_rubric.selectedIndex].text;
    } else {
	the_texts = the_texts + ", " + document.form_submit_offer.f_rubric.options[document.form_submit_offer.f_rubric.selectedIndex].text;
    }

    document.form_submit_offer.f_rubric_list.value = the_values;
    document.form_submit_offer.f_rubric_list_text.value = the_texts;

    return true;

}


function addMsgMarket() {

    var new_val = document.form_submit_offer.f_rubric_type.options[document.form_submit_offer.f_rubric_type.selectedIndex].value;
    if (new_val == "") return false;

    var new_val_len = new_val.length;

    var the_values = document.form_submit_offer.f_msg_rubric_types.value;
    var the_texts = document.form_submit_offer.f_market_list_text.value;
    var the_values_len = the_values.length;
    var extended_new_val = new RegExp(" " + new_val + " ","i"); 

    if (the_values.substring(0,new_val_len) == new_val || the_values.substring(the_values_len - new_val_len, the_values_len) == new_val || the_values.search(extended_new_val) != "-1") return false;

    if (the_values == "") {
	the_values = new_val;
    } else {
	the_values = the_values + " " + new_val;
    }

    if (the_texts == "") {
	the_texts = document.form_submit_offer.f_rubric_type.options[document.form_submit_offer.f_rubric_type.selectedIndex].text;
    } else {
	the_texts = the_texts + ", " + document.form_submit_offer.f_rubric_type.options[document.form_submit_offer.f_rubric_type.selectedIndex].text;
    }

    document.form_submit_offer.f_msg_rubric_types.value = the_values;
    document.form_submit_offer.f_market_list_text.value = the_texts;

    return true;

}


function PopIt(address) {

    var _ad_popup = window.open(address,'__ad_popup','width=210,height=310,screenX=0,screenY=0,buttons=no,status=no,scrollbars=yes,resizable=yes,location=no');

}


function GetCookie(name) {

    var allcookies = document.cookie;
    var pos = allcookies.indexOf(name + "=");

    if (pos == -1) return null;

    var namelength = name.length;
    var start = pos + name + 1;
    var end = allcookies.indexOf(";", start);
    if (end == -1) end = allcookies.length;

    var value = allcookies.substring(start,end);
    value = unescape(value);

    return value;

}

function SetCookie(name,value,expires,path,domain) {

    document.cookie = name + "=" + value + "; expires=" + expires.toGMTString() + "; path=" + path + "; domain=" + domain;
    return;

}


function ClearCookie(name,path,domain) {

    if (GetCookie(name)) document.cookie = name + "=; path=" + path + "; domain=" + domain + "; expires=Thu, 31-Jan-90 00:00:01 GMT";
    return;

}


var newdate = new Date();
newdate.setTime(newdate.getTime() + (0.25*60*60*1000));


function PopUpAd(url,domain,path) {

    var poptest = GetCookie('seenpop');

    if (poptest == null || poptest == "") {
	SetCookie('seenpop', 1, newdate, path, domain);
	PopIt(url);
    }

    return;

}


function jsFormatTextSelection(area, tag) {

  tag = unescape(tag);

  if (window.opera) {
    wrapOpera(tag);
  } else if (!(window.opera) && document.all) {
    wrapIE(tag);
  } else if (document.getElementById) {
    wrapMoz(area, tag);
  } else {
      alert('Sie verwenden keinen passenden Browser!');
  }

}

function wrapIE(tag) {
    strSelection = document.selection.createRange().text;
    if (strSelection!="") {
	document.selection.createRange().text = "[" + tag + "]" + strSelection + "[/" + tag + "]";
    }
    return;
}

function wrapMoz(txtarea, tag) {
    var ttt = txtarea.value;
    var selLength = txtarea.textLength;
    var selStart = txtarea.selectionStart;
    var selEnd = txtarea.selectionEnd;
    if (selStart == 'undefined') return;

    if (selEnd==1 || selEnd==2) selEnd=selLength;
    var s1 = ttt.substring(0,selStart);
    var s2 = ttt.substring(selStart, selEnd);
    var s3 = ttt.substring(selEnd, selLength);

    // check for tag already set before selection
    var taglength = tag.length;

    var s1length = s1.length;

    if (s1.substring(s1length - taglength - 2, s1length) == "[" + tag + "]") {
	
	s1 = s1.substring(0,s1length - taglength - 2);

	if (s3.substring(0, taglength + 3) == "[/" + tag + "]") {
	    var s3length = s3.length;
	    s3 = s3.substring(taglength + 3, s3length);
	} else  {
	    var s2length = s2.length;
	    if (s2.substring(s2length - taglength - 3, s2length) == "[/" + tag + "]") s2 = s2.substring(0,s2length - taglength - 3);
	}
	
	txtarea.value = s1 +  s2 + s3;

    } else if (s2.charAt(0) == "[") {
	// tag also selected, starting with bracket?
	s2 = s2.replace(/[[^]]*]/,"");
	s2 = s2.replace(/[\/[^]]*]/,"");
	txtarea.value = s1 + s2 + s3;
    } else {
	txtarea.value = s1 + "[" + tag + "]" + s2 + "[/" + tag + "]" + s3;
    }
    return;
}

function wrapOpera(txtarea, tag) {

    alert('Leider funktioniert die Text-Markierung nicht im Opera-Browser!');
    return;

    var ttt = txtarea.value;
    var selLength = txtarea.textLength;
    var selStart = txtarea.selectionStart;
    var selEnd = txtarea.selectionEnd;

    alert('length: ' + selLength + ' - start: ' + selStart + ' - end: ' + selEnd);

    if (selEnd==1 || selEnd==2) selEnd=selLength;
    var s1 = ttt.substring(0,selStart);
    var s2 = ttt.substring(selStart, selEnd);
    var s3 = ttt.substring(selEnd, selLength);
    txtarea.value = s1 + "[" + tag + "]" + s2 + "[/" + tag + "]" + s3;
    return;
}



function setBranchesVisible(formname, formelement, branchboxlayer) {
    var currval = eval("document." + formname + "." + formelement + ".options[document." + formname + "." + formelement + ".selectedIndex].value");
    if (currval == "")
	document.getElementById(branchboxlayer).style.display = "none";
    else document.getElementById(branchboxlayer).style.display = "";


    return;
}

function ajaxBuildBranchUrl(formname, formelement, branchboxlayer, countcheck) {

    exchangelayer = branchboxlayer;
    var rubric_id = eval("document." + formname + "." + formelement + ".options[document." + formname + "." + formelement + ".selectedIndex].value");
    ajaxurl = "/ajaxcalls/index.adp?object_id=" + rubric_id + "&mode=rubrictype&countcheck="+countcheck;

    if (arguments.length > 4) {
	if (arguments[4] == 'preset') preset_branch = arguments[5];
    }

};


function newAjaxRequest() {

    try {

	if( window.XMLHttpRequest ) {
	    req = new XMLHttpRequest();
	} else if( window.ActiveXObject ) {
	    req = new ActiveXObject( "Microsoft.XMLHTTP" );
	} else {
	    alert( "Ihr Browser unterst&uuml;tzt die notwendige Funktionalit&auml;t nicht (AJAX)!" );
	}

	req.open( "GET", ajaxurl, true );
	req.onreadystatechange = ajaxSwitchHTML;
	req.send( null );

    } catch( e ) {

	alert( "Fehler: " + e );

    }

    return true;

}




function ajaxSwitchHTML() {

    if(req.readyState == 4) {

	if( req.status == 200 ) {

	    if (exchangelayer == 'branchbox' || exchangelayer == 'ypregisterbranchbox') {
		if (exchangelayer == 'ypregisterbranchbox') {
		    var nr = document.nt_ypregister_form.f_rubric.length;
		    if (nr > 0) {
			for (i = nr-1; i>=0; i--) document.nt_ypregister_form.f_rubric.options[i] = null;
		    }
		} else {
		    var nr = document.form_submit_yellow.f_rubric.length;
		    if (nr > 0) {
			for (i = nr-1; i>=0; i--) document.form_submit_yellow.f_rubric.options[i] = null;
		    }
		}
		newopts = req.responseText;
		var the_vals = newopts; 
		the_vals = the_vals.replace(/&#252\;/g,"ü");
		the_vals = the_vals.replace(/&auml\;/g,"ä");
		the_vals = the_vals.replace(/&#228\;/g,"ä");
		the_vals = the_vals.replace(/&#214\;/g,"Ö");
		the_vals = the_vals.replace(/&#246\;/g,"ö");
		the_vals = the_vals.replace(/&#196\;/g,"Ä");
		the_vals = the_vals.replace(/&#223\;/g,"ß");
		the_options = the_vals.split(',,,');
		the_length = the_options.length;

		if (the_length > 1) {

		    // (4) loop 'em!
		    for (i=0; i<the_length; i++) {

			j = i;
			i++;
			place = 1 + (j / 2) ;

			// (4a) new option, not default, not selected
			if (preset_branch == the_options[j]) newEntry = new Option(the_options[i],the_options[j], false, true);
			else newEntry = new Option(the_options[i],the_options[j], false, false);
			if (exchangelayer == 'ypregisterbranchbox') {
			    document.nt_ypregister_form.f_rubric.options[place] = newEntry;
			} else {
			    document.form_submit_yellow.f_rubric.options[place] = newEntry;
			}

		    }

		}
	    } else if (exchangelayer == 'commentdc') {
		document.commentbox_formular.gb_dc_value.value = req.responseText;
	    } else if (exchangelayer == 'commentname') {
		document.commentbox_formular.visitorname.value = req.responseText;
	    } else if (exchangelayer == 'commentcheckuser') {

		r = req.responseText.split("\n");
		user_id = r[0];

		if (user_id == '') {
		    document.getElementById('comment_entrybox').style.display = "none";
		    document.getElementById('comment_loginbox').style.display = "";
		} else {
		    var myaction = document.commentbox_formular.action;
		    if (myaction.search(/-commentprocess/) == -1) document.commentbox_formular.action = myaction.replace(/,1,0.html/,"-commentprocess,1,0.html");
		    document.getElementById('comment_loginbox').style.display = "none";
		    document.getElementById('comment_entrybox').style.display = "";
		    document.commentbox_formular.gb_dc_value.value = r[2];
		    document.commentbox_formular.visitorname.value = r[1];
		}
	    }

	} else {
	    //alert( "Fehler! " + req.status + " - " + req.statusText );
	}

    }

    return true;

}

var exchangelayer = '';

