var _GLOBAL = {};
_GLOBAL.transaction_key = null;
_GLOBAL.report_id = null;
_GLOBAL.domain_1 		= null;
_GLOBAL.keyword_1 		= null;
_GLOBAL.keyword_2 		= null;
_GLOBAL.keyword_3 		= null;
_GLOBAL.keyword_5 		= null;
_GLOBAL.captcha_code	= null;
_GLOBAL.load_text = '<img id="loading" src="/_IMAGES/ajax_loading_small.gif" /> Loading...';

//##########################################################################################

//--> Begin Function :: ValidateForm

	function ValidateForm(){
		
		if(document.$("domain_1").value == ""){
			alert("Please specify a url!!");
			return;
		}

		if(    document.$("keyword_1").value == "" 
			&& document.$("keyword_2").value == "" 
			&& document.$("keyword_3").value == "" 
			&& document.$("keyword_4").value == ""
			&& document.$("keyword_5").value == ""
		){
			alert("Please specify at least one keyword!");
			return;
		}
		
		if(document.$("captcha_code").value == ""){
			alert("Please enter a code");
			return;
		}
		
		var SubmitData = {};
		SubmitData["name_first"] 	= document.$("name_first").value;
		SubmitData["name_last"] 	= document.$("name_last").value;
		SubmitData["email"] 		= document.$("email").value;
		SubmitData["phone"] 		= document.$("phone").value;
		SubmitData["url"] 			= document.$("domain_1").value;
		SubmitData["comment"] 		= "keyword_1:"+ document.$("keyword_1").value +"\n";
		SubmitData["comment"] 		+= "keyword_2:"+ document.$("keyword_2").value +"\n";
		SubmitData["comment"] 		+= "keyword_3:"+ document.$("keyword_3").value +"\n";
		SubmitData["comment"] 		+= "keyword_4:"+ document.$("keyword_4").value +"\n";
		SubmitData["comment"] 		+= "keyword_5:"+ document.$("keyword_5").value +"\n";
		SubmitData["source_id"] 	= "112";
		SubmitLead(SubmitData);
		
		//set values
		_GLOBAL.domain_1 		= document.$("domain_1").value;
		_GLOBAL.keyword_1 		= document.$("keyword_1").value;
		_GLOBAL.keyword_2 		= document.$("keyword_2").value;
		_GLOBAL.keyword_3 		= document.$("keyword_3").value;
		_GLOBAL.keyword_4 		= document.$("keyword_4").value;
		_GLOBAL.keyword_5 		= document.$("keyword_5").value;
		_GLOBAL.captcha_code    = document.$("captcha_code").value;
		
		//set inverted values
		_GLOBAL[_GLOBAL.domain_1] 		= "domain_1";
		_GLOBAL[_GLOBAL.keyword_1]  	= "keyword_1";
		_GLOBAL[_GLOBAL.keyword_2]  	= "keyword_2";
		_GLOBAL[_GLOBAL.keyword_3] 		= "keyword_3";
		_GLOBAL[_GLOBAL.keyword_4] 		= "keyword_4";
		_GLOBAL[_GLOBAL.keyword_5] 		= "keyword_5";
		_GLOBAL[_GLOBAL.captcha_code] 	= "captcha_code";
		
		//sequence out queues
		AddToQueue("GetTransactionKey()");
		AddToQueue("GetReportID()");
		AddToQueue("GetReport()");
		
		//execute queue
		ExecuteQueue();
	}

//--> End Function :: ValidateForm

//##########################################################################################

