var URL = location.href.replace("http://","");
var URL_SEC = URL.split("?");
var URL_SEC_1 = URL_SEC[0];
var URL_SEC_2 = (URL_SEC[1]==undefined)?"":URL_SEC[1];
var segmentArr = [];
loadSeg();

////For Localuser
//var COMPONENT  = segment(2);
//var ACTION  = segment(3);
//var SITE_URL = "http://" + segment(0)+"/"+segment(1)+"/";

////For Live
var COMPONENT  = segment(1);
var ACTION  = segment(2);
var SITE_URL = "http://" + segment(0)+"/";


COMPONENT = ( COMPONENT== undefined)?"store":COMPONENT;
ACTION = ( ACTION== undefined)?"home":ACTION;


function queryString(ji) {
    var hu = (URL_SEC_2=="")?"":URL_SEC_2;
    gy = hu.split("&");
    for (i=0;i<gy.length;i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) {
            return ft[1];
        }
    }
}

function loadSeg()
{
	var gy = URL_SEC_1.split("/");
	for (i=0;i<gy.length;i++) {
	    ft = gy[i].split("=");
		
		if(gy[i].length!= 0 )
		{
			segmentArr.push(gy[i]);
		}
		
	}
}

function segment(id) {

    if(segmentArr[id])
    {
		return segmentArr[id];
    }    
}

function strstr (haystack, needle, bool) {
    
    var pos = 0;
    haystack += '';
    pos = haystack.indexOf( needle );    if (pos == -1) {
        return false;
    } else{
        if (bool){
            return haystack.substr( 0, pos );        } else{
            return haystack.slice( pos );
        }
    }
}

function Validate_Email_Address(email_address)
{
//Assumes that valid email addresses consist of user_name@domain.tld
at = email_address.indexOf('@');
dot = email_address.indexOf('.');

if(at == -1 || 
   dot == -1 || 
   dot <= at + 1 ||
   dot == 0 || 
   dot == email_address.length - 1)
   return(false);
   
user_name = email_address.substr(0, at);
domain_name = email_address.substr(at + 1, email_address.length);                  

if(Validate_String(user_name) === false || 
   Validate_String(domain_name) === false)
   return(false);                     

return(true);
}

function Validate_String(string, return_invalid_chars)
{
valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
invalid_chars = '';

if(string == null || string == '')
   return(true);

//For every character on the string.   
for(index = 0; index < string.length; index++)
   {
   char = string.substr(index, 1);                        
   
   //Is it a valid character?
   if(valid_chars.indexOf(char) == -1)
     {
     //If not, is it already on the list of invalid characters?
     if(invalid_chars.indexOf(char) == -1)
       {
       //If it's not, add it.
       if(invalid_chars == '')
          invalid_chars += char;
       else
          invalid_chars += ', ' + char;
       }
     }
   }                     
   
//If the string does not contain invalid characters, the function will return true.
//If it does, it will either return false or a list of the invalid characters used
//in the string, depending on the value of the second parameter.
if(return_invalid_chars == true && invalid_chars != '')
  {
  last_comma = invalid_chars.lastIndexOf(',');
  
  if(last_comma != -1)
     invalid_chars = invalid_chars.substr(0, $last_comma) + 
     ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
             
  return(invalid_chars);
  }
else
  return(invalid_chars == ''); 
}

function reloadModule(module,element)
{
	
	$(element).html("Loading....");
	$.ajax({
				url: SITE_URL + "ajax/reloadmodule",
				type: "GET",
				dataType: "html",
				data: { module:module},
				success: function(theResponse) 
				{
					$(element).html(theResponse);
				}
			});
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}

var FilterList = {}
FilterList.List = {
        Filter : function (inputSelector, listSelector) {

                // Sanity check
                var inp, rgx = new RegExp(), titles = $(listSelector), keys;
                if (titles.length === 0) {
                        return false;
                }

                // The list with keys to skip (esc, arrows, return, etc)
                // 8 is backspace, you might want to remove that for better usability
                keys = [ 13, 27, 32, 37, 38, 39, 40, 8 ];

                // binding keyup to the unordered list
                $(inputSelector).bind('keyup', function (e) {
                        if (jQuery.inArray(e.keyCode, keys) >= 0) {
                                return false;
                        }

                        // Building the regex from our user input, 'inp' should be escaped
                        inp = $(this).attr('value');
                        rgx.compile(inp, 'im');
                        titles.each(function () {
                                if (rgx.source !== '' && !rgx.test($(this).html())) {
                                        $(this).parent('li').hide();
                                } else {
                                        $(this).parent('li').show();
                                }
                        });
                });
        }
};

