// Get the HTTP Object (this is for implementation of AJAX)

function getHTTPObject(){

	if (window.ActiveXObject)        

		return new ActiveXObject("Microsoft.XMLHTTP");   

	else if (window.XMLHttpRequest)        

		return new XMLHttpRequest();   

	else {      

		alert("Your browser does not support AJAX.");      

		return null;   

	}

}



function copy(id) {

  if(arguments.length>0) {

  	cpy = document.getElementById(id);

  } else {

  	cpy = document.getElementById('copy_link');

  }

  

  cpy.innerHTML='copied'; 

  cpy.style.color='gray'; 

  cpy.style.textDecoration='none'; 

  cpy.style.cursor='text'; 

  

  inElement = document.getElementById('url_hidden');

  if (inElement.createTextRange) {

    var range = inElement.createTextRange();

    if (range && BodyLoaded==1)

      range.execCommand('Copy');

  } else {

    var flashcopier = 'flashcopier';

    if(!document.getElementById(flashcopier)) {

      var divholder = document.createElement('div');

      divholder.id = flashcopier;

      document.body.appendChild(divholder);

    }

    document.getElementById(flashcopier).innerHTML = '';

    var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';

    document.getElementById(flashcopier).innerHTML = divinfo;

  }

}



function ClipBoard() {

	//dashboard_qpc.innerText = copytext.innerText;

	//alert("test");

	alert(document.getElementById('url_hidden').value);

	Copied = document.getElementById('url_hidden').createTextRange();

	Copied.execCommand("Copy");

}



function userExists(twitterid, callbackFunction) {

	httpObject = getHTTPObject();   



	if (httpObject != null) {

		httpObject.open("GET", "php/userexists.php?username="+twitterid);

		httpObject.send(null);

		

        httpObject.onreadystatechange = function() {

			if (httpObject.readyState == 4){

				if(httpObject.responseText=='no') {

					callbackFunction(); //submit to aweber

				} else {

					//just proceed

				}

				//document.getElementById('main_confirmation_form_newurl').innerHTML

					//= httpObject.responseText;

		    }

		};

	}

}



function twitterAuthenticate(url, twitterid, pword, callbackFunction) {

	httpObject = getHTTPObject();   

	// Use AJAX to Create a Short URL

	//alert(twitterid + pword);

	if (httpObject != null) {

		//alert(pword);

		httpObject.open("GET", "php/twitter_authenticate.php?username="+twitterid+"&password="+pword);

		httpObject.send(null);

		

        httpObject.onreadystatechange = function() {

			if (httpObject.readyState == 4){

				if(httpObject.responseText=='success') {

					//shortenURL(url,api,twitterid); //Go ahead and shorten the URL now.

					callbackFunction();

				} else {

					alert("Sorry, we were unable to authenticate your twitter account");

					//$("#main_url_wrapper").toggle("slide", { direction: "left" }, 300);

					$("#main_login_wrapper").toggle("slide", { direction: "right" }, 300);

					$("#main_confirmation_wrapper").toggle("slide", { direction: "left" }, 300);

				}

				//document.getElementById('main_confirmation_form_newurl').innerHTML

					//= httpObject.responseText;

		    }

		};

	}

}



function justLogIn(twitterid,pword) {

	twitterAuthenticate("",twitterid,pword, function() {location.href="stats.php";});

}



function shortenURL(url, twitterid, callback){

	

	httpObject = getHTTPObject();    

	//alert(url + api + twitterid);

	// Use AJAX to Create a Short URL

	if (httpObject != null) {

		httpObject.open("GET", "php/api.php?url="+url+"&twitterid="+twitterid);

		httpObject.send(null);

		

        httpObject.onreadystatechange = function() {

			if (httpObject.readyState == 4){

				callback();

		    }

		};

	}

}





function del(username,rw,longurl,ds) {

	$("#"+rw).fadeOut(200);

	//open_row(rw);

	$("#stats_display_"+rw).hide();

	$("#row_display_" + rw).hide();

	//$("#stats_display_"+rw).hide();

	

	$("#delete_confirmation").html("Tracking for <span style='color:green;'>http://twitclicks.com/"+rw+"</span> has ended.  <a href=\"javascript:void(0);\" onclick=\"undo_del('"+username+"','"+rw+"','"+longurl+"','"+ds+"');\">Undo</a>");

	

	$("#delete_confirmation").slideDown();

	setTimeout(function() {$("#delete_confirmation").slideUp(); },(60*1000));

	

	

	httpObject = getHTTPObject();    

	

	if (httpObject != null) {

		httpObject.open("GET", "php/deleteurl.php?tcid="+rw);

		httpObject.send(null);

	}

	

}



