/**
* Function gets info about specified widget (provided by w_id) and inject html with widget description 
*/
function ajax_showInstalledWidgets(wc_id) {
	var xmlHttp;
	try  {
  		xmlHttp=new XMLHttpRequest();
  	} 
  	catch (e)  {
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	} 
    	catch (e) {
    		try {
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		} 
      		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
      		}
    	}
  	}
  	
  	xmlHttp.onreadystatechange=function() {
    	if (xmlHttp.readyState==4) {
		   document.getElementById('installedWidgets').innerHTML = xmlHttp.responseText;
		   YAHOO.example.DDApp.init();
		   
      		//alert(xmlHttp.responseText);
      	}
    }
  	
    xmlHttp.open("GET","/widgets/installed/"+wc_id,true);
    xmlHttp.send(null);
}

function ajax_showAllWidgets(wc_id,t) {
        (t=top.document.getElementById('allWidgets')).className+=' loading'

        var xmlHttp;
	try  {
  		xmlHttp=new XMLHttpRequest();
  	} 
  	catch (e)  {
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
    	catch (e) {
    		try {
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		} 
      		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
      		}
    	}
  	}
  	
  	xmlHttp.onreadystatechange=function() {
    	if (xmlHttp.readyState==4) {


		   document.getElementById('allWidgets').innerHTML = xmlHttp.responseText;
		   t.className=t.className.replace(/\bloading\b/g,'loaded')

      		//alert(xmlHttp.responseText);
      	}
    }
  	
    xmlHttp.open("GET","/widgets/all/"+wc_id,true);
    xmlHttp.send(null);
}

function ajax_deleteWidget(f_id, w_id, wc_id, number_of_user_widgets, max_user_widgets, widget_title) {
	var xmlHttp;
	try  {
  		xmlHttp=new XMLHttpRequest();
  	} 
  	catch (e)  {
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	} 
    	catch (e) {
    		try {
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		} 
      		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
      		}
    	}
  	}
  	
  	xmlHttp.onreadystatechange=function(c) {
    	if (xmlHttp.readyState==4) {
		   //document.getElementById('workarea').innerHTML = xmlHttp.responseText;
		   if (xmlHttp.responseText==1) {
                           ajax_showInstalledWidgets(wc_id);
                           if (number_of_user_widgets==max_user_widgets)
			      ajax_showAllWidgets(wc_id);
			   (c=document.getElementById('widget-'+w_id)).className=c.className.replace(/\bloaded\b/g,'')
			   document.getElementById('widget-title-'+w_id).innerHTML = widget_title;


		   }
      		//alert(xmlHttp.responseText);
      	}
    }
  	
    xmlHttp.open("GET","/widget/"+f_id+"/delete/",true);
    xmlHttp.send(null);
}

function ajax_addWidget(w_id, wc_id, number_of_user_widgets, max_user_widgets) {

        (t=document.getElementById('widget-'+w_id)).className+=' loading'

	var xmlHttp;
	try  {
  		xmlHttp=new XMLHttpRequest();
  	} 
  	catch (e)  {
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	} 
    	catch (e) {
    		try {
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		} 
      		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
      		}
    	}
  	}
  	
  	xmlHttp.onreadystatechange=function() {
    	if (xmlHttp.readyState==4) {
		   //document.getElementById('workarea').innerHTML = xmlHttp.responseText;
		   if (xmlHttp.responseText>0) {
			   ajax_showInstalledWidgets(wc_id);

			   if (xmlHttp.responseText==max_user_widgets)
			      ajax_showAllWidgets(wc_id);
			   t.className=t.className.replace(/\bloading\b/g,'loaded')

		   }
      		//alert(xmlHttp.responseText);
      	}
    }
  	
    xmlHttp.open("GET","http://www.myflyscreen.com/index.php?module=widgets&action=add_widget_to_set&w_id="+w_id+"&wc_id="+wc_id,true);
  	xmlHttp.send(null);
}


