var QS_Utils;

/**

QS_Utils:
   For reusable global/util functions

author: dedmondson
date:   13/05/2009
*/

// Object Def
function  QS_Utils(){
	
  // Public 
  return{
  	
	
	getUrlParam:function(name){
		
		/*
		  Retrieves a named parameter from the request URL
		  
		  Date: 05/01/2009
		  Author:dedmondson
		  Source: http://www.mabaloo.com/Web-Development/Accessing-GET-parameters-with-Javascript.html
		*/

		name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]"); 
		var regexS = "[\\?&]"+name+"=([^&#]*)"; 
		var regex = new RegExp( regexS ); 
		var results = regex.exec( window.location.href ); 
		if( results == null ){
			
			/* This may be because the page has an unexpected error condition 
			 * and not returned the main store variables in the forwarded response..
			   ..so try to get it from the session cache .
			*/
			if(name == 'storeId'){
				return sessvars.guard_storeId;
			}
			else if(name == 'catalogId'){
				return sessvars.guard_catalogId;
			}
			else if(name == 'langId'){
				return sessvars.guard_langId;
			}
			else{
				return ""; 
			}	
		}
		else {
			var param = results[1];
			
			// cache main store variables in case we loose them in an error condition.
			if(param)
			{
				if(name == 'storeId'){
				 if(sessvars.guard_storeId != param){
					 sessvars.guard_storeId = param;
				 }
				}
				else if(name == 'catalogId'){
				 if(! sessvars.guard_catalogId != param){
					 sessvars.guard_catalogId = param;
				 }
				}
				else if(name == 'langId'){
				 if(! sessvars.guard_langId != param ){
					 sessvars.guard_langId = param;
				 }
				}
			}
		    return param;
	    }
		
	},
	
	getCookie:function (name){

		/**
		  Get a named value from document.cookie
		  
		  Taken from : Wrox - Professional Javascript for Web Developers. Chapter 16, p484.
		  
		  Date: 03/02/2009
		  Author:dedmondson
		*/
		
	  var pattern ="(?:; )?" + name + "=([^;]*);?";
	  var regex = new RegExp(pattern);
	  
	  if( regex.test(document.cookie) ){
	    return decodeURIComponent(RegExp["$1"]);
	  }
	  else{
	   return null;
	  }
	  
	},
	
	deleteCookie:function (name, path, domain) {
	
		/**
		  delete named value via expiry method from document.cookie
		
		  Date: 03/02/2009
		  Author:dedmondson
		*/
		
	    if (QS_Utils.getCookie(name)) {
	        document.cookie = name + "=" + 
	            ((path) ? "; path=" + path : "") +
	            ((domain) ? "; domain=" + domain : "") +
	            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	   }
	},
	
	callNull:function(){
		
		/**
		  Simply an empty call.
		  
		  This is used from <a href="javascript: QS_Utils.callNull();"> so that 
		  the link will not invoke a  response other than that wired up by jquery click bindings.
		  
		  Date: 08/01/2009
		  Author:dedmondson
		*/
		
	},
	
	createPageLink:function(linkName , linkTarget,linkClass , linkContainerId){ 
	    /*
		  Creates a page link from a contentComponent to pageLink component
		  when page renders.
		  Date: 05/04/2009
		  Author:dedmondson
		*/
	
	 $("#"+linkContainerId).append("<li class='"+linkClass+"'><a href='#"+linkTarget+"'>"+linkName+"</a></li>");
	},

	fadeTransition:function(fadeOutSelector, fadeInSelector, speed){
		
		/**
		Method to clean up commonly used fade code.
		
		Date: 03/02/2009
		Author:dedmondson
		*/
	    $(fadeOutSelector).fadeOut(speed,function(){
	    	$(fadeInSelector).fadeIn(speed)
	    });
	    
	},
	
	encodeHtml:function( s ) {
	    so = new String(s);
	    so =  so.replace("\"", ""); 
	    so =  so.replace("\'", ""); 
		return  so.valueOf();
	 }, 
	
	validateCardExpiryDate:function( expiryMonth, expiryYear ){
	
		var date = new Date();
		var year = date.getFullYear();
		var month = date.getMonth();
		month++;
	
		var monthsSinceADZero = year * 12 + month;
		var expiryMonthsSinceADZero = (expiryYear +2000) * 12 + expiryMonth;
		
		if((monthsSinceADZero <= expiryMonthsSinceADZero)  && (expiryMonth < 13) && (expiryMonth > 0)) {
		    return true;
		} else {    
		    return false;
		}
	},
	
	validateCardStartDate:function( startMonth, startYear ){
	    
	
			var date = new Date();
			var year = date.getFullYear();
			var month = date.getMonth();
			month++;
		
			var monthsSinceADZero = year * 12 + month;
			var startMonthsSinceADZero = (startYear +2000) * 12 + startMonth;
			
			if( (startMonthsSinceADZero <= monthsSinceADZero) && (startMonth < 13) && (startMonth > 0)){
			    return true;
			} else {    
			    return false;
			}
		
	},
	
	hideDefaultErrors:function(){
	  $("#QS_defaultErrorSection").hide();
	},
	
	hideValidationErrors:function(){
		$(".errorSection").hide();
		$(".errorSection ul").empty();
		$("*").removeClass("errorHighlight");
	}
    
 
  }//end return
}//end object def

// On page load
$(function(){
 QS_Utils = new QS_Utils();
});