function undo_del(username,tcid,longurl,ds) {

	httpObject = getHTTPObject();    

	if (httpObject != null) {

		httpObject.open("GET", "php/undodelete.php?username="+username+"&tcid="+tcid+"&longurl="+longurl+"&datestarted="+ds);

		httpObject.send(null);

		$("#delete_confirmation").hide();

		

		//$("#row_display_"+tcid).hide();

		//$("#row_display_"+tcid).css('position','absolute');

		$("#"+tcid).show();

		$("#"+tcid).effect("highlight", {}, 2000);

		

	}

}



function open_row(tcid,longurl,tid) {



	if (document.getElementById(tcid).style.display == "") { //if the row is closed

		

		if (document.getElementById("stats_display_" + tcid).style.display == "none") {

			$("#row_display_" + tcid).show();

			$("#stats_display_" + tcid).slideDown('fast'); //slide it down

			

			document.getElementById('url_hidden').value = "http://twitclicks.com/" + tcid;

			

			httpObject = getHTTPObject();    

		

			if (httpObject != null) {

				httpObject.open("GET", "php/showstats.php?tcid=" + tcid + "&longurl=" + longurl + "&tid=" + tid);

				

				httpObject.send(null);

				httpObject.onreadystatechange = function(){

					if (httpObject.readyState == 4) {

						//alert(httpObject.responseText);

						//$("#stats_display_" + tcid).html(httpObject.responseText); //display the general stats in the container

						document.getElementById('stats_display_' + tcid).innerHTML=httpObject.responseText;

					}

				};

			}	

				

							

		}

		else {

			$("#row_display_" + tcid).hide();

			$("#stats_display_" + tcid).slideUp('fast');

		}

	}	



}



function pop_open_row(rid,dt) {



	if (document.getElementById('pop_'+rid).style.display == "") { //if the row is closed

		if (document.getElementById("pop_stats_display_" + rid).style.display == "none") {

			$("#pop_row_display_" + rid).show();

			$("#pop_stats_display_" + rid).slideDown('fast'); //slide it down

			

			httpObject = getHTTPObject();    

		

			if (httpObject != null) {

				httpObject.open("GET", "php/guessstats.php?rid=" + rid + "&dt=" + dt);

				

				httpObject.send(null);

				httpObject.onreadystatechange = function(){

					if (httpObject.readyState == 4) {

						$("#pop_stats_display_" + rid).html(httpObject.responseText); //display the general stats in the container

					}

				};

			}

				

							

		}

		else {

			$("#pop_row_display_" + rid).hide();

			$("#pop_stats_display_" + rid).slideUp('fast');

		}

	}	



}



function change_range(str) {

	$("#tbdy").html(""); //cou'd put loading txt here

	if(str=="All Time") {

		dr="all";

	} else if(str=="Last 30 Days") {

		dr="last30";

	} else if(str=="Last Week") {

		dr="lastweek";

	} else if(str=="Today") {

		dr="today";

	}

	

	httpObject = getHTTPObject();    

	if (httpObject != null) {

		httpObject.open("GET", "php/displaystats.php?daterange="+dr);

		httpObject.send(null);

		

        httpObject.onreadystatechange = function() {

			if (httpObject.readyState == 4){

				//alert(httpObject.responseText);

				$("#tbdy").html(httpObject.responseText);

		    }

		};

	}

	

}



function checkEnter(o,e,callback) {

	

		var k;

		if (!e) var e = window.event

		if (e.keyCode) k = e.keyCode;

		else if (e.which) k = e.which;

		if(k==13) {

			//alert("checking");

			callback();

		}

		//if(window.event.keyCode==13) 

	}

	

