var MyCareer_ad;
var distributionScript = servicesHost + "a1.js";

function DhtmlLoadScript(id, url) {
	var h = document.getElementsByTagName("head");
	if (document.getElementById(id) != null) {
		h[0].removeChild(document.getElementById(id));
	}
	var e = document.createElement("script");
	e.id = id;
	e.src = url;
	e.type = "text/javascript";
	h[0].appendChild(e);
}

function selectRadio(inp,sel,offsel1,offsel2) {
	sel.disabled=!inp.checked;
	if (offsel1 != null) offsel1.disabled=inp.checked;
	if (offsel2 != null) offsel2.disabled=inp.checked;
}

function copyToClipboard(x) {	
	if (!document.all) return;
	var r = x.createTextRange();
	r.execCommand("Copy");
}

function showHide(x) {
	var x = $(x);
	if (x.className == "hidden") x.className = ""; else x.className = "hidden";
}

function jsonQuoteString(s) {
	return "\"" + s.replace(/\\/g, "\\\\").replace(/"/g, "\\\"") + "\"";
}

function createJsTextArea(id) {
	var textArea = "<textarea onfocus=\"this.select();\" id=\"" + id + "\" class=\"js\" readonly=\"true\"><" + "/textarea>";
	if (document.all) {
		textArea += "<button onclick=\"copyToClipboard($('" + id + "'));return false;\">Copy to clipboard<" + "/button>";
	}
	else {
		textArea += "<div id=\"js_help\">Select all of the javascript above and copy and paste into your web page.<" + "/div>";
	}
	
	$(id+"_area").innerHTML = textArea;
}

function createScript(showAdCall) {
	return "<s" + "cript type=\"text/javascript\"><" + "!--\n" + showAdCall + "\n/" + "/--" + "><" + "/scr" + "ipt>\n<s" + "cript type=\"text/javascript\" src=\"" + distributionScript + "\">\n<" + "/scr" + "ipt>\n";
}

function sb_setup_advanced() {
	var style = $F("sb_option_keywords_sectors") || $F("sb_option_keywords_locations") || $F("sb_option_sectors_locations");
	switch (style) {
	case "sectors,locations":
		$("sb_default_keywords").disabled = true;
		sb_default_sectors.disabled = false;
		sb_default_locations.disabled = false;
		break;

	default:
		$("sb_default_keywords").disabled = false;
		sb_default_sectors.disabled = false;
		sb_default_locations.disabled = false;
		break;
	}

	sb_configurator();
}

function sb_setup() {
	sb_setup_advanced();
}

function sb_configurator() {
	var width = $F("sb_adunit").split("x")[0];
	var height = $F("sb_adunit").split("x")[1];
	var style = $F("sb_option_keywords_sectors") || $F("sb_option_keywords_locations") || $F("sb_option_sectors_locations");
	var default_keywords = $F("sb_default_keywords");
	var default_sectors = $F(sb_default_sectors)
	var default_locations = $F(sb_default_locations);
	var ag = null;
	if ($(check1) != null) ag = $F(check1) || null;

	var showAdCall = "MyCareer_ad = {\"style\": \"search\", \"width\": "+width+", \"height\": "+height+", \"show\": \""+style+"\"";
	MyCareer_ad = {"style": "search", "width": width, "height": height, "show": style, "target": "_top", "el": "sb_preview", "disabled": "disabled"};

	if (default_keywords != null && default_keywords.length > 0) {
		showAdCall += ", \"keyword\": " + jsonQuoteString(default_keywords);
		MyCareer_ad["keyword"] = default_keywords;
	}
	
	if (default_sectors != null) {
		if (!isNaN(parseInt(default_sectors)) && parseInt(default_sectors) > 0) {
			showAdCall += ", \"sector\": " + default_sectors;
			MyCareer_ad["sector"] = default_sectors;
		}
	}
	
	if (default_locations != null) {
		if (!isNaN(parseInt(default_locations)) && parseInt(default_locations) > 0) {
			showAdCall += ", \"location\": " + "\"" +default_locations + "\"";
			MyCareer_ad["location"] = default_locations;
		}
	}

	if (ag != null) {
		showAdCall += ", \"ag\": " + ag;
		MyCareer_ad["ag"] = ag;
	}
	showAdCall += "};";

	$("sb_js").value = createScript(showAdCall);

	DhtmlLoadScript("a1", distributionScript);
}

