var validation_flg=false;

function appChange(){
    document.preview.submit();
}

function makeURL(){
    script = "http://contentsrss.jp/SidebarTool/SidebarTool.php?app=@app&cstl=@cstl&charset=@charset&sc=@sc&amazonID=@amazonID";
    script = script.replace(/@app/,escape(document.preview.app.value));
    script = script.replace(/@cstl/,escape(document.preview.constellation.value));
    script = script.replace(/@charset/,escape(document.preview.char_code.value));
    script = script.replace(/@amazonID/,escape(document.preview.affiliate_id.value));
    script = script.replace(/@sc/,escape(document.preview.skin.value));

    return script;
}

function updatePreview(script){
    /*	var pw = document.getElementById("preview_window");
	if(pw.childNodes[0] != undefined)
	pw.removeChild(pw.childNodes[0]);

	var scr = document.createElement('script');
	scr.src = "/sandbox/SidebarTool/SidebarTool.php?cstl=capricorn&charset=EUC&sc=sample1&amazonID";
	pw.appendChild(scr);
    */
    pw = document.getElementById("prev_window");
    pw.setAttribute('src',script + "&ishtml=1");
    //.location.href=script + "&ishtml=1"
}

function disagree(){
    document.preview.script.value="";
    validation_flg=false;
    document.getElementById("button1").style.display="none";
    document.getElementById("button2").style.display="block";
    document.getElementById("button3").style.display="none";
    document.getElementById("button4").style.display="block";

}

function agree(){
    validation_flg=true;
    document.getElementById("button1").style.display="block";
    document.getElementById("button2").style.display="none";

    document.getElementById("button3").style.display="block";
    document.getElementById("button4").style.display="none";

    conditionChange();

}

function updateView(){
    btn = document.getElementById("widget_area");
    sa = document.getElementById("script_area");
    btn_cocolog = document.getElementById("cocolog_area");
	

    sa.style.display = "none";
    btn.style.display = "none";
    btn_cocolog.style.display = "none";

    if(document.preview.char_code.options[document.preview.char_code.selectedIndex].text == "TypePad"){
	btn.style.display = "block";

	
    }else if(document.preview.char_code.options[document.preview.char_code.selectedIndex].text == "¥³¥³¥í¥°"){
	btn_cocolog.style.display = "block";

    }else{
	sa.style.display = "block";

	
    }

}

function updateWidgetDesc(){
    document.widget_form.service_name.value = "ContentsRSS";
	
    document.widget_form.long_name.value = "ContentsRSS-"
	+ document.preview.app.value + "-"
	+ document.preview.constellation.value + "-"
	+ document.preview.skin.value;

    document.widget_form.short_name.value = "CRSS"
	+ document.preview.app.value
	+ document.preview.constellation.value
	+ document.preview.skin.value;
	
    document.cocolog_form.long_name.value = "ContentsRSS-"
	+ document.preview.app.value + "-"
	+ document.preview.constellation.value + "-"
	+ document.preview.skin.value;

    document.cocolog_form.short_name.value = "CRSS"
	+ document.preview.app.value
	+ document.preview.constellation.value
	+ document.preview.skin.value;

}

function conditionChange(){
    script = makeURL();
    updatePreview(script);
    updateWidgetDesc();
    updateView();
    if(!validation_flg){
	document.preview.script.value="";
	return;
    }
    document.widget_form.content.value="<script src=\"" + script
	+ "\"></script>";

    document.cocolog_form.content.value="<script src=\"" + script
	+ "\"></script>";

    document.preview.script.value="<script src=\"" + script
	+ "\"></script>";
}