function ajax_addCustomWidget(w_id, wc_id, number_of_user_widgets, max_user_widgets) {
	(t=top.document.getElementById('widget-'+w_id)).className+=' loading'

	var xmlHttp;
	try  {
  		xmlHttp=new XMLHttpRequest();
  	} 
  	catch (e)  {
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	} 
    	catch (e) {
    		try {
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		} 
      		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
      		}
    	}
  	}
  	
	xmlHttp.onreadystatechange=function(o) {
		if (xmlHttp.readyState==4) {
                        //alert(xmlHttp.responseText+'-'+max_user_widgets);
			//document.getElementById('workarea').innerHTML = xmlHttp.responseText;
			if (xmlHttp.responseText>=1) {
				top.ajax_showInstalledWidgets(wc_id);
				t.getElementsByTagName('p')[0].innerHTML=document.getElementById('uw_custom_rssfeed_title').value
				parent.parent.GB_hide();
				if (xmlHttp.responseText==max_user_widgets)
			        top.ajax_showAllWidgets(wc_id);
				t.className=t.className.replace(/\bloading\b/g,'loaded')
			}
		//alert(xmlHttp.responseText);
		}
	}
  	
    var uw_custom_color = document.getElementById("uw_custom_color").value;
    top.document.getElementById('widget'+w_id).style.backgroundColor=uw_custom_color;

    var uw_custom_rssfeed_link=encodeURIComponent(document.getElementById("uw_custom_rssfeed_link").value)
	var uw_custom_rssfeed_title=encodeURIComponent(document.getElementById("uw_custom_rssfeed_title").value)
	var uw_custom_color=encodeURIComponent(document.getElementById("uw_custom_color").value)
	var uw_encoding=encodeURIComponent(document.getElementById("uw_encoding").value)
	
	

    xmlHttp.open("GET","http://www.myflyscreen.com/index.php?module=widgets&action=add_widget_to_set&w_id="+w_id+"&wc_id="+wc_id+"&uw_custom_rssfeed_link="+uw_custom_rssfeed_link+"&uw_custom_rssfeed_title="+uw_custom_rssfeed_title+"&uw_custom_color="+uw_custom_color+"&uw_encoding="+uw_encoding,true);
  	xmlHttp.send(null);
}

function ajax_addPrivateWidget(w_id, wc_id, number_of_user_widgets, max_user_widgets) {
	(t=top.document.getElementById('widget-'+w_id)).className+=' loading'

	var xmlHttp;
	try  {
  		xmlHttp=new XMLHttpRequest();
  	} 
  	catch (e)  {
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	} 
    	catch (e) {
    		try {
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		} 
      		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
      		}
    	}
  	}
  	
  	xmlHttp.onreadystatechange=function() {
    	if (xmlHttp.readyState==4) {
		   //document.getElementById('workarea').innerHTML = xmlHttp.responseText;
		   if (xmlHttp.responseText>=1) {
			   top.ajax_showInstalledWidgets(wc_id);
			   parent.parent.GB_hide();
			   if (xmlHttp.responseText==max_user_widgets)
                           top.ajax_showAllWidgets(wc_id);
			   t.className=t.className.replace(/\bloading\b/g,'loaded')
		   }
      		//alert(xmlHttp.responseText);
      	}
    }
  	var uw_login=encodeURIComponent(document.getElementById("uw_login").value)
	var uw_password=encodeURIComponent(document.getElementById("uw_password").value)

    xmlHttp.open("GET","http://www.myflyscreen.com/index.php?module=widgets&action=add_widget_to_set&w_id="+w_id+"&wc_id="+wc_id+"&uw_login="+uw_login+"&uw_password="+uw_password,true);
  	xmlHttp.send(null);
}

