// JavaScript Document


//** GENERAL functions ----------------------------------------------------------------------------  **//
	function MM_swapImgRestore() { //v3.0
	  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
	}
	
	function MM_preloadImages() { //v3.0
	  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
		var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
		if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
	}
	
	function MM_findObj(n, d) { //v4.01
	  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	  if(!x && d.getElementById) x=d.getElementById(n); return x;
	}
	
	function MM_swapImage() { //v3.0
	  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
	}
	
	function popUp(whatPage, winName, w, h, scroll){
		var winT = (screen.height-h)/ 2;
		var winL = (screen.width-w)/ 2;
		winProps = 'height='+h+',width='+w+',top='+winT+',left='+winL+',toolbar=no ,scrollbars=no,resizable=no,status=no';
		storeWin = window.open(whatPage,winName,winProps);
		if (parseInt(navigator.appVersion) >= 4) { storeWin.window.focus(); }
		storeWin.window.focus(); 
	}



//** GENERAL functions ----------------------------------------------------------------------------  **//
function BrowserInfo(){

		this.agent = navigator.userAgent;
		this.version = navigator.appVersion.substr(0,4);
		this.codename = navigator.appCodeName;
		this.javaYes = navigator.javaEnabled();
		this.screenWidth = screen.width;
		this.screenHeight = screen.height;
		
		
		 //--> this.name // Browser name
		if ( (document.all) && (document.getElementById) ){ 
			if (this.agent.lastIndexOf("Opera") != -1) {
				this.name =  "OP"; // opera 7.0 + 
			}else{
				this.name =  "IE"; // IE 4.0+
			} 
		}else if( (!document.all) && (document.getElementById) ){ 
			if (this.agent.lastIndexOf("Firefox") != -1){
				this.name =  "FF"; // Firefox 1.0 + 
			}else if (this.agent.lastIndexOf("Safari") != -1){
				this.name =  "SF";  // Safari 1.0 + 
			}else{
				this.name =  "NN"; // Netscape 6+
			}
		}else if (document.layers){ // Netscape 4
			this.name = "NN4";
		}else{
			this.name = "other"; // not available
		}
		
		 //--> this.platform // Platform name
		if(navigator.appVersion.indexOf("Win") != -1){
		 this.platform = "PC";
		}else if(navigator.appVersion.indexOf("Mac") != -1){
		 this.platform = "Mac";
		}else{ 
		 this.platform = "other";
		}
	}

	var browser = new BrowserInfo();




//** AJAX General functions ----------------------------------------------------------------------------  **//

function ajaxObj(){ 

        var myObj = false;

        if (window.XMLHttpRequest) {
            myObj = new XMLHttpRequest();
            if (myObj.overrideMimeType) {
                myObj.overrideMimeType('text/xml'); // See note below about this line
            }
        }else if (window.ActiveXObject) { // PC -> IE 5.0 or 6.0
            try {
                myObj = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    myObj = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
				}
            }
        }
		
		if (!myObj) {
            return false;
        } else{
			return myObj;
		}
		
}

var http_request  = ajaxObj();




