var Cookie = {
	set: function(name, value, daysToExpire) {
		var expire = '';
		if (daysToExpire != undefined) {
			var d = new Date();
			d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
			expire = '; expires=' + d.toGMTString();
		}
		path = '; path=/';
		domainArray = document.domain.split(".");
		if (domainArray[0] == 'www') {
			domainArray[0] = "";
		}
		domain = domainArray.join(".");
		domain = '; domain='+domain;
		cookieString = escape(name) + '=' + escape(value || '') + expire + path + domain;
		return (document.cookie = cookieString);
	}, 
	get: function(name) {
		var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]*)'));
		return (cookie ? unescape(cookie[2]) : null);
	},
	erase: function(name) {
		var cookie = Cookie.get(name) || true;
		Cookie.set(name, '', -1);
		return cookie;
	},
	accept: function() {
		if (typeof navigator.cookieEnabled == 'boolean') {
			return navigator.cookieEnabled;
		}
		Cookie.set('_test', '1');
		return (Cookie.erase('_test') === '1');
	}
};

function showItem(img) {
	$('preview_img_car').src = "";
	$('preview_img_pic').src = "";
	$('preview_img_car').src = itemList[img.id][0];
	$('preview_img_pic').src = itemList[img.id][1];
	$('preview_header').innerHTML = itemList[img.id][2];
	$('preview_description').innerHTML = itemList[img.id][3];
	
	$('itemDisplayBox').style.visibility = 'visible';
	$('itemDisplayBox').style.display = '';

	$('itemDisplayBox').style.position = "absolute";
	$('itemDisplayBox').style.top = "250px";
	$('itemDisplayBox').style.right = "130px";
	
	$('itemDisplayBox').style.zIndex = "100";	
}

function hideItem(img) {
	$('itemDisplayBox').style.visibility = 'hidden';
	$('itemDisplayBox').style.display = 'none';
}

function changeAmount(direction) {
	if (direction == 1) {
		if ($('description_selected_product_Amount').value < 1 || $('description_selected_product_Amount').value == "" ) {
			$('description_selected_product_Amount').value = 1; 
		} else {
			$('description_selected_product_Amount').value++;
		}
	} else {
		if ($('description_selected_product_Amount').value < 1 || $('description_selected_product_Amount').value == 1) {
			$('description_selected_product_Amount').value = 1; 
		} else {
			$('description_selected_product_Amount').value--;
		}
	}
}

function stringValidation(string, return_invalid_chars) {
	valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	invalid_chars = '';
	
	if(string == null || string == '') return(true);

	for(index = 0; index < string.length; index++) {
		char = string.substr(index, 1);
		if(valid_chars.indexOf(char) == -1) {
			if(invalid_chars.indexOf(char) == -1) {
				if(invalid_chars == '')
					invalid_chars += char;
				else
					invalid_chars += ', ' + char;
			}
		}
	}
	
	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 Validate_Email_Address(str) {
	// trim starting / ending whitespace
	str = str.replace(/^\s*/, "");
	str = str.replace(/\s*$/, "");
	
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)

	if (str.indexOf(at)==-1) return false
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) return false
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) return false
	if (str.indexOf(at,(lat+1))!=-1) return false
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) return false
	if (str.indexOf(dot,(lat+2))==-1) return false
	if (str.indexOf(" ")!=-1) return false
	
	user_name = str.substr(0, lat);
	domain_name = str.substr(lat + 1, str.length);
	if(stringValidation(user_name) === false || stringValidation(domain_name) === false) return(false);
 	return true;
}
 
function navbx(direction, step) {
	if (typeof step == "undefined") {
    	step = 1;
  	}

	if (bxLst.length > maxShown) {
		var go = false;
		if (direction == 'up') {
			fprdbx = fprdbx + step;
			while (go == false) {
				if (bxLst[fprdbx]) {
					go = true;
				} else {
					fprdbx--;
				}
			}
			if (bxLst.length - fprdbx < step) {
				if ($('navbx_up')) 	$('navbx_up').style.visibility = 'hidden';
				if ($('navbx_down')) $('navbx_down').style.visibility = 'visible';
			} else {
				if ($('navbx_up')) $('navbx_up').style.visibility = 'visible';
				if ($('navbx_down') && fprdbx > 0) $('navbx_down').style.visibility = 'visible';
			}
		} else if (direction == 'down') {
			if (fprdbx - step < 0) {
				fprdbx = 0;
			} else {
				fprdbx = fprdbx - step;
			}
			go = true;
			
			if (fprdbx == 0) {
				if ($('navbx_down')) $('navbx_down').style.visibility = 'hidden';
				if ($('navbx_up')) $('navbx_up').style.visibility = 'visible';
			} else {
				if ($('navbx_up')) $('navbx_up').style.visibility = 'visible';
				if ($('navbx_down')) $('navbx_down').style.visibility = 'visible';
			}
		}

		if (go) {
			lprdbx = fprdbx + maxShown - 1;

			for (var i = 0; i < fprdbx; i++) {
				$('bx'+bxLst[i]).style.display = 'none';
			}
	
			for (var i = fprdbx; i < lprdbx+1; i++) {
				if ($('bx'+bxLst[i])) {
					$('bx'+bxLst[i]).style.display = 'inline';
				}
			}

			for (var i = lprdbx + 1; i < bxLst.length; i++) {
				if ($('bx'+bxLst[i])) {
					$('bx'+bxLst[i]).style.display = 'none';
				}
			}
		}				
	}
}

function checkidnum(attr){
	var idnum = attr.value;
	
	if (idnum != "") {
		var v = "0123456789";
		var w = "";
		
		for (i=0; i < idnum.length; i++) {
			x = idnum.charAt(i);
			if (v.indexOf(x,0) != -1)
			w += x;
		}
		attr.value = w;
		idnum = w;
		while (idnum.length < 9){
			idnum="0"+idnum;
		}
		
		var oneortwo = 2;
		var sumval = 0;
		var i = 0;
		while (i <= 8) {
			oneortwo = (oneortwo == 2)?1:2;
			
			var diggit = idnum.substr(i,1)*oneortwo;
			diggit = (diggit>9)?((diggit%10)+1):diggit;

			sumval = parseInt(sumval) + parseInt(diggit);
			i++;
		}

		sumval=sumval%10
		if (sumval > 0){
			attr.style.borderColor = "red";
			return false;
		} else {
			attr.style.borderColor = "";
			return true;
		}
	} else {
		attr.style.borderColor = "red";
		return false;					
	}
}

function urlEncode(u)
{
    u = u.toString();
    var matches = u.match(/[\x90-\xFF]/g);
    if (matches)
    {
        for (var mid = 0; mid < matches.length; mid++)
        {
            var char_code = matches[mid].charCodeAt(0);
            u = u.replace(matches[mid], '%u00' + (char_code & 0xFF).toString(16).toUpperCase());
        }
    }

    return escape(u).replace(/\+/g, "%2B");
}
function isNumberKey(evt) {
	var charCode = (evt.which) ? evt.which : evt.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	return true;
}