/**
* Function gets info about specified widget (provided by w_id) and inject html with widget description 
*/
function ajax_checkDownloading(u_login) {
	var xmlHttp;
	try  {
  		xmlHttp=new XMLHttpRequest();
  	} 
  	catch (e)  {
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	} 
    	catch (e) {
    		try {
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		} 
      		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
      		}
    	}
  	}
  	
  	xmlHttp.onreadystatechange=function() {
    	if (xmlHttp.readyState==4) {
		   //document.getElementById('widgetInfo').innerHTML = xmlHttp.responseText;
      		if (xmlHttp.responseText==1) location='http://www.myflyscreen.com/index.php?module=registration&action=registerFormStep3&u_login='+u_login;
      	}
    }
  	
    xmlHttp.open("POST","http://www.myflyscreen.com/index.php?module=registration&action=checkAppDownloaded&u_login="+u_login,true);
  	xmlHttp.send(null);
}


function ajax_checkAppLaunching(u_login) {
	var xmlHttp;
	try  {
  		xmlHttp=new XMLHttpRequest();
  	} 
  	catch (e)  {
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	} 
    	catch (e) {
    		try {
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		} 
      		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
      		}
    	}
  	}
  	
  	xmlHttp.onreadystatechange=function() {
    	if (xmlHttp.readyState==4) {
		   //document.getElementById('widgetInfo').innerHTML = xmlHttp.responseText;
      		if (xmlHttp.responseText==1) location='http://www.myflyscreen.com/index.php?module=registration&action=registerFormStep4';
      	}
    }
  	
    xmlHttp.open("GET","http://www.myflyscreen.com/index.php?module=registration&action=checkAppLaunched&u_login="+u_login,true);
  	xmlHttp.send(null);
}


function ajax_rssUrlValidation(input, w_id) {
	var xmlHttp;
	try  {
  		xmlHttp=new XMLHttpRequest();
  	} 
  	catch (e)  {
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	} 
    	catch (e) {
    		try {
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		} 
      		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
      		}
    	}
  	}
  	
  	xmlHttp.onreadystatechange=function() {
    	if (xmlHttp.readyState==4) {
  		if (xmlHttp.responseText!=0) {
		   		document.getElementById('url_validation').innerHTML = '<img src="http://www.myflyscreen.com/gfx/ico_valid.gif" /> URL is valid!';
                document.getElementById('url_validation').style.color="green";
		   		document.getElementById('url_validation_next_button').innerHTML = '<input class="button" type="image" src="http://www.myflyscreen.com/gfx/button_next.gif" width="158" height="27" alt="Next" border="0" />';
		   		document.getElementById('uw_encoding').value=xmlHttp.responseText;
				ajax_addCustomWidget(w_id,0)
		   }
		   else { 
		   		document.getElementById('url_validation').style.color="red";
		   		document.getElementById('url_validation').innerHTML = '<img src="http://www.myflyscreen.com/gfx/ico_invalid.gif" /> URL is invalid!';
		   		document.getElementById('url_validation_next_button').innerHTML = '<input class="button" type="image" src="http://www.myflyscreen.com/gfx/button_next.gif" width="158" height="27" alt="Next" border="0" />';
		   }
      	}
    }

	var uw_custom_rssfeed_link=encodeURIComponent(document.getElementById(input).value);
	xmlHttp.open("GET","http://www.myflyscreen.com/index.php?module=widgets&action=check_custom_widget_url&uw_custom_rssfeed_link="+uw_custom_rssfeed_link, true);
	xmlHttp.send(null);
}