//--> Begin Function :: GetReport

	function GetReport() {
		if(_GLOBAL.transaction_key != null && _GLOBAL.report_id != null){
			
			SetInnerHTMLById("company_name_content", _GLOBAL.company_name);
			SetInnerHTMLById("domain_1_name", _GLOBAL.domain_1);

			document.$('field_container').style.display = "";
			document.$('report_container').style.display = "";
			
			AddToQueue("ShowQuickLoad(true)");
			
			if(_GLOBAL.keyword_1 != ""){
				SetInnerHTMLById('keyword_1_name', _GLOBAL.keyword_1);
				SetInnerHTMLById('keyword_1_google_rank', _GLOBAL.load_text);
				SetInnerHTMLById('keyword_1_yahoo_rank', _GLOBAL.load_text);
				SetInnerHTMLById('keyword_1_msn_rank', _GLOBAL.load_text);
				
				AddToQueue("GetKeywordRanking('" + _GLOBAL.domain_1 + "','" + _GLOBAL.keyword_1 + "','" + _GLOBAL.report_id + "','" + _GLOBAL.transaction_key + "','keyword_1')");
			}
			
			if(_GLOBAL.keyword_2 != ""){
				SetInnerHTMLById('keyword_2_name', _GLOBAL.keyword_2);
				SetInnerHTMLById('keyword_2_google_rank', _GLOBAL.load_text);
				SetInnerHTMLById('keyword_2_yahoo_rank', _GLOBAL.load_text);
				SetInnerHTMLById('keyword_2_msn_rank', _GLOBAL.load_text);
				
				AddToQueue("GetKeywordRanking('" + _GLOBAL.domain_1 + "','" + _GLOBAL.keyword_2 + "','" + _GLOBAL.report_id + "','" + _GLOBAL.transaction_key + "','keyword_2')");
			}
			
			if(_GLOBAL.keyword_3 != ""){
				SetInnerHTMLById('keyword_3_name',_GLOBAL.keyword_3);
				SetInnerHTMLById('keyword_3_google_rank', _GLOBAL.load_text);
				SetInnerHTMLById('keyword_3_yahoo_rank', _GLOBAL.load_text);
				SetInnerHTMLById('keyword_3_msn_rank', _GLOBAL.load_text);
				
				AddToQueue("GetKeywordRanking('" + _GLOBAL.domain_1 + "','" + _GLOBAL.keyword_3 + "','" + _GLOBAL.report_id + "','" + _GLOBAL.transaction_key + "','keyword_3')");
			}
			
			if(_GLOBAL.keyword_4 != ""){
				SetInnerHTMLById('keyword_4_name', _GLOBAL.keyword_4);
				SetInnerHTMLById('keyword_4_google_rank', _GLOBAL.load_text);
				SetInnerHTMLById('keyword_4_yahoo_rank', _GLOBAL.load_text);
				SetInnerHTMLById('keyword_4_msn_rank', _GLOBAL.load_text);
				
				AddToQueue("GetKeywordRanking('" + _GLOBAL.domain_1 + "','" + _GLOBAL.keyword_4 + "','" + _GLOBAL.report_id + "','" + _GLOBAL.transaction_key + "','keyword_4')");
			}
			
			if(_GLOBAL.keyword_5 != ""){
				SetInnerHTMLById('keyword_5_name', _GLOBAL.keyword_5);
				SetInnerHTMLById('keyword_5_google_rank',  _GLOBAL.load_text);
				SetInnerHTMLById('keyword_5_yahoo_rank', _GLOBAL.load_text);
				SetInnerHTMLById('keyword_5_msn_rank',  _GLOBAL.load_text);
				
				AddToQueue("GetKeywordRanking('" + _GLOBAL.domain_1 + "','" + _GLOBAL.keyword_5 + "','" + _GLOBAL.report_id + "','" + _GLOBAL.transaction_key + "','keyword_5')");
			}
			
			AddToQueue("SetInnerHTMLById('download_link','<a href=\"#here\" onclick=\"ViewReport();\">Download Report</a>'); _GLOBAL.queue_continue = true;");
			
			AddToQueue("ShowQuickLoad(false)");			
			
			AddToQueue("ShadeScreen(true)");
			
			//release queue
			_GLOBAL.queue_continue = true;
			
			//execute queue
			ExecuteQueue();
			
		}else{
			RefreshCaptcha();
			document.$("captcha_code").value = "";					
			document.$("captcha_alert").innerHTML = "Incorrect Code.";	
		}
	}

//--> End Function :: GetReport

//##########################################################################################

//--> Begin Function :: GetKeywordRanking

	function GetKeywordRanking(domain, keyword, report_id, transaction_key, field){
		
		var MyAjax = new powerHTTP();
		
		MyAjax._current_domain = domain;
		MyAjax._current_keyword = keyword;
		
	//begin handling loading
		MyAjax.loading = function() {};
	//end handling loading
	
	//begin handling response
		MyAjax.complete = function(response) {
			if(MyAjax.was_successful()) {
				
				//eval into response
				var Response = eval('(' + response.text + ')');
				
				//get element id
				var keyword_id = _GLOBAL[this._current_keyword];
				
				//test for scraping
				if(Response.is_success == "true"){
					SetInnerHTMLById(keyword_id + "_name", this._current_keyword);
					SetInnerHTMLById(keyword_id + "_msn_rank", Response.msn_rank);
					SetInnerHTMLById(keyword_id + "_yahoo_rank", Response.yahoo_rank);
					SetInnerHTMLById(keyword_id + "_google_rank", Response.google_rank);
				}else{
					//error
				}
			}
			else {
				//error
			}
			
			//release queue
			_GLOBAL.queue_continue = true;
			
		};
	//end handling response

	//begin execute request
		var Data = new Array();
		Data.push("field=" + field);
		Data.push("domain=" + domain);
		Data.push("keyword=" + keyword);
		Data.push("report_id=" + report_id);
		Data.push("transaction_key=" + transaction_key);
		var myPostData = Data.join("&");
		MyAjax.post("/seo-tools/search-engine-ranking-report.html?r=get_keyword_ranking", myPostData);	
	//end execute request
	
	}
	
//--> End Function :: GetKeywordRanking

//##########################################################################################

