document.observe("dom:loaded", function() {	
	if (!Prototype.Browser.IE) {	
		$$(".overview").each(function(overview) {		
			var title = "Toggle Additional Informations";
			
			var div = new Element("div", { "class":"toogleBox-close" });
			
			var imageLink = new Element("a", { "href":"javascript:void(0);", "class":"imageLink", "title":title });
				imageLink.observe("click", function(event){
					var additional = imageLink.up(1).next(0).select(".additionalInfos").first();
					//additional.visible() ? additional.hide() : additional.show();
					Effect.toggle(additional, "blind");
				}.bind(this));
				// commented details link in my websites,my campaign,my profile
			//var textLink = new Element("a", { "href":"javascript:void(0);", "class":"textLink", "title":title }).update("details");
				textLink.observe("click", function(event){
					var additional = imageLink.up(1).next(0).select(".additionalInfos").first();
					//additional.visible() ? additional.hide() : additional.show();
					Effect.toggle(additional, "blind");
				}.bind(this));
			
			var description = new Element("span", { "class":"invisible" }).update(title);
			overview.insert({ top:div.insert({ top:imageLink.insert({ top:description }) }) });
			div.insert({ top:textLink });
		}.bind(this));
	}
	
	// Create Select/Clear Code Buttons
	if ($("invocationCode")) {
		var title = "Clear Selection";
		var link = new Element("a", { "href":"javascript:void(0);", "class":"btn_minus_grey_119", "title":title, "style":"display:inline-table;" }).update(title);
		
		link.observe("click", function(event){
			clearSelection();
		}.bind(this));		

		$("invocationCode").insert({ after:link });
		link.insert({ after:new Element("br") });
		
		var title = "Select Code";
		var link = new Element("a", { "href":"javascript:void(0);", "class":"btn_plus_grey_119", "title":title, "style":"display:inline-table;margin-right:10px;" }).update(title);
		
		link.observe("click", function(event){
			selectNode($("invocationCode"));
		}.bind(this));

		$("invocationCode").insert({ after:link });
	}
	
	// Create Print-Button in Balance
	if ($("tb_accountkeeping")) {		
		var title = "Print";
		var link = new Element("a", { "href":"javascript:void(0);", "class":"btn_print_grey_133", "title":title }).update(title);	
			link.observe("click", function(event){
				window.print();
			}.bind(this));
			
		$("tb_accountkeeping").insert({ after:new Element("div", { "style":"padding: 20px; width: 798px;" }).insert({ bottom:link }) });
		link.insert({ after:new Element("br", { "class":"clr" }) });
	}
	
	// Dynamic Error-Message, currently deactivated
	if (($("errorMessage") || $("noticeMessage")) && false) {
		if (!(false /*@cc_on || @_jscript_version < 5.7 @*/)) {
			kind = $("errorMessage") ? "error" : "notice";
			
			var message = $(kind + "Message");	
				message.removeClassName("message-" + kind);
				message.addClassName("message-dyn");
				message.update("<div><span>" + message.innerHTML + "</span></div>");
				var width = message.down(1).getWidth();
				message.down(0).insert({ top:new Element("img", { "src":"/img/icons/" + ( kind ) + ".png" }) }).setStyle({ width:(width + 40) + "px" });
			$(kind + "Message").remove();
			$$("body").first().insert({ bottom:message });
			
			Effect.Fade.delay(5, message);
		}
	}
	
	// Form Manipulations
	$$(".datetime").each(function(datetime) {
		var input = datetime.select("input").first();
		input.observe("focus", function(event){
			input.setStyle({ "color":"#4b4b4b" });
		}.bind(this));
	});
	
	$$(".tb_statistics .website input").each(function(input) {
		input.show();
	});
});

