window.Submit_request=function(){
	;

	conpoll_init(20,40);
	asyncRequest("GET", buildAjaxLink(), processSerpanalyticsCallback, "");

	//request link from serpanalytics
	// render responce to page
	// look through all name='PRrequest' and request pageranks
	// after responce - render pagerank,using template:  <div class="prbar_container"><div class="prbar_fill" style="width:__pr__0%"></div></div> __pr__</td>
	
	
	//cument.request_form.action=link;
	//cument.request_form.submit();
}
window.process_SerpanalyticsSuccess=function(o){
	document.getElementById("results").innerHTML = o.responseText;
	
	requests = document.getElementsByName("PRrequest");
	for(i=0;i<requests.length;i++){
		requesturl = requests[i].href;
		asyncRequest("GET",requesturl, processGoogleCallback( "div_" + requests[i].id), "");
	}	
}
window.processSerpanalyticsCallback={success:process_SerpanalyticsSuccess, failure:function(o){}};

window.getpagerank=function(text){ 
	res = text.match(/Rank_[^:]+:[^:]+:(\d+)/);
	if(res&&res[1])
		return res[1];
	return false;
}

window.process_GoogleSuccess=function(id){return function(o){
	pr = getpagerank(o.responseText);
	if(pr){
		template =  '<div class="prbar_container"><div class="prbar_fill" style="width:__pr__0%;display:__display__"></div></div> __pr__</td>';
		document.getElementById(id).innerHTML = template.replace(/__pr__/g,pr).replace(/__display__/g,(pr==0)?'none':'block');
	}else{
		process_GoogleFailure(id)(o);
	}
}}		
window.process_GoogleFailure=function(id){return function(o){
	template =  '<div class="prbar_container"></div>Error or no PR value</td>';
	document.getElementById(id).innerHTML = template;//str_replace("__pr__",getpagerank(o.responseText),template);
}}	
window.processGoogleCallback=function(id){return { success:process_GoogleSuccess(id), failure:process_GoogleFailure(id) }};// place after functions
// functions for operating with two list of datacenters - selected dcs and full list

	// compare to ip's for sorting
	window.compareips=function(a,b){
			var na = a.split(/\./);
			var nb = b.split(/\./);
			for(j=0;j<4;j++){
				if(parseInt(na [j]) >parseInt(nb[j]) || nb[j]=='*'){
					return 1;
				}
				if(parseInt(na [j]) < parseInt(nb[j])|| na[j]=='*'){
					return -1;
				}
			}
			return 0;
	}	

	//from lj.com - move some items from list to another
	window.moveItems=function(from, to){
	     var selindex;
	     while ((selindex=from.selectedIndex) != -1)
	     {
	         var i;
	         var item = new Option(from.options[selindex].text,
	                               from.options[selindex].value,
	                               false, true);
	         from.options[selindex] = null;

			 // find spot to put new item
	         for (i=0; i<to.options.length && compareips(to.options[i].text,item.text)  == -1 ; i++) { }
	         newindex=i;
			 //alert(newindex);

	         // move everything else down
	         for (i=to.options.length; i>newindex; i--) {
	                  to.options[i] = new Option(to.options[i-1].text,
	                                        to.options[i-1].value,
	                                        false,
	                                        to.options[i-1].selected);
	         }
	         to.options[newindex] = item;
	     }
		 to.selectedIndex=-1;
	}
 
	//code for add and remove buttons
	window.AddItem=function(){
		moveItems(document.getElementById("datacenters"),document.getElementById("selected"));
		buildLink();
	}
	window.RemoveItem=function(){
		moveItems(document.getElementById("selected"),document.getElementById("datacenters"));
		buildLink();
	}

	// Checking selection - ip's with star '*' should not be selected
	window.CheckSelection=function(){
		var obj = document.getElementById("datacenters");
		for( i=0;i<obj.options.length;i++){
			if(obj.options[i].selected && (obj.options[i].text.lastIndexOf('*')!=-1))
				obj.options[i].selected = false;
		}
	}
	
	