function prependTable(username,tcid,longurl) {	

	//date

	var d = new Date();

	if(d.getHours()>12) {hr = d.getHours() - 12; ampm="PM"} else {hr = d.getHours(); ampm="AM";}

	if(hr<10) hr = "0"+hr;

	

	mn = d.getMinutes();

	if(mn<10) mn = "0"+mn;

	

	sc = d.getSeconds();

	if(sc<10) sc = "0"+sc;

	

	mo = d.getMonth();

	if(mo<10) mo = "0"+mo;

	

	dom = d.getDate();

	if(dom<10) dom = "0"+dom;

	

	var dt = hr + ":" + mn + " "+ampm;

	var dbdt = d.getFullYear() + "-" + (mo+1) + "-" + dom + " " + d.getHours() + ":" + mn + ":" + sc;

	//alert(dbdt);

	//set table content

	var content = "";

	content += "<tr id=\""+tcid+"\" class=\"even\">\n";

	content += "    <td onclick=\"open_row('"+tcid+"');\"><img src=\"images/arw.png\"/></td>\n";

	content += "	<td onclick=\"open_row('"+tcid+"');\"><b>Today</b> "+dt+"</td>\n";

	content += "	<td onclick=\"open_row('"+tcid+"');\">0</td>\n"; 

	content += "	<td onclick=\"open_row('"+tcid+"');\">0.0%</td>\n";

	if(longurl.length>50) {longurl = longurl.substring(0,47)+"...";}

	content += "	<td onclick=\"open_row('"+tcid+"');\" title=\""+longurl+"\">"+longurl+" (http://twitclicks.com/"+tcid+")</td>\n";

	content += "	<td><img onclick=\"del('"+username+"','"+tcid+"','"+longurl+"','"+dbdt+"')\" src=\"images/dlt.gif\"/></td>\n";

	content += "</tr>\n";



	content += "<tr style='display:none;' id=\"row_display_"+tcid+"\">\n";

	content += "	<td colspan=\"6\">\n";

	content += "		<div class=\"stats_display\" id=\"stats_display_"+tcid+"\" align=\"center\" style=\"display:none; background-color:yellow;\">\n";	

	content += "			No Data to Show Yet...\n";

	content += "		</div>\n";

	content += "	</td>\n";

	content += "</tr>\n";

	

	$("tbody").prepend(content); 

	$("#"+tcid).effect("highlight", {}, 2000);

	

	

}



function showc(str,tcid) {

	height=600;

	width=837;

	inlineId="myOnPageContent";

	modal=true;

	

	drval = $(".date_range").val();

	if(drval=="All Time") {

		dr="all";

	} else if(drval=="Last 30 Days") {

		dr="last30";

	} else if(drval=="Last Week") {

		dr="lastweek";

	} else if(drval=="Today") {

		dr="today";

	}

	

	document.getElementById('showGen').alt="#TB_inline?height=" + height + "&width=" + width + "&inlineId=" + inlineId + "&modal=" + modal;

	document.getElementById('showGen').click();

	

	if (str != 'map') {

		document.getElementById('iframe_modal').src = "modal.php?type=" + str + "&tcid=" + tcid + "&daterange=" + dr;

	} else {

		document.getElementById('iframe_modal').src = "modal-map.php?tcid=" + tcid + "&daterange=" + dr;

	}





}



function showReg(name,tid,pass) {

	height=600;

	width=837;

	inlineId="myOnPageContent";

	modal=true;

	

	document.getElementById('showGen').alt="#TB_inline?height=" + height + "&width=" + width + "&inlineId=" + inlineId + "&modal=" + modal;

	document.getElementById('showGen').click();

	

	document.getElementById('iframe_modal').src = "modal-register.php?name=" + name + "&tid=" + tid + "&pass=" + pass;

	



}


function showPop(page) {
	alert(page);
	height=600;
	width=837;
	inlineId="myOnPageContent";
	modal=true;

	document.getElementById('showGen').alt="#TB_inline?height=" + height + "&width=" + width + "&inlineId=" + inlineId + "&modal=" + modal;
	document.getElementById('showGen').click();

	document.getElementById('iframe_modal').src = page;
		
}

function doRegister(tid,pass) {

	//alert('test');

	name = document.getElementById('name').value;

	email = document.getElementById('email').value;

	

	//invite_code = document.getElementById('invite_code').value;

	follow_twitclicks = document.getElementById('follow_twitclicks').value;

	follow_kylegraham = document.getElementById('follow_kylegraham').value;

	tweet_me = document.getElementsByName('tweet_me')[0].checked;

	

	tweet_txt = encodeURIComponent(document.getElementById('tweet_txt').value);

	

	//perform validation

	if (name == "" || email=="") {

		alert("Please Complete The Form");

	} else {

		//alert(pass);

		httpObject = getHTTPObject();    

		if (httpObject != null) {

			httpObject.open("GET", "php/doRegister.php?tid=" + tid + "&pass=" + pass + "&name=" + name + "&email=" + email + "&follow_twitclicks=" + follow_twitclicks + "&follow_kylegraham=" + follow_kylegraham + "&tweet_me=" + tweet_me + "&tweet_txt=" + tweet_txt);

			httpObject.send(null);

			

	        httpObject.onreadystatechange = function() {

				if (httpObject.readyState == 4){

					//alert(httpObject.responseText);	

					//parent.tb_remove();

					document.forms.frm.submit();

			    }

			};

		}

	}

	

}



function switchTab(tab) {

	//hide all tabls

	$(".tabcontent").hide();

	$("#pop_tech_content_"+tab).show();

	var cssObjOff = {

        "border": "none",

        "border-right": "1px solid #d6d6d6"

      }

	$("LI").css(cssObjOff);

	  

	var cssObj = {

        "border": "1px solid #d6d6d6",

        "border-right": "1px solid white",

        "background-color": "white"

      }

	$("#pop_technical_tabs_"+tab).css(cssObj);

}