selectNode=function(node) {
	var selection, range, doc, win;

	if ((doc = node.ownerDocument) && (win = doc.defaultView) && typeof win.getSelection != 'undefined' && typeof doc.createRange != 'undefined' && (selection = window.getSelection()) && typeof selection.removeAllRanges != 'undefined') {
		range = doc.createRange();
		range.selectNode(node);
		selection.removeAllRanges();
		selection.addRange(range);
	} else if (document.body && typeof document.body.createTextRange != 'undefined' && (range = document.body.createTextRange())) {
		range.moveToElementText(node);
		range.select();
	}
}

clearSelection=function() {
	if (document.selection) {
		document.selection.empty();
	} else if (window.getSelection) {
		window.getSelection().removeAllRanges();
	}
}

hide_checkBoxCollectors=function(){
	$$(".checkboxCollector").each(function(checkboxCollector) {
		if (!checkboxCollector.up(0).previous(2).checked) {
			create_hider(checkboxCollector);
			checkboxCollector.setStyle({ "overflowY":"hidden" });
		}
	}.bind(this));

	init_radio_buttons();
}

init_radio_buttons=function(){
	if ($("targetingOptions")) {		
		$("targetingOptions").select(".toggleRadio").each(function(radio) {
			radio.observe("click", function(event){
				radio.nextSiblings().each(function(sibling) {
					if (sibling.hasClassName("field")) {
						
						if (!Prototype.Browser.IE) {						
						
							if (sibling.down(1).hasClassName("hider")) {
								if (radio.readAttribute("value") != 0) {
									sibling.down(1).remove();
									sibling.down(0).setStyle({ "overflowY":"scroll" });
								}
							} else {
								if (radio.readAttribute("value") != 1) {
									create_hider(sibling.down(0));
									sibling.down(0).setStyle({ "overflowY":"hidden" });
								}
							}
						
						}else{
							if (sibling.down(1)==document.getElementById('hider')) {
								if (radio.readAttribute("value") != 0) {
									sibling.down(1).remove();
									sibling.down(0).setStyle({ "overflowY":"scroll" });
								}
							} else {
								if (radio.readAttribute("value") != 1) {
									create_hider(sibling.down(0));
									sibling.down(0).setStyle({ "overflowY":"hidden" });
								}
							}
							
						}
						
					}
				}.bind(this));
			}.bind(this));
		}.bind(this));
	}
}

preload_area=function(where, spec_css){
	var hider = new Element("div", { "class":"hider", "id":"hider" });
	spec_css ? hider.setStyle({ "width":"99.9%", "height":($(where).getHeight() - 6) + "px", "top":"5px" }) : null;
	var preloader = new Element("img", { "src":"/img/content/preloader.gif", "alt":"Preloader" });
	$(where).insert({ top: hider.insert({ top: preloader }) });
}

create_hider=function(where) {
	if (!Prototype.Browser.IE) {
		var hider = new Element("div", { "class":"hider" }).setStyle({ "height":"3000px" });
	}else{
		var hider = new Element("div", { "id":"hider" }).setStyle({ "height":"3000px" });	
	}
	$(where).insert({ top: hider });
	
	/*var hider = new Element("div", { "class":"hider" }).setStyle({ "height":"3000px" });
	$(where).insert({ top: hider });*/
}

function div_toggle(whichLayer,layer2) {
    if (document.getElementById)
    {
        // this is the way the standards work
        var style2 = document.getElementById(whichLayer).style;
        style2.display = "block";
        var style3 = document.getElementById(layer2).style;
        style3.display = "none";
       
    }
    else if (document.all)
    {
        // this is the way old msie versions work
        var style2 = document.all[whichLayer].style;
        style2.display = "block";
        var style3 = document.getElementById(layer2).style;
        style3.display = "none";
    }
    else if (document.layers)
    {
        // this is the way nn4 works
        var style2 = document.layers[whichLayer].style;
        style2.display = "block";
        var style3 = document.getElementById(layer2).style;
        style3.display = "none";
    }
    
}

