// Library file for utility functions either pulled from the web or hand crafted.



/*
  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
*/
function getUrlParam(name)
{ 
	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.bookaboo_storeId;
		}
		else if(name == 'catalogId'){
			return sessvars.bookaboo_catalogId;
		}
		else if(name == 'langId'){
			return sessvars.bookaboo_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.bookaboo_storeId != param){
				 sessvars.bookaboo_storeId = param;
			 }
			}
			else if(name == 'catalogId'){
			 if(! sessvars.bookaboo_catalogId != param){
				 sessvars.bookaboo_catalogId = param;
			 }
			}
			else if(name == 'langId'){
			 if(! sessvars.bookaboo_langId != param ){
				 sessvars.bookaboo_langId = param;
			 }
			}
		}
	    return param;
    }
}

/*
Checks that a password string contains at least one char and one number.




Date: 05/01/2009
Author:dedmondson
*/
function isValidCommercePassword(string)
{
	var isAlphaNumeric  = new RegExp( "[/^[a-z\d]{6,10}$/i]" ); 
	var containsAlpha   = new RegExp( "[/[a-z]/i]" ); 
	var containsNumeric = new RegExp( "[/\d/]" ); 
    alert('test1'+isAlphaNumeric.test(string));
    alert('test2'+containsAlpha.test(string));
    alert('test3'+containsNumeric.test(string));
	if(isAlphaNumeric.test(string) && (containsAlpha.test(string) && containsNumeric.test(string)) ){
		return true;
	
	}

	return false;
}


/**
  Simply an empty call.
  
  This is used from <a href="javascript: 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
*/
function callNull(){
}

/**
  Get a named value from document.cookie
  
  Taken from : Wrox - Professional Javascript for Web Developers. Chapter 16, p484.
  
  Date: 03/02/2009
  Author:dedmondson
*/
function getCookie(name){
  var pattern ="(?:; )?" + name + "=([^;]*);?";
  var regex = new RegExp(pattern);
  
  if( regex.test(document.cookie) ){
    return decodeURIComponent(RegExp["$1"]);
  }
  else{
   return null;
  }
  
}

/**
  delete named value via expiry method from document.cookie

  Date: 03/02/2009
  Author:dedmondson
*/
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
   }
}

/**
Method to clean up commonly used fade code.

Date: 03/02/2009
Author:dedmondson
*/
function fadeTransition(fadeOutSelector, fadeInSelector, speed){
    $(fadeOutSelector).fadeOut(speed,function(){
    	$(fadeInSelector).fadeIn(speed)
    });
    
}


/**
	encodeHtml
	Takes a string and ecodes it for html so that it may be 
	inserted by jquery safely into a dom node
	
	Todo .. not implemented yet
	
	Date: 16/02/2009
	Author:dedmondson
 */

function encodeHtml( s ) {
    so = new String(s);
    so =  so.replace("\"", ""); 
    so =  so.replace("\'", ""); 
	return  so.valueOf();
  } 


function validateCardExpiryDate( 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;
	}
}

function validateCardStartDate( 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;
		}
	
}

function hideDefaultErrors(){
  $("#QS_defaultErrorSection").hide();
}