//--> Begin Function :: GetReportID

	function GetReportID() {
		
		var MyAjax = new powerHTTP();
		
	//begin handling loading
		MyAjax.loading = function() {};
	//end handling loading
	
	//begin handling response
		MyAjax.complete = function(response) {
			if(MyAjax.was_successful()) {
				
				//eval into response
				var Response = eval('(' + response.text + ')');
				
				if(Response.is_success == "true"){				
					_GLOBAL.report_id = Response.report_id;
				}else{
					_GLOBAL.report_id = null;
				}
			}
			else {
				_GLOBAL.report_id = null;
			}
			
			//release queue
			_GLOBAL.queue_continue = true;
		};
	//end handling response

	//begin execute request	
		var Data = new Array();
		Data.push("domain_1=" 			+ _GLOBAL.domain_1);
		Data.push("keyword_1=" 			+ _GLOBAL.keyword_1);
		Data.push("keyword_2=" 			+ _GLOBAL.keyword_2);
		Data.push("keyword_3=" 			+ _GLOBAL.keyword_3);
		Data.push("keyword_4=" 			+ _GLOBAL.keyword_4);
		Data.push("keyword_5=" 			+ _GLOBAL.keyword_5);
		Data.push("transaction_key=" 	+ _GLOBAL.transaction_key);
		var myPostData = Data.join("&");
		MyAjax.post("/seo-tools/search-engine-ranking-report.html?r=get_report_id", myPostData);	
	//end execute request
	
	}
	
//--> End Function :: GetReport

//##########################################################################################

//--> Begin Function :: GetTransactionKey
	
	function GetTransactionKey() {
		
		var MyAjax = new powerHTTP();
		
	//begin handling loading
		MyAjax.loading = function() {};
	//end handling loading
	
	//begin handling response
		MyAjax.complete = function(response) {
			if(MyAjax.was_successful()) {
				
				//eval into response
				var Response = eval('(' + response.text + ')');
				
				//test for scraping
				if(Response.is_success == "false"){				
					_GLOBAL.transaction_key = null;
				}else{
					_GLOBAL.transaction_key = Response.transaction_key;
				}
			}
			else {
				_GLOBAL.transaction_key = null;
			}
			
			//release queue
			_GLOBAL.queue_continue = true;
		};
	//end handling response

	//begin execute request	
		var Data = new Array();
		Data.push("captcha_code=" + _GLOBAL.captcha_code);
		var myPostData = Data.join("&");
		MyAjax.post("/seo-tools/_captcha.aspx?r=get_transaction_key", myPostData);	
	//end execute request
	}
	
//--> End Function :: GetTransactionKey

//##########################################################################################

//--> Begin Function :: RefreshCaptcha

	function RefreshCaptcha(){
		var its_random = new Date().getTime();
		document.$("captcha").setAttribute('src', '/seo-tools/_captcha.aspx?' + its_random);	
	}
	
//--> End Function :: RefreshCaptcha

//##########################################################################################

//--> Begin Function :: SetInnerHTMLById

	function SetInnerHTMLById(id, html){
		if(html == ""){
			html = "&#160;";	
		}
		var Elements = document.getElementsByTagName("*");
		for(var i = 0 ; i < Elements.length ; i++) {
			if(Elements[i].id == id){
				try{
					Elements[i].innerHTML = html;
				}catch(err){
					alert("error name:" + err.name + "\n message:" + err.message + "\n" + "id:"+ id + "\n html:" + html.length);	
				}
			}
		}

	}

//--> End Function :: SetInnerHTMLById

//##########################################################################################

//--> Begin Function :: AddToQueue

	_GLOBAL.queue = new Array();
	_GLOBAL.queue_continue = true;
	function AddToQueue(obj){
		_GLOBAL.queue.push(obj);
	}
	
//--> End Function :: AddToQueue

//##########################################################################################

//--> Begin Function :: ExecuteQueue
	
	function ExecuteQueue(){
		
		//is the queue empty?
		if(_GLOBAL.queue.length == 0) {
			return;
		//is the engine busy?
		}else if(_GLOBAL.queue_continue == false) {
			//call the queue sender in another 10 m.s.
			setTimeout("ExecuteQueue()", 100);
		}
		else{
			
			//dont execute until the function called sets to true
			_GLOBAL.queue_continue = false;
			
			//shift off current function
			var CurrentCall = _GLOBAL.queue.shift();
			
			//execute
			eval(CurrentCall);
			
			if(_GLOBAL.queue.length > 0){
				ExecuteQueue();
			}
		}
			
	}
	
//--> End Function :: ExecuteQueue

//##########################################################################################

//--> Begin Function :: ViewReport

	function ViewReport(report_id){
		ShadeScreen(false);
		window.open("/seo-tools/download_report.aspx?r=ranking_report&report_id=" + _GLOBAL.report_id ,'','scrollbars=yes,menubar=no,height=600,width=800,resizable=yes,toolbar=no,location=no,status=no');
	}
	
//--> End Function :: ViewReport

//##########################################################################################