var baseserverurl="http://www.projectthingy.com";
var loadimage="<img src=http://projectthingy.images.s3.amazonaws.com/loading.gif>";
function setHTML(divname, h) {
	if (!$(divname)) return;
	$(divname).innerHTML=h;
}
function d(s) { s=unescape(s); return s.replace(/\+/g, " "); }
function doBRs(s) { s=s.replace(/\n/g, ""); return s.replace(/<br>/g, "\n"); }
function $(id) { return document.getElementById(id); }
function cd() {	return document.createElement("div"); }
function n(s) { s=d(s); return s.replace(/ /g, "_"); }
function empty(d) {
	if (!d) return;
	while(d.hasChildNodes()) d.removeChild(d.firstChild);
}
var JSC_NEWWIDGET=100;
var currjson;
function handleJSON(command,json) {
	currjson=json;

	if (command==JSC_NEWWIDGET) {
		if (json.s=="ok") {
			var h="<span class=bold>Cool! Here's the code for your ProjectThingy.</span><br>Click and copy this html code to a web page, upload it to your site at <i>"+currdomain+"</i> and try it out: &nbsp;<a href=\"#\" onclick=\"ddrivetip(event, \'When you put this html code on a web page, upload it to your site and try it out, you\\\'ll see a login form that you, your clients and partners can use to login to your ProjectThingy.<br><br><b>Your code will only work on web pages at <i>"+currdomain+"</i></b>. That\\\'s because you wouldn\\\'t want other people to be able to grab your widget and use it on their site - it\\\'s your ProjectThingy!\','', 350);\" class=getit>(more info)</a><br>";
			h+="<textarea style='border-color:#999999; border-width:1px; border-style:solid; background:#ffffff; width:920px; margin-top:3px; margin-bottom:10px; height:100px' onclick='this.select()'>"; 

			h+="<script src=\"http://projectthingy.resources.s3.amazonaws.com/projectthingy.js\" type=\"text/javascript\" language=\"Javascript\"></script>\n";
h+="<iframe id=\"pt_uploadtarget\" name=\"pt_uploadtarget\" src=\"\" style=\"width:0px;height:0px;border:0\"></iframe>\n";
h+="<span id='pt_dataarea0'></span><span id='pt_dataarea1'></span><span id='pt_dataarea2'></span><span id='pt_dataarea3'></span><span id='pt_dataarea4'></span>\n";
h+="<div id='pt_apparea' style='text-align:left;'><p><img src=http://projectthingy.images.s3.amazonaws.com/loading.gif> &nbsp; <a href=http://www.projectthingy.com>Loading ...</a></p></div>\n";
h+="<script type=\"text/javascript\" language=\"Javascript\">\n";
//h+="var baseserverurl='http://s1.projectthingy.com';\n";
h+="var sitecode=\""+json.c+"\";\n";
h+="projectThingy();\n";
h+="</script>\n";

			h+="</textarea>";
			h+="<br><br><span class=bold>Next steps:</span><br>After you've put your ProjectThingy on your site, login with the username and password you just entered. You can then start your first project, add project members and share files.\n";

			h+="<br><br><span class=bold>Don't feel like putting a ProjectThingy on your site right now?</span><br><a href=\"http://www.projecthingy.com/projectthingy="+json.c+"\">Go here for your personal ProjectThingy</a> at projectthingy.com. You can always come back later and get the html code for your site - just use the same domain, username and password as you did now.\n";


			h+="<br><br><br><span class=bold>Other options:</span><ul><li>Want to start over? <a href=/>Click here</a><li>Not quite sure what to do? <a href=howitworks.html>Watch the video</a> - It'll show you how it works</li><li>Contact us at +1 415-670-9090 or info<span style=\"display:none\">!removethis!</span>@<span style=\"display:none\">!removethis!</span>projectthingy.com</span>, we'd love to help out!</li></ul>";
			
			h+="<iframe src=/awc.html style=\"width:0px;height:0px;border:0\"></iframe>";
					
			setHTML("maintable", "<table width=100%><tr valign=top><td width=31>&nbsp;</td><td>"+h+"</td><td width=35 style='background:url(http://49video.resources.s3.amazonaws.com/images/uploadthingy_rightline1.gif) repeat-y;'>&nbsp;</td></tr></table>");
		}
		else if (json.s=="wrongpassword") setHTML("getwidget", "Ooops. This domain has already been registered and that was not the correct password. <a href=# onclick='window.location.reload();'>Please click here to try again.</a>");
		else setHTML("getwidget", "Ooops. There was an error. <a href=# onclick='window.location.reload();'>Please click here to try again.</a>");
	}
	else setHTML("getwidget", "Ooops. There was an error. <a href=# onclick='window.location.reload();'>Please click here to try again.</a>");
}
var commandcnt=0;
var showwaitsave;
function sendCommand(serverurl,c,showwait) {
	if (showwait) {
		showwaitsave=$(showwait).innerHTML;
		setHTML(showwait,"<table border=0><tr valign=center><td>loading...</td><td>"+ loadimage+"</td></tr></table>");
	}
	var s=document.createElement("script");
	s.src=serverurl+c+"&cnt="+commandcnt;

	empty($("dataarea"));
	$("dataarea").appendChild(s);
	commandcnt++;
}
var currdomain;
function getNewWidget() {
	var d=$('domain').value;
//alert("getWidget "+d+" "+d.length);
	var m=null;
	d=d.toLowerCase();
	d=d.replace(/^\s+|\s+$/g,"");
	if (d.indexOf("http://")==0) d=d.substring(7);
	if (d.indexOf("www.")==0) d=d.substring(4);
	if (d.indexOf("/")>0) d=d.substring(0, d.indexOf("/"));

	if (d.length<5 || d.lastIndexOf(".")<2 || d.lastIndexOf(".")>d.length-3 || d.indexOf(" ")!=-1) m="Ooops. That is not a valid domain.\nDomain example: mysite.com.\n\nWe need to know which domain your ProjectThingy will live on so nobody else can use it - except you on your site!";
	else if (d.indexOf("/")!=-1) m="Ooops. That is not a valid domain - you can't have a '/' in the domain.\nDomain example: mysite.com";
	else if (d.indexOf("@")!=-1) m="Ooops. That is not a valid domain - you can't have a '@' in the domain.\nDomain example: mysite.com";
	else if ($('username').value.length<3) m="Ooops. Your username must be at least 3 characters long.";
	else if ($('password').value.length<5) m="Ooops. Your password must be at least 5 characters long.";
	else if ($('password').value!=$('confirmpassword').value) m="Ooops. You did not confirm your password correctly.";
	else if (!$("tos").checked) m="Ooops. You must agree to the Terms of Service.";

	if (m!=null) alert(m);
	else {
		sendCommand(baseserverurl,"/widget?d="+escape(d)+"&un="+escape($('username').value)+"&pw="+escape($('password').value)+"&cpw="+escape($('confirmpassword').value),"getwidget");
		currdomain=d;
	}
}
function expandspan(what) {
//alert("expandSpan "+$(what).style.display);
	if ($(what).style.display=="inline") $(what).style.display="none";
	else $(what).style.display="inline";
}

