/**
 * Toggle the default text... userfriendly = yes
 */
function toggleDefaultVal(oInput,bClick){
	oSubmit = document.getElementById('free_check_submit');
	
	if (!bClick && oInput.value == ""){
		oInput.cleared = false;
		oInput.value = "Hey, enter a site!";
		jQuery('#example_domain').show();
	
	} else if(!oInput.cleared) {
		oInput.cleared = true;
		oInput.value = "";
		jQuery('#example_domain').hide();
	
	}//-if (oInput.cleared)
	
}//-function toggleDefaultVal

/**
 * Handles pseudo form submit
 */
function submitCheck(){
	//Ajax call is already running... exit
 	if(bRunning){return false;}
	
	//Get and validate the domain name
	sDomain = validateDomain();
	
	//No domain??? Something is wrong! Abort!!
	if(!sDomain){
		return false;
	}
	
	//Send user to upup w/ domain in uri
	window.location = "http://" + sUpupDomain + "/" + (sDomain);
}//-function submitCheck


/**
 * Do the check already, would you!
 */
function doCheck(bNew){
 	if(typeof bNew == "undefined") bNew = false;
 	
 	//New user submitted request?
 	if(bNew){
	 	//Ajax call is already running... exit
 		if(bRunning){return false;}
	 	
	 	//Reset array tracker
	 	iLocal = 0;
	 	
	 	//We're running
	 	bRunning = true;
	}//-if(bNew)
 	
 	//Get and validate the domain name
 	sDomain = validateDomain();
 	
 	if(!sDomain) return false;
 	
 	//Handle http on domain
	if("http" != sDomain.substr(0,4) && "ftp" != sDomain.substr(0,3)){
 		sDomain = "http://" + sDomain;
 	}
	 	
 	oAjax.Reset();
 	oAjax.SetVar("domain", sDomain);
 	oAjax.SetVar("proxy_url", "http://" + aLocations[iLocal].subdomain + "upup.com/API/Monitor.php");
 	oAjax.Method = "GET";
 	oAjax.RequestFile = "http://" + sUpupDomain + "/proxy.php";
 	oAjax.ResponseType = "json";
 	oAjax.onLoading = doCheck_onLoaded;
 	oAjax.onCompletion = doCheck_onCompleted;
 	oAjax.onFail = doCheck_onCompleted;
 	oAjax.RunAJAX();
		//oAjax.DebugAJAX();
 
}//-function doCheck
//---------------------------------------------------------------
	 function doCheck_onLoaded(){
	 	oSubmit = document.getElementById('free_check_submit');
	 	oSubmit.value = 'checking';
	 	
	 	oContainer = document.getElementById('response_container');
		oContainer.style.display = "block";
	 }//-function doCheck_onLoaded
	 
	 function doCheck_onCompleted(){
	 	switch (iLocal){
		 	case (aLocations.length - 1):
			 	oSubmit = document.getElementById('free_check_submit');
			 	oSubmit.value = 'Recheck';
			 	bRunning = false;
			 	//drop trhough
			 	
		 	default:
		 		//handle json response here to build new response_loc
		 		oCheck = oAjax.Response;
		 		
		 		//Correct Returned Time
		 		if(oCheck.direction == 0){ 
		 			sDisplayTime = "DOWN";
		 			oArrow = document.getElementById('status_arrow_' + iLocal + '_down');
		 		}else{
		 			sDisplayTime = oCheck.time + "ms";
		 			oArrow = document.getElementById('status_arrow_' + iLocal + '_up');
		 		}
		 		
		 		//set load time
		 		document.getElementById('time_' + iLocal).innerHTML = sDisplayTime;
		 		
		 		//set arrow
		 		oArrow.style.display = 'block';
		 		
		 		iLocal++;
		 		
		 		if(iLocal < aLocations.length){ 
		 			doCheck();
		 		}//-if(iLocal < aLocations.length)
	 	}//-switch (iLocal)
	 	
	 }//-function doCheck_onCompleted()
//---------------------------------------------------------------

 /**
  * Check if a valid domain has been entered
  */ 
function validateDomain(){ 	
 	//Get Domain
	var oDomain = document.getElementById('domain_field');
 	var sDomain = oDomain.value;
	
	//Check that user entered something
	if(sDomain == "Check if your site is upup!" || sDomain == "Hey, enter a site!" || sDomain == ""){
 		showError('Try typing something in the box first...');
 		
 		return false;
 	}
 	
 	iProtocol		= sDomain.indexOf("://");
 	iMalProtocol	= sDomain.indexOf(":/");
 	
 	if(iProtocol < 0 && iMalProtocol != iProtocol){
 		//Correct http:/test.com
 		sDomain			= sDomain.replace(":/", "://");
 		oDomain.value	= sDomain;
 		
 	}else if(iProtocol < 0 && iMalProtocol < 0){
 		//Add Protocol
 		sDomain			= "http://" + sDomain;
 		oDomain.value	= sDomain;
 	}
	
	return sDomain;
}//-function validateDomain()

/**
 * Show submission error messages
 */
function showError(sMessage){
	
	aLocations = getElementsByClassName('response_location',document.getElementById('response_container'));
	
	for(i=0, goTo = aLocations.length; i < goTo; i++){
		oLocation = aLocations[i];
		oLocation.style.display	= 'none';
	}

	oError = document.getElementById('err_msg');
	oError.innerHTML		= sMessage;
	oError.style.display	= 'block';
}//-function showError
 
/**
 * Get elements by classname, start with the parent node...
 */
function getElementsByClassName(classname, node) {
	if(!node) node = document.getElementsByTagName("body")[0];
	
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");

	for(var i=0,j=els.length; i<j; i++)
		if(re.test(els[i].className))a.push(els[i]);

	return a;
}//function getElementsByClassName(classname, node)