function ajax_saveWidgetSet() {
        document.getElementById('saveButton').innerHTML='';
	document.getElementById('readyForSync').innerHTML='Saving...<br /><img src="/assets/images/gfx/progress.gif" />';

	var xmlHttp;
	try  {
  		xmlHttp=new XMLHttpRequest();
  	} 
  	catch (e)  {
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	} 
    	catch (e) {
    		try {
      			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      		} 
      		catch (e) {
      			alert("Your browser does not support AJAX!");
      			return false;
      		}
    	}
  	}
  	
  	xmlHttp.onreadystatechange=function() {
    	if (xmlHttp.readyState==4) {

		   if (xmlHttp.responseText==1) {
		          document.getElementById('readyForSync').innerHTML='Now you can hit "Update Now" on your phone to force a sync, or wait until the next update.  Custom widgets may take several minutes as they are running through our system for the first time.';
		          document.getElementById('saveButton').innerHTML='<input type="image" src="/assets/images/gfx/button_save.gif" width="158" height="27" alt="Save" value="send" name="submit"/>';

		   }

      	}
    }

    var hidden_widgets_order_list=encodeURIComponent(document.getElementById("hidden_widgets_order_list").value)
    xmlHttp.open("GET","/widgets/order/"+hidden_widgets_order_list,true);
  	xmlHttp.send(null);

}

function select_innerHTML(objeto,innerHTML){
    objeto.innerHTML = ""
    var selTemp = document.createElement("micoxselect")
    var opt;
    selTemp.id="micoxselect1"
    document.body.appendChild(selTemp)
    selTemp = document.getElementById("micoxselect1")
    selTemp.style.display="none"
    if(innerHTML.indexOf("<option")<0){
        innerHTML = "<option>" + innerHTML + "</option>"
    }
    innerHTML = innerHTML.replace(/<option/g,"<span").replace(/<\/option/g,"</span")
    selTemp.innerHTML = innerHTML
      
    
    for(var i=0;i<selTemp.childNodes.length;i++){
  var spantemp = selTemp.childNodes[i];
  
        if(spantemp.tagName){     
            opt = document.createElement("OPTION")
    
   if(document.all){ 
    objeto.add(opt)
   }else{
    objeto.appendChild(opt)
   }       
    
   for(var j=0; j<spantemp.attributes.length ; j++){
    var attrName = spantemp.attributes[j].nodeName;
    var attrVal = spantemp.attributes[j].nodeValue;
    if(attrVal){
     try{
      opt.setAttribute(attrName,attrVal);
      opt.setAttributeNode(spantemp.attributes[j].cloneNode(true));
     }catch(e){}
    }
   }

   if(spantemp.style){
    for(var y in spantemp.style){
     try{opt.style[y] = spantemp.style[y];}catch(e){}
    }
   }

   opt.value = spantemp.getAttribute("value")
   opt.text = spantemp.innerHTML

   opt.selected = spantemp.getAttribute('selected');
   opt.className = spantemp.className;
  } 
 }    
 document.body.removeChild(selTemp)
 selTemp = null
}

function ajax_loadPhoneModels() {

  var xmlHttp;
  try  {
    xmlHttp=new XMLHttpRequest();
  } 
  catch (e)  {
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    } 
    catch (e) {
      try {
	xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      } 
      catch (e) {
	alert("Your browser does not support AJAX!");
	return false;
      }
    }
  }
  	
  xmlHttp.onreadystatechange=function() {
    if (xmlHttp.readyState==4) {

      if (xmlHttp.responseText != 0) {
	document.getElementById('id_mobile_model').style.display = "inline";
	document.getElementById('id_mobile_other').style.display = "none";
	document.getElementById('id_mobile_other').value = "";
	select_innerHTML(document.getElementById('id_mobile_model'), xmlHttp.responseText);
      } else {
	document.getElementById('id_mobile_model').style.display = "none";
	document.getElementById('id_mobile_other').style.display = "inline";
      }

    }
  }

  var manufacturer = document.getElementById("id_mobile_manufacturer").value;
  var model_key = document.getElementById("id_mobile_model").value;
  xmlHttp.open("GET","/models/vendor/" + manufacturer + "/" + model_key, true);
  xmlHttp.send(null);

}

function copyit(theField) {
  theField.focus();
  theField.select();
  var selectedText = document.selection;
  if (selectedText && selectedText.type == 'Text') {
    var newRange = selectedText.createRange();
    theField.focus();
    theField.value = newRange.text;
  }
}
