﻿// JavaScript Document

// BOI, followed by one or more digits, followed by EOI.
 var reInteger = /^\d+$/
 
//Validation for integer
function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return false;
       else return (isInteger.arguments[1] == true);

    return reInteger.test(s)
}


// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function getHTTPObject(){
		var ro;
		if (window.XMLHttpRequest)     // Object of the current windows
{ 
       	ro = new XMLHttpRequest();     // Firefox, Safari, ...
} 
else 
if (window.ActiveXObject)   // ActiveX version
{
       ro = new ActiveXObject("Microsoft.XMLHTTP");    // Internet Explorer 
} 
return ro;
}

var http = getHTTPObject(); // We create the HTTP Object

var isWorking = false;

function addToToteBag(prod_id,g_web_root_path, g_lang, offer)
{
	if (!isInteger(document.getElementById('int_quantity_'+prod_id).value))
	{
		alert('Please enter valid quantity');
	}
	else
	{
		url = g_web_root_path+"cart/tote_bag.cfm?action=addtocart&no="+Math.random() +"&int_product_id=" + prod_id + "&int_quantity=" +document.getElementById('int_quantity_'+prod_id).value + "&specialoffer=" +offer+ "&lang=" + g_lang; 
		if (!isWorking && http ) 
		{
			http.open("GET", url, true);
			http.onreadystatechange = handleObjectiveResponse;
			isWorking = true;
			http.send(null);
		}
	}
	return false;
}


/*
function addToToteBag(prod_id,g_web_root_path)
{
	if (!isInteger(document.getElementById('int_quantity_'+prod_id).value))
	{
		alert('Please enter valid quantity');
	}
	else
	{
		url = g_web_root_path+"cart/tote_bag.cfm?action=addtocart&no="+Math.random() +"&int_product_id=" + prod_id + "&int_quantity=" +document.getElementById('int_quantity_'+prod_id).value; 
		if (!isWorking && http ) 
		{
			http.open("GET", url, true);
			http.onreadystatechange = handleObjectiveResponse;
			isWorking = true;
			http.send(null);
		}
	}
	return false;
}
*/


/*
function addToToteBag(prod_id, g_root_path, g_lang,offer) 
{
	if (!isInteger(document.getElementById('int_quantity_'+prod_id).value))
	{
		alert('Please enter valid quantity');
	}
	else
	{
		alert(offer);	
		url = g_web_root_path+"cart/tote_bag.cfm?lang=" + g_lang + "&action=addtocart&no="+Math.random() +"&int_product_id=" + prod_id + "&int_quantity=" +document.getElementById('int_quantity_'+prod_id).value+"&specialoffer=" +offer; 
		if (!isWorking && http ) 
		{
			http.open("GET", url, true);
			http.onreadystatechange = handleObjectiveResponse;
			isWorking = true;
			http.send(null);
		}
	}
	return false;
}
*/







function handleObjectiveResponse() {

	if (http.readyState == 4) 
	{
		
		// Use the XML DOM to unpack the data 
		xmlDocument = http.responseText; 
		var contnt_tote_bag;
		content = xmlDocument.replace('<META NAME="ColdFusionMXEdition" CONTENT="ColdFusion DevNet Edition - Not for Production Use.">','');
		var arrContent = new Array(2);
		arrContent = content.split("||");
		if(arrContent[1] == 'QtyError')
		{
			document.getElementById("success").innerHTML = '在庫数を上回るご注文数です。';
		}
		else
		{
			document.getElementById("success").innerHTML = 'カートに入れました';
		}
		document.getElementById("tote-bag").innerHTML = arrContent[0];
		
		isWorking = false;
	}
}