function shiftOpacity(id, millisec) { 
    //if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById('info_shown').value == "yes"){
		return true;
	}
	else{
		document.getElementById('info_shown').value = "yes";
	    if(document.getElementById(id).style.opacity == 0) {
	        opacity(id, 0, 100, millisec);
	    } else {
	        opacity(id, 100, 0, millisec);
	    }
	    return false;
	}
} 
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 50);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
} 

//function to change the date based on the dropdown
function statisticsChangeDate() {
	var val = document.getElementById('StatisticRange').value;
	if((val != '')  && (val != null)) { 
		var date_arr = val.split('::');
		var start_date = date_arr[0];
		var end_date = date_arr[1];
		document.getElementById('StatisticBegin').value =  start_date;
		document.getElementById('StatisticEnd').value =  end_date;
	}
}

function conditionStatisticRange(){
	var val = document.getElementById('StatisticRange').value;
	if((val != '')  && (val != null)) { 
		document.getElementById('StatisticBegin').style.color = '#A3A2A2';
		document.getElementById('StatisticEnd').style.color = '#A3A2A2';
		return true;
	} else {
		document.getElementById('StatisticBegin').style.color = '#000000';
		document.getElementById('StatisticEnd').style.color = '#000000';
		return false;
	}
}

//function to display the confirmation box on edit profile
function confirm_submit(){
	input_box=confirm("Your changes of e-mail address, user name or password are applied to your cross.tv and cross+tribution account, as well. ");

	if (input_box==true) { 
	    return true;
	}else{
	    return false;
	}
}

function emailChange_confirm (){
	input_box=confirm("We have changed your email address as per the crosstv details. Do you wish to proceed?  ");

	if (input_box==true) { 
	    return true;
	}else{
	    alert('The e-mail address stored in your cross.tv account (cross.tv e-mail address) does not match the e-mail address you entered on this form. Please use a different user name or change your e-mail address for this account on cross.tv and submit this form one more time.');
	    return false;
	}
	
}

function setFocus(){
	if(document.getElementById('UserUsername')){
		document.getElementById('UserUsername').focus();
	}
}

function displaycheckbox(x,y){
	var check_obj = "CampaignContinent"+ y;
	var check_objs="Continent"+ y;
	if(document.getElementById(check_obj) && document.getElementById(check_obj).checked==true){
		document.getElementById(x).style.display = "block";
		document.getElementById("img_"+y).src = "/img/expanded.gif";
	}else if(document.getElementById(check_objs) && document.getElementById(check_objs).checked==true){
		document.getElementById(x).style.display = "block";
		document.getElementById("img_"+y).src = "/img/expanded.gif";
	}else{
		document.getElementById(x).style.display = "none";
		document.getElementById("img_"+y).src = "/img/collapsed.gif";
	}	
	
}
var vals=0;
function displaycheckboxArrow(x,y){
	var check_obj = "CampaignContinent"+ y;
	var check_objs="Continent"+ y;
	
	if(vals=='1'){
		document.getElementById(x).style.display = "none";
		if(document.getElementById(check_obj) && document.getElementById(check_obj).checked==true){
			document.getElementById(check_obj).checked=false;
		}
		document.getElementById("img_"+y).src = "/img/collapsed.gif";
		vals=0;
	}else{
			document.getElementById(x).style.display = "block";
			if(document.getElementById(check_obj)  && document.getElementById(check_obj).checked!=true){
				document.getElementById(check_obj).checked=true;
			}
			document.getElementById("img_"+y).src = "/img/expanded.gif";
			vals=1;
	}	
	
}

// function to limit the characters in the text area of cross.ads wizard
function textCounter(maxlimit) {	
	if (document.getElementById('CrossadLine1').value.length > maxlimit) // if too long...trim it!
		document.getElementById('CrossadLine1').value =document.getElementById('CrossadLine1').value.substring(0, maxlimit);
}
