var AFFL_shinsei_domain = "shinseibank.com"
var AFFL_shinsei_path = "/"

var AFFL_Name = new Array();
var AFFL_Value = new Array();
var AFFL_Keys = 0;

var AFFL_MAXLOCATION_LENGTH = 512;
var AFFL_HTTPONLY = false;
var AFFL_SSLONLY = false;

// M3.com custom functions

function AFFL_M3DotCom_SetCookie(){
	AFFL_BuildQueryStringList()
	
	if(AFFL_GetString("userId") != "" && AFFL_GetString("pageCode") != ""){
		AFFL_SetCookie("M3Code",  AFFL_GetString("userId"), AFFL_HTTPONLY, AFFL_SSLONLY)
		AFFL_SetCookie("M3PageID",  AFFL_GetString("pageCode"), AFFL_HTTPONLY, AFFL_SSLONLY)
	} else if(AFFL_GetString("userId") != "" && AFFL_GetString("pageCode") == ""){
		AFFL_SetCookie("M3Code",  AFFL_GetString("userId"), AFFL_HTTPONLY, AFFL_SSLONLY)
		AFFL_DeleteCookie("M3PageID")
	}
}


// cookie based affiliate utility functions

function AFFL_Sanitize(input_str){
	// limit input_str length to AFFL_MAXLOCATION_LENGTH
	if (input_str.length > AFFL_MAXLOCATION_LENGTH)
	{
		input_str = input_str.substring(0,AFFL_MAXLOCATION_LENGTH);
	}

	// remove malicious text
	var filter1 = /((\%3C)|<)((\%2F)|\/)*[a-z0-9\%]+(((%20)*|[ ]*)|((%20)+|[ ]+)[^>]+)((\%3E)|>)/gi;
	var beforeValue = input_str;
	var clean_str = beforeValue.replace(filter1, "");
	var int1 = 0;
	while( beforeValue != clean_str && int1 < 500 )
	{
		beforeValue = clean_str;
		clean_str = clean_str.replace(filter1, "");
		int1 ++;
	}

	var filter2 = /[<>\(\)\;\"]|\%3C|\%3E|\%28|\%29|\%3B|\%22/gi; 
	clean_str = clean_str.replace(filter2, "");
	return clean_str;
}


function AFFL_BuildQueryStringList(){
	var name  = '';
	var value = '';
	var flag  = false;

        var sanitized_loc_str = AFFL_Sanitize(location.search);

	for(ct = 1; ct <= sanitized_loc_str.length - 1; ct++){
		ch = sanitized_loc_str.charAt(ct);
		if(ch == '?'){
			flag = false;
			continue;
		}
		if(ch == '='){
			flag = true;
			if(value.length > 0){
				name = value;
			}
			value = '';
			continue;
		}
		if((ch == '&') || (ch == '#')){
			flag = false;
			if((name.length > 0) && (value.length > 0)){
				AFFL_AddString(name, value);
			}
			name  = '';
			value = '';
			continue;
		}
		value += ch;
	}
	if(value.length > 0){
		if(flag == true){
			AFFL_AddString(name, value);
		}
	}
	return 0;
}


function AFFL_GetString(name){
	var value = '';
	
	if((num = AFFL_SearchString(name)) >= 0){
		value = AFFL_Value[num];
	}
	return value;
}

function AFFL_AddString(name, value){
	AFFL_Name[AFFL_Keys]  = name;
	AFFL_Value[AFFL_Keys] = value;
	AFFL_Keys++;
}

function AFFL_SearchString(name){
	var num = -1;
	
	for(ct = 0; ct <= AFFL_Keys; ct++){
		if(name == AFFL_Name[ct]){
			num = ct;
			break;
		}
	}
	return num;
}


function AFFL_SetCookie(cookieKey, cookieValue, httponly, sslonly){
	var str1 = cookieKey + "=" + escape(cookieValue)

	str1 += "; domain=" + AFFL_shinsei_domain

	var d = new Date();
	d.setTime(d.getTime() + (1000*60*60*24*365))

	str1 += "; expires=" + d.toGMTString()

	str1 += "; path=" + AFFL_shinsei_path

	if(httponly == true){
		str1 += ";HttpOnly"
	}

	if(sslonly == true){
		str1 += ";secure"
	}

	window.document.cookie = str1
}

function AFFL_DeleteCookie(cookieKey){
	var str1 = cookieKey + "="
	str1 += "; domain=" + AFFL_shinsei_domain
	str1 += "; expires=Thu, 01-Jan-2000 02:54:39 GMT";

	str1 += "; path=" + AFFL_shinsei_path

	window.document.cookie = str1
}

function AFFL_GetCookie(key){
	var cookieValue = document.cookie;
	var cookieArray = cookieValue.split(";")
	var result = ""

	for(var i = 0; i < cookieArray.length; i++){
		var index = cookieArray[i].indexOf(key + "=")
		if(index >= 0){
			result = cookieArray[i].substring(index + key.length + 1,cookieArray[i].length)
			result = unescape(result)
			break;
		}
	}

	return result;
}


