// JavaScript Document
// select option country drop down ------------------------------------------------------------------------------------------------
  $(document).ready(function() {
            $(".dropdown img.flag").addClass("flagvisibility");

            $(".dropdown dt a").click(function() {
                $(".dropdown dd ul").toggle();
            });
                        
            $(".dropdown dd ul li a").click(function() {
                var text = $(this).html();
                $(".dropdown dt a span").html(text);
                $(".dropdown dd ul").hide();
                //$("#result").html("Selected value is: " + getSelectedValue("sample"));
				var countryVal = getSelectedValue("sample");
				checkAll(countryVal);
            });
                        
            function getSelectedValue(id) {
                return $("#" + id).find("dt a span.value").html();
            }

            $(document).bind('click', function(e) {
                var $clicked = $(e.target);
                if (! $clicked.parents().hasClass("dropdown"))
                    $(".dropdown dd ul").hide();
            });


            $("#flagSwitcher").click(function() {
                $(".dropdown img.flag").toggleClass("flagvisibility");
            });
        });
//------------------------------------------------------------------------------------------------------------------------------  
  function checkAll(str2){//(str,str2,id)
	var divEls = document.getElementsByTagName("span");
	var i = 0;
	var j = 0;
	var prods = new Array();
	for(i=0;i<divEls.length;i++){
		var thisDiv = divEls[i];
		var styleClassName = thisDiv.className;			
		if (styleClassName == 'activediv') {
				
			var stre=thisDiv.id;
			var id=stre.replace("img", "");
			var str = id;
			prods[j]= str;
			j++;
			document.getElementById("img"+id).innerHTML = '<img style="margin-right:79px" align="left" src="jos-addons/country-select/front/ajax-loader.gif" title="load" alt="load" />';
			
			if (str2 == ''){
				var thehtml2 = 'Select a Country';
				var thehtml = '<img align="left" src="jos-addons/country-select/img/plug.png" title="Is it compatible ?" alt="compatible" />';
				document.getElementById("img"+id).innerHTML=thehtml;
				document.getElementById("text"+id).innerHTML=thehtml2;
			}	
		} //end  if
	} // end for
	cookie_name = 'selectCountry';
	document.cookie=cookie_name+"="+str2+"; expires=Wednesday, 01-Aug-2040 08:00:00 GMT";
	if (str2!=''){
		if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	//response from server -----------------------------------------------------------------------------------	
		xmlhttp.onreadystatechange=function(){
			if (xmlhttp.readyState==4 && xmlhttp.status==200){
				var countryname = '';
				var countryimg = '';
				var responsear = xmlhttp.responseText;
				responsear = responsear.split("|");
				var response = responsear[0];
				countryname = responsear[1];
				countryimg = responsear[2];
				countryHzVt = responsear[3];		
				
				var response = response.split(",");
				
				var x=0;
				for (x=0; x<response.length; x++){
					var responseli = '';
					var responseli = response[x].split("->");
					
					if (responseli[1] == 'yes'){
						var thehtml = '<img align="left" src="jos-addons/country-select/img/plug-ok.png" title="compatible" alt="compatible" />';
						var thehtml2 = 'This part is <strong>compatible</strong> with '+countryname+' '+countryHzVt;
					}
					else if (responseli[1] == 'no'){
						var thehtml = '<img align="left" src="jos-addons/country-select/img/plug-not.png" title="not compatible" alt="not compatible" />';
						var thehtml2 = 'This part is <strong>NOT compatible</strong> with '+countryname+' '+countryHzVt;
					}
					else {
						var thehtml = '<img align="left" src="jos-addons/country-select/img/plug.png" title="Is it compatible ?" alt="Is it compatible ?" />';
						var thehtml2 = 'Select a Country';
					}
					if (responseli[0]!=''){
						document.getElementById("img"+responseli[0]).innerHTML=thehtml;
						document.getElementById("text"+responseli[0]).innerHTML=thehtml2;
						countryimgtag = '<img align="left" src="'+countryimg+'" title="'+countryname+'" alt="'+countryname+'" />';
						document.getElementById("countryimg"+responseli[0]).innerHTML=countryimgtag;
					}	
				}//end for	
			}
		}
		var prodsLine = prods.join(",");
		str = prodsLine+"|"+str2;
		xmlhttp.open("GET","jos-addons/country-select/front/result-ajax.php?q="+str,true);
		xmlhttp.send();
	}
}