function br_setup() {
    if ($F("br_option_sectors")) {
        selectRadio($("br_option_sectors"), br_locations, br_sectors_locations, br_sectors_subsectors);
    }
    else if ($F("br_option_locations")) {
        selectRadio($("br_option_locations"), br_sectors_locations, br_locations, br_sectors_subsectors);
    }
	else if ($F("br_option_subsectors")) {
        selectRadio($("br_option_subsectors"),  br_sectors_subsectors, br_sectors_locations, br_locations );	
    }    
	
	br_configurator(); 
}

function br_configurator() {
	var showAdCall;
	var ag = null;
	var width = $F("br_adunit").split("x")[0];
	var height = $F("br_adunit").split("x")[1];
	var location = $F(br_locations);
	var sector_for_location = $F(br_sectors_locations);
	var sector_for_subsector = $F(br_sectors_subsectors);
	var title = $F("br_title");

	if ($(check2) != null) ag = $F(check2) || null;

	MyCareer_ad = {"style": "browse", "width": width, "height": height, "el": "br_preview", "disabled": "disabled"};
	showAdCall = "MyCareer_ad = {\"style\": \"browse\", \"width\": "+width+", \"height\": "+height;

	if ($F("br_option_sectors")) {
		showAdCall += ", \"show\": \"sectors\", \"location\": " + "\"" + location + "\"";
		MyCareer_ad["location"] = location;
		MyCareer_ad["show"] = "sectors";
	}
	if ($F("br_option_locations")) {
		showAdCall += ", \"show\": \"locations\", \"sector\": " + sector_for_location;
		MyCareer_ad["sector"] = sector_for_location;
		MyCareer_ad["show"] = "locations";
	}
	if ($F("br_option_subsectors")) {
		showAdCall += ", \"show\": \"subsectors\", \"sector\": " + sector_for_subsector;
		MyCareer_ad["sector"] = sector_for_subsector;
		MyCareer_ad["show"] = "subsectors";
	}

	if (ag != null) {
		showAdCall += ", \"ag\": " + ag;
		MyCareer_ad["ag"] = ag;
	}
	if (title.length > 0) {
		showAdCall += ", \"title\": " + jsonQuoteString(title);
		MyCareer_ad["title"] = title;
	}

	showAdCall += "};";

	$("br_js").value = createScript(showAdCall);

	DhtmlLoadScript("a1", distributionScript);
}

function jr_setup() {
	jr_configurator();
}

function jr_configurator() {
	var width = $F("jr_adunit").split("x")[0];
	var height = $F("jr_adunit").split("x")[1];
	var location = parseInt($F(jr_locations));
	var sector = parseInt($F(jr_sectors));
	var subsector = parseInt($F(jr_subsectors));
	var title = $F("jr_title");
	var keyword = $F("jr_keyword");
	var ag = null;
	if (check3 != null) ag = $F(check3) || null;
	
	var showAdCall = "MyCareer_ad = {\"style\": \"jobroll\", \"width\": "+width+", \"height\": "+height;
	MyCareer_ad = {"style": "jobroll", "width": width, "height": height, "el": "jr_preview", "disabled": "disabled"};
	if (!isNaN(location)) {
		showAdCall += ", \"location\": " + "\"" +location + "\"" ;
		MyCareer_ad["location"] = location;
	}
	if (!isNaN(sector)) {
		showAdCall += ", \"sector\": "+sector;
		MyCareer_ad["sector"] = sector;
	}
	if (!isNaN(subsector)) {
		showAdCall += ", \"subsector\": "+subsector;
		MyCareer_ad["subsector"] = subsector;
	}
	if (keyword.length > 0) {
		showAdCall += ", \"keyword\": " + jsonQuoteString(keyword);
		MyCareer_ad["keyword"] = keyword;
	}
	if (ag != null) {
		showAdCall += ", \"ag\": " + ag;
		MyCareer_ad["ag"] = ag;
	}
	if (title.length > 0) {
		showAdCall += ", \"title\": " + jsonQuoteString(title);
		MyCareer_ad["title"] = title;
	}
	showAdCall += "};";

	$("jr_js").value = createScript(showAdCall);

	DhtmlLoadScript("a1", distributionScript);
}