//** AJAX Specific Functions and Vars----------------------------------------------------------------------------  **//

	var myId = ""; // friends
	var language = "en"; // // shared
	var ajaxCountryId = ""; // shops
	var ajaxProvinceId = ""; // shops
	var ajaxCityId = ""; // shops
	var myElement = ""; // shops


	function doAjaxShared (url, outputFunction){ // shared //actually make the ajax call. Called by various doAjax functions. For example, in doAjaxProvinces()
		if (!http_request){
				alert ( (language == "en") ? "Error: An XMLHttpRequest Object could not be made." : "Erreur: L'objet XMLHttpRequest ne peut être créé." );
			}else{
				
				http_request.open('GET',url, true);
				http_request.onreadystatechange = outputFunction;
				http_request.send(null);
			}
	}
	
	function ajaxShopsLoadingMsg (anElementId){ // shared
			var myContent = (language == "en") ? "<select><option value='---' >Retrieving data...</option></select>" : "<select><option value='---' >Téléchargement en cours...</option></select>";
			if (browser.name != "NN4"){ 
					document.getElementById( anElementId ).innerHTML = myContent;
			} else {
					document.layers[ anElementId ].innerHTML = myContent;
			}
	}
	
	function ajaxShopsLoadingMsg2 (anElementId){ // shared
			var myContent = (language == "en") ? "Retrieving data......" : "Téléchargement en cours...";
			if (browser.name != "NN4"){ 
					document.getElementById( anElementId ).innerHTML = myContent;
			} else {
					document.layers[ anElementId ].innerHTML = myContent;
			}
	}
	
	
	//** ---------FRIENDS section of website --------------------------------------**/
	function doAjax(aLanguage, url, nameValue, emailValue, stateValue, countryValue, outputFunction, actionMsgElementId) {
	    language = aLanguage;
		myId = actionMsgElementId;

		if (nameValue != "" && emailValue != "" && stateValue != "---" && countryValue != "---"){
				doAjaxShared (url+'?lang='+language+'&name='+nameValue+'&email='+emailValue+'&state='+stateValue+'&country='+countryValue, outputFunction);
		}else{
			var ref = (browser.name != "NN4") ? document.getElementById(myId) : document.layers[ myId ] ; 
			ref.innerHTML = (language == "en") ? "Please fill-in all fields." : "S.V.P. veuillez remplir tous les champs requis.";
		}
	}
	function outputFeedback(){
        if (http_request.readyState == 4) { // loading complete
		
            if (http_request.status == 200) { // 200 means "OK", 404 means "Not Found"
			
				var doc = JSON.parse(http_request.responseText);
				var msg = doc.msg;
				var ref = (browser.name != "NN4") ? document.getElementById(myId) : document.layers[ myId ] ; 
				if (doc.success && doc.success == "yes"){
					ref.innerHTML = "<font color='#ADAF9C'>" + msg + "</font>";
				}else{
					ref.innerHTML =  msg ; 
				}
			}
            
        }else if (http_request.readyState == 1){ // loading
			ajaxShopsLoadingMsg2 (myId);
		}

    }
	
	
	
    //**-----------SHOPS Section-----------------------------------------------------------------------  **//
	
	function doAjaxProvinces(aLanguage, url, countryId, outputFunction) {
		if (countryId != "---"){
			language = aLanguage;
			ajaxCountryId = countryId;
			myElement = "provinces";
			doAjaxShared( url+'?lang='+language+'&countryId='+ajaxCountryId, outputFunction );
		}
	}
	function outputProvinces(){//(see ajax.getProvinces.php)
		
        if (http_request.readyState == 4) { // loading complete
		
           	if (http_request.status == 200) { // 200 means "OK", 404 means "Not Found"
			
				var doc = JSON.parse(http_request.responseText);
				if (doc.msg){
					 var myContent = doc.msg ; // means there was an error in sql or no data to display
				}else{
					var numOfResults = doc.total;
					if (numOfResults >=1){
							var myContent = '<select name="provinceForm" onChange="doAjaxCities(\'' + language + '\', \'../ajax/ajax.getCities.php\', '+ajaxCountryId+', this.value,  outputCities);"><option value="---" selected>' + ((language == 'en') ? '( Select )' : '( Selectionner )') + '</option><option value="---" disabled>---------------</option>';
							for (k=1; k<=numOfResults; k++){
								myContent += '<option value="' + doc['item' + k][0] + '">' + doc['item' + k][1] + '</option>';
							}
							myContent += '</select>';
					}
				}
					
				
				var ref = (browser.name != "NN4") ? document.getElementById(myElement) : document.layers[ myElement ] ; 
				var ref2 = (browser.name != "NN4") ? document.getElementById('cities') : document.layers[ 'cities' ] ; 
				ref.innerHTML = myContent;
				ref2.innerHTML = '<select><option value="---" selected>---</option></select>';
				
				
			}
            
        }else if (http_request.readyState == 1){ // loading
			ajaxShopsLoadingMsg (myElement);
		}

    }
	
	
	
	
	function doAjaxCities(aLanguage, url, countryId, provinceId, outputFunction) {
		if (provinceId != "---"  && countryId != "---"){
			language = aLanguage;
			ajaxCountryId = countryId;
			ajaxProvinceId = provinceId;
			myElement = "cities";
			doAjaxShared( url+'?lang='+language+'&countryId='+ajaxCountryId+'&provinceId='+ajaxProvinceId, outputFunction );
		}
	}
	function outputCities(){ //(see ajax.getCities.php)

        if (http_request.readyState == 4) { // loading complete
		
            if (http_request.status == 200) { // 200 means "OK", 404 means "Not Found"
				var doc = JSON.parse(http_request.responseText);
				 if (doc.msg){
					 var myContent = doc.msg ; // means there was an error in sql or no data to display
				 }else{
					var numOfResults = doc.total;
					if (numOfResults >=1){
						var myContent = '<select name="cityForm" onChange="doAjaxLocations(\'' + language + '\', \'../ajax/ajax.getLocations.php\', '+ajaxCountryId+', '+ajaxProvinceId+', this.value, outputLocations);"><option value="---" selected>' + ((language == 'en') ? '( Select )' : '( Selectionner )') + '</option><option value="---" disabled>---------------</option>';
						for (k=1; k<=numOfResults; k++){
							myContent += '<option value="' + doc['item' + k][0] + '">' + doc['item' + k][1] + '</option>';
						}
						myContent += '</select>';
					}
				 }
				
				var ref = (browser.name != "NN4") ? document.getElementById(myElement) : document.layers[ myElement ] ; 
				ref.innerHTML = myContent;
            }
			
				
			
        }else if (http_request.readyState == 1){ // loading
			ajaxShopsLoadingMsg (myElement);
		}

    }


	function doAjaxLocations(aLanguage, url, countryId, provinceId, cityId, outputFunction) {
		if (countryId != "---" && provinceId != "---" && cityId != "---" ){
			language = aLanguage;
			ajaxCountryId = countryId;
			ajaxProvinceId = provinceId;
			ajaxCityId = cityId;
			myElement = "locations";
			doAjaxShared(url+'?lang='+language+'&countryId='+ajaxCountryId+'&provinceId='+ajaxProvinceId+'&cityId='+ajaxCityId, outputFunction);
		}
	}
	function outputLocations(){ //(see ajax.getCities.php)

        if (http_request.readyState == 4) { // loading complete
		
            if (http_request.status == 200) { // 200 means "OK", 404 means "Not Found"
				var doc = JSON.parse(http_request.responseText);
				 if (doc.msg){
					 var myContent = doc.msg ; // means there was an error in sql or no data to display
				 }else{
					var numOfResults = doc.total;
					if (numOfResults >=1){
						var myContent = '<TABLE width=\"600\" cellpadding="0"  cellspacing="0"  border="0" >';
						if (numOfResults > 1){
							for (k=1; k<=numOfResults; k++){
								myContent += (k % 2 == 1) ?  "<TR>" : "" ; // we have an odd number
								myContent += "<TD><P>";
								myContent += "<span class=\"contactHdr\">" + doc['item' + k][0] + '</span><br>' //store_title
								myContent += doc['item' + k][1] + '<br>'; //store_address
								myContent += doc['item' + k][2] + ', '; // city_name
								myContent += doc['item' + k][3] + '<br>'; // state_name
								myContent += (doc['item' + k][4] != '') ? doc['item' + k][4] + '<br>' : '' ; //store_zipcode
								myContent += doc['item' + k][5] + '<br>'; //country_name
								myContent += (doc['item' + k][6] != "") ? doc['item' + k][6] + '<br>' : '' ; //store_info
								myContent += (doc['item' + k][7] != "") ? "<a href=\"" + doc['item' + k][7] + "\" target=\"_blank\">" + doc['item' + k][7] + '</a>' : '' ; //store_url
								myContent += "</P>";
								myContent += "</TD>";
								myContent += (k % 2 == 0) ?  "</TR>" : "" ;  // we have an even number
							}
						}else{
							 var myItem = 'item1';
							    myContent += "<TR><TD colspan=\"2\"><P>";
								myContent += "<span class=\"contactHdr\">" + doc[myItem][0] + '</span><br>' //store_title
								myContent += doc[myItem][1] + '<br>'; //store_address
								myContent += doc[myItem][2] + ', '; // city_name
								myContent += doc[myItem][3] + '<br>'; // state_name
								myContent += (doc[myItem][4] != '') ? doc[myItem][4] + '<br>' : '' ; //store_zipcode
								myContent += doc[myItem][5] + '<br>'; //country_name
								myContent += (doc[myItem][6] != "") ? doc[myItem][6] + '<br>' : '' ; //store_info
								myContent += (doc[myItem][7] != "") ? "<a href=\"" + doc[myItem][7] + "\" target=\"_blank\">" + doc[myItem][7] + '</a>' : '' ; //store_url
								myContent += "</P></TD></TR>";
								
						}
						    myContent += '</TABLE>';
					}
				 }
				
				var ref = (browser.name != "NN4") ? document.getElementById(myElement) : document.layers[ myElement ] ; 
				ref.innerHTML = myContent;
				
				
            }
			
        }else if (http_request.readyState == 1){ // loading
			ajaxShopsLoadingMsg2 (myElement);
		}

    }