// copyright 2007 SWAE SPorts
// a collection of Javascript functions used on the SWAE site
// Independent type function run standalone, using only the passed parameters to their thing;
// Dependent functions either use a global variable, or access specific elements on a page

	var ardenLib = {}; // global Object container
	ardenLib.urlLiveSite = "";
	
	//Added by RLA - fix for region lists
	ardenLib.ignoreThirdRegionList = false;
	
	ardenLib.userPressedBrowserButton = 1; // what initiated the page unload? if browser button, set to 1, other page element, set to 0

	ardenLib.acceptableLink = function( )
	{
		this.userPressedBrowserButton = 0; // no pop-up needed
	}

	ardenLib.userWantsOut = function( fromWhichPage)
	{
		if (this.userPressedBrowserButton)
		{
			switch( fromWhichPage )
			{
					case "adventure":
						var returnValue = "By exiting the Create/Edit an Adventure process without completing your profile the information that you have already entered will be automatically saved and you will have the ability to complete this profile in the future without starting over from scratch. We know, it's a beautiful thing. The incomplete profile will appear in your My Adventures section. From there you will be able to complete the profile or discard it.";
						break;
					case "event":
						var returnValue = "By exiting the Create/Edit an Event process without completing your profile the information that you have already entered will be automatically saved and you will have the ability to complete this profile in the future without starting over from scratch. We know, it's a beautiful thing. The incomplete profile will appear in your My Events section. From there you will be able to complete the profile or discard it.";
						break;
					case "profile":
						var returnValue = "By exiting the Edit Profile process without completing your profile the information that you have already entered will be automatically saved and you will have the ability to complete this profile in the future without starting over from scratch. We know, it's a beautiful thing. Next time you visit this page you will be given the option to continue where you left off or to start from scratch.";
						break;
					case "vendor":
						var returnValue = "By exiting the Edit Vendor Info process without completing your profile the information that you have already entered will be automatically saved and you will have the ability to complete this profile in the future without starting over from scratch. We know, it's a beautiful thing. Next time you visit this page you will be given the option to continue where you left off or to start from scratch.";
						break;
					default:
						var returnValue = "You have not saved this profile.  When exiting the edit process without completing and saving,  the information that you have already entered will be automatically saved and you will have the ability to complete this profile in the future without starting over from scratch. We know, it's a beautiful thing.";
						break;
			}
			return  returnValue + "\n\nIf you are receiving this alert and not attempting to exit the setup phase press OK to continue with the setup process";
	    }
	}


	ardenLib.getStateList = function( countryName , callBackFunction, targetState, targetRegion) 
	{
		$('ajaxCommSpinner').addClass('ajax-loading');
		new Ajax( this.urlLiveSite + '/ajax/getStateList.php', {method: 'get', data: 'country_name=' + encodeURIComponent(countryName) + '&targetState=' + targetState + '&targetRegion=' + targetRegion, onComplete: callBackFunction}).request();
	}

	ardenLib.getRegionList = function( countryName , stateName, callBackFunction, targetRegion) 
	{
		$('ajaxCommSpinner').addClass('ajax-loading');
//alert( 'country_name=' + encodeURIComponent(countryName) + '&state_name=' + encodeURIComponent(stateName) +  '&targetRegion=' + targetRegion);
		new Ajax( this.urlLiveSite + '/ajax/getRegionList.php', {method: 'get', data: 'country_name=' + encodeURIComponent(countryName) + '&state_name=' + encodeURIComponent(stateName) +  '&targetRegion=' + targetRegion, onComplete: callBackFunction}).request();
	}

	ardenLib.getStateListCallback = function( data ) 
	{
//alert (data );
		if (data.indexOf ( "<pre>" ) > -1 )
		{
			var myNewData = data.replace("<pre>","").replace("</pre>","");
			if( myNewData.length > 15 );
				eval( myNewData );

			var oStateSelect = document.getElementById(targetStateId);

			while (oStateSelect.length > 1) 
			{
			    oStateSelect.remove(1);
			}
			
			var optionCount = 0;
			for ( var keyVar in stateList  ) 
			{  
	
				var y=document.createElement('option');
	   		    y.text= stateList[keyVar];
		        y.value= keyVar;
		  		try
	    		{
			    	 oStateSelect.add(y,null); // standards compliant
		    	}
		  		catch(ex)
		  		{
		    		 oStateSelect.add(y); // IE only
    	  		}
				optionCount++;
   			}

			if( targetRegionId != "" )
				ardenLib.clearAllRegionLists(targetRegionId);


			if(optionCount > 0 )
			{
				oStateSelect.options[0].value = "";
				oStateSelect.options[0].text = "-- Select State --";
				$('ajaxCommSpinner').removeClass('ajax-loading');
			}
			else
			{
				oStateSelect.options[0].value = "NA";
				oStateSelect.options[0].text = " Skip State Selection ";
				if( targetRegionId != "" )
					ardenLib.getRegionList( countryName , "NA", ardenLib.getRegionListCallback, targetRegionId) ;
				else
					$('ajaxCommSpinner').removeClass('ajax-loading');
			}

		}

	}
	ardenLib.getStateListCallbackSpecial = function( data ) 
	{
			ardenLib.getStateListCallback( data ) ;
			document.forms['myAccountForm'].elements['user_shipping_state'].value = document.forms['myAccountForm'].elements['user_billing_state'].value
	}
	ardenLib.getStateListCallbackSpecial_new = function( data ) 
	{
			ardenLib.getStateListCallback( data ) ;
			document.forms['myAccountForm'].elements['new_user_shipping_state'].value = document.forms['myAccountForm'].elements['new_user_billing_state'].value
	}

	ardenLib.getRegionListCallback = function( data ) 
	{
//alert(data);
		if (data.indexOf ( "<pre>" ) > -1 )
		{
			var myNewData = data.replace("<pre>","").replace("</pre>","");
			if( myNewData.length > 15 );
				eval( myNewData );

			var oRegionSelect = document.getElementById(targetRegionId);
			var oRegionSelect2 = document.getElementById(targetRegionId + "2");

			ardenLib.clearAllRegionLists(targetRegionId);
//alert( oRegionSelect.id + ":" + oRegionSelect2.id + ":" + 	oRegionSelect3.id );				
			for ( var keyVar in regionList  ) 
			{  
	
				var y=document.createElement('option');
				var y2=document.createElement('option');
				if (!this.ignoreThirdRegionList)
					var y3=document.createElement('option');
	   		    y.text= unescape(regionList[keyVar].replace(/\+/g, " "));
		        y.value= keyVar;
	   		    y2.text= unescape(regionList[keyVar].replace(/\+/g, " "));
		        y2.value= keyVar;
	
					try
					{
						 oRegionSelect.add(y,null); // standards compliant
						 oRegionSelect2.add(y2,null); // standards compliant
					}
					catch(ex)
					{
						 oRegionSelect.add(y); // IE only
						 oRegionSelect2.add(y2); // IE only
					}

   			}

		}

		$('ajaxCommSpinner').removeClass('ajax-loading');
	}

	ardenLib.clearAllRegionLists = function( targetRegionId )
	{
		var oRegionSelect = document.getElementById(targetRegionId);
		var oRegionSelect2 = document.getElementById(targetRegionId + "2");

		while (oRegionSelect.length > 1) 
		    oRegionSelect.remove(1);
		while (oRegionSelect2.length > 1) 
		    oRegionSelect2.remove(1);
		
	}

	/* For Home Page */
	ardenLib.getHomeRegionList = function( countryId , callBackFunction, targetRegion, targetActivity) 
	{
		//alert(countryId);
		//$('ajaxCommSpinner').addClass('ajax-loading');
		new Ajax( this.urlLiveSite + '/ajax/getHomeRegionList.php', {method: 'get', data: 'countryId=' + encodeURIComponent(countryId) +  '&targetRegion=' + targetRegion +  '&targetActivity=' + targetActivity, onComplete: callBackFunction}).request();
	}

	ardenLib.getHomeRegionListCallback = function( data ) 
	{
		//alert (data );
		if (data.indexOf ( "<pre>" ) > -1 )
		{
			var myNewData = data.replace("<pre>","").replace("</pre>","");
			if( myNewData.length > 15 );
				eval( myNewData );

			//alert(targetRegion);
			var oRegionSelect = document.getElementById(targetRegionId);
			//var countryId = document.getElementById(countryId);
			//alert(countryId);
			//alert(oRegionSelect.length);
			while (oRegionSelect.length > 1) 
			{
			    oRegionSelect.remove(1);
			}
			
			var optionCount = 0;
			for ( var keyVar in regionList  ) 
			{	
				var y=document.createElement('option');
	   		    y.text= regionList[keyVar];
		        y.value= keyVar;
		  		try
	    		{
			    	 oRegionSelect.add(y,null); // standards compliant
		    	}
		  		catch(ex)
		  		{
		    		 oRegionSelect.add(y); // IE only
    	  		}
				optionCount++;
   			}

			oRegionSelect.options[0].value = "0";
			oRegionSelect.options[0].text = "- All Regions";

			//ardenLib.getHomeActivityList( countryId , ardenLib.getActivityListCallback, targetRegionId) ;
			ardenLib.getHomeActivityList( countryId, ardenLib.getHomeActivityListCallback, targetActivityId);
			//$('ajaxCommSpinner').removeClass('ajax-loading');
			
			/*if(optionCount > 0 )
			{
				oRegionSelect.options[0].value = "0";
				oRegionSelect.options[0].text = "- All Regions";
				//$('ajaxCommSpinner').removeClass('ajax-loading');
			}
			else
			{
				oRegionSelect.options[0].value = "0";
				oRegionSelect.options[0].text = "- All Regions";
				//$('ajaxCommSpinner').removeClass('ajax-loading');
			}*/
		}

	}

	ardenLib.getHomeActivityList = function( countryId , callBackFunction, targetActivity) 
	{
		//alert(countryId);
		//$('ajaxCommSpinner').addClass('ajax-loading');
		new Ajax( this.urlLiveSite + '/ajax/getHomeActivityList.php', {method: 'get', data: 'countryId=' + encodeURIComponent(countryId) +  '&targetActivity=' + targetActivity, onComplete: callBackFunction}).request();
	}

	ardenLib.getHomeActivityListCallback = function( data ) 
	{
		//alert (data );
		if (data.indexOf ( "<pre>" ) > -1 )
		{
			var myNewData = data.replace("<pre>","").replace("</pre>","");
			if( myNewData.length > 15 );
				eval( myNewData );

			//alert(targetRegion);
			var oActivitySelect = document.getElementById(targetActivityId);
			//alert(oRegionSelect.length);
			while (oActivitySelect.length > 0) 
			{
			    oActivitySelect.remove(0);
			}
			
			var optionCount = 0;
			for ( var keyVar in activityList  ) 
			{	
				var y=document.createElement('option');
	   		    y.text= activityList[keyVar];
		        y.value= keyVar;
		  		try
	    		{
			    	 oActivitySelect.add(y,null); // standards compliant
		    	}
		  		catch(ex)
		  		{
		    		 oActivitySelect.add(y); // IE only
    	  		}
				optionCount++;
   			}
		}
	}
	/* For Home Page */
	
   ardenLib.findPosX = function(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  ardenLib.findPosY = function(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
