function getPageTitle(){
    if(typeof(schmapplet)!="undefined" && schmapplet.uid ){
     	var pagetitle = 'schmap '+schmapplet.uid+' widget';
    }else{
        var pagetitle = pageTitle;
        }
    return pagetitle;   
}

function doSubmit() {
    document.typepadform.submit();
}

function installToTypepad(){
    if($("iframecode"))
    	$("typepad_content").value = $("iframecode").value;
    else 
	    $("typepad_content").value = $("code").value;
    setTimeout(doSubmit,0);
    return true;
}

function installToBlogger(actionUrl, isIE6){
    var pagetitle = getPageTitle();
	if($("iframecode"))
    	var blogUrl = escape($("iframecode").value);
    else 
	var blogUrl = escape($("code").value);

    blogUrl = blogUrl.replace("/", "%2F");

    //if (isIE6) {
        window.open(actionUrl + '?blogurl=' + blogUrl + "&title=" + pagetitle,
                    '_blank',
                    'height=' + screen.height + ',width=' + screen.width + ',toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,location=yes,status=yes,z-look=yes');
//    } else { 
  //      obj.target = "_blank";
   //     top.location.href = actionUrl + "?blogurl=" + blogUrl + "&title=" + pagetitle;
 //   }
}

function installToLive(actionUrl, obj, isIE6){
    var pagetitle = getPageTitle();
    if($("iframecode")){
    	var liveUrl = escape($("iframecode").value);
}  else{ 
       var liveUrl = escape($("code").value);
}
    liveUrl = liveUrl.replace("/", "%2F");

    var host = document.location.hostname;
    var url = "http://" + host + '/plugins/live/download.php';
    xmlHttp = getXMLHttpRequest();
    xmlHttp.onreadystatechange = function() { handleLiveStateChange(actionUrl, obj, isIE6); }
    xmlHttp.open('POST', url, true);
    xmlHttp.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
    xmlHttp.send("src=" + liveUrl + "&title=" + pagetitle);
}

function handleLiveStateChange(actionUrl, obj, isIE6) {
    if (xmlHttp.readyState == 4)
    {
        if (xmlHttp.status == 200)
        {
            var downlink = xmlHttp.responseText;

            downlink = escape(downlink);
            downlink = downlink.replace("%2F", "/");
            downlink = downlink.replace("%3A", ":");
            // open('http://www.google.com/ig/adde?source=atgs&moduleurl=' + downlink, '_blank');
            logDebug(downlink);

            if (isIE6) {
                window.open(actionUrl + '?liveurl=' + escape(xmlHttp.responseText),
                    '_blank',
                    'height=' + screen.height + ',width=' + screen.width + ',toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,location=yes,status=yes,z-look=yes');
            } else {
                open(actionUrl + '?liveurl=' + escape(xmlHttp.responseText));
            }
        }
    }
}


var xmlHttp;

function installToWordpress(){
    installTo('/plugins/wordpress/download.php', handleWordpressStateChange);
}

function installToDrupal(){
    installTo('/plugins/drupal/download.php', handleDrupalStateChange);
}

function installToGoogle(){
    var pagetitle = getPageTitle();
    if($("iframecode"))
    	var code = $("iframecode").value;
    else 
       var code = $("code").value;
    var pattern = /src="(.*)"/;
    var cid = pattern.exec(code);
    var host = document.location.hostname;
    var url = "http://" + host + '/plugins/google/download.php';
    xmlHttp = getXMLHttpRequest();
    xmlHttp.onreadystatechange = handleGoogleStateChange;
    xmlHttp.open('POST', url, true);
    xmlHttp.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
    if($("widget_name"))
	var thisTitle = $("widget_name").innerHTML;
    else {
	var thisTitle = pagetitle;
    }
    
    xmlHttp.send("src=" + escape(cid[1]) + "&title=" + escape(thisTitle));

}

function installTo(download_php, handleStateChange){
    if($("iframecode"))
    	var code = escape($("iframecode").value);
    else
        var code = escape($("code").value);
    var host = document.location.hostname;
    var url = "http://" + host + download_php;
    xmlHttp = getXMLHttpRequest();
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.open('POST', download_php, true);
    xmlHttp.setRequestHeader("Content-Type" , "application/x-www-form-urlencoded");
    xmlHttp.send("src=" + code);
}

function handleGoogleStateChange() {
    if (xmlHttp.readyState == 4)
    {
        if (xmlHttp.status == 200)
        {
            var downlink = xmlHttp.responseText;
            downlink = escape(downlink);
            open('http://www.google.com/ig/adde?source=atgs&moduleurl=' + downlink, '_blank');
        }
    }
}

function handleDrupalStateChange() {
    if (xmlHttp.readyState == 4)
    {
        if (xmlHttp.status == 200)
        {

            var downlink = xmlHttp.responseText;
			if(!isIE6)
            downlink = escape(downlink);
            downlink = downlink.replace("/", "%2F");
            window.open('/plugins/drupal/download.html?downlink=' + downlink,
                        'PopupPage',
                        'height=450,width=500,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no,z-look=yes');
//            document.getElementById("download-frame").src = xmlHttp.responseText;
        }
    }
}

function handleWordpressStateChange() {
    if (xmlHttp.readyState == 4)
    {
        if (xmlHttp.status == 200)
        {
            var downlink = xmlHttp.responseText;
            downlink = escape(downlink);
            downlink = downlink.replace("/", "%2F");

            window.open('/plugins/wordpress/download.html?downlink=' + downlink,
                        'PopupPage',
                       'height=450,width=500,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no,z-look=yes');

//            document.getElementById("download-frame").src = xmlHttp.responseText;
        }
    }
}

