function AcceptOrder(item,source_page) {

	var new_data = "";

	if (source_page) {
		// keep track of their page, for 'continue shopping'
		SetCookie('ShoppingPage', source_page, 365, '/', '','' );
	}
	
/* Is our cookie there */	
	if ( !GetCookie( 'cart_data' ) ) {
		/* Nope, nothing there, so start a new one */
		/* ('value' of cookie will be list of items, each one preceded by a carat ...)*/
		/* a 10-digit random string will serve to identify this order when logged on the server later */
		new_data = randomString() + "^1*" + item;
		SetCookie('cart_data', new_data, 365, '/', '','' );
	} else {
		new_data = UpdateCookie(item);
	}
	UpdateLinks(item);
}



function CheckForCookie () {

	var cookie_value = GetCookie( 'cart_data' );
	if ( cookie_value ) {
		/* Yep ... one there, so write the shopping cart links ... */
		UpdateLinks (cookie_value) ;
	}
}




function SetCookie( name, value, expires, path, domain, secure ) {
	// time is in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}



function UpdateCookie(item) {

// get the cookie
	var cookie_value = GetCookie( 'cart_data' );

// update the data string ... 
	new_data = cookie_value + '^1*'+ item;

// then save it ...
	SetCookie('cart_data', new_data, 365, '/', '','' );
	return new_data;
	
}



function UpdateLinks (item_number) {

	var newHTML = "<span id=\"shopping_cart_link\" name=\"shopping_cart_link\" style=\"float:right;margin:3px 5px 0 0;\"><a href=\"../shopping_cart/shopping_cart.php5ja\"  target=\"main_iframe\"><img src=\"../images/basket_35_full.png\" border=\"0\" onClick=\"top.switch_tabs('shopping_cart');\" /\"></a></span><p><a href=\"../shopping_cart/shopping_cart.php5ja\"  target=\"main_iframe\">カゴの中を見る ...</a></p>";

	if (item_number != '0099') {
		/* show the 'Basket is active' image  */	
		top.main_iframe.nav_box_iframe.document.getElementById('cart_notify').innerHTML = newHTML;
		
	}

}



function GetCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}				
					

function CheckAddressErrors() {
// read values from address form, and check for problems.
// If any found, send alert message, flag the wrong section in red, and wait for a retry

	var found_errors = false;
	
// clear the error display field (in case we are going around again ...)
//	top.main_iframe.document.getElementById('error_display').style.display = "none";

// run through the list of required fields

	found_errors = isFieldEmpty(top.main_iframe.document.address_data.lastname, "「姓」を入力ください");
	if (found_errors) { return; }
	found_errors = isFieldEmpty(top.main_iframe.document.address_data.addy_line_one, "「住所」を入力ください");
	if (found_errors) { return; }
	found_errors = isFieldEmpty(top.main_iframe.document.address_data.postcode, "「郵便番号」を入力ください");
	if (found_errors) { return; }
	found_errors = isFieldEmpty(top.main_iframe.document.address_data.email, "「メールアドレス」を入力ください");
	if (found_errors) { return; }

	if (top.main_iframe.document.address_data.email.value == top.main_iframe.document.address_data.email_confirm.value) {
		// do nothing
	} else {
		top.main_iframe.document.getElementById('error_display').innerHTML = "二つのメールアドレスが一致しません";
		top.main_iframe.document.getElementById('error_display').style.display = "block";
		found_errors = true;
	}
	if (found_errors) { return; }

	var index0 = top.main_iframe.document.address_data.prefecture.selectedIndex;
	if (top.main_iframe.document.address_data.prefecture.options[index0].value == "都道府県を選ぶ") {
		top.main_iframe.document.getElementById('error_display').innerHTML = "都道府県を選んでください";
		top.main_iframe.document.getElementById('error_display').style.display = "block";
		found_errors = true;
	}
	if (found_errors) { return; }
	
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
		if (top.main_iframe.document.address_data.email.value.match(illegalChars)) {
		top.main_iframe.document.getElementById('error_display').innerHTML = "The email address contains incorrect characters";
		top.main_iframe.document.getElementById('error_display').style.display = "block";
		found_errors = true;
	}
	if (found_errors) { return; }


// if OK, then process the order ... 
	if (!found_errors) {

	top.main_iframe.document.getElementById('error_display').innerHTML = "OK ... moving to next page ...";

// put the address data into the final of the three cookies
	var encoded_stuff = "";
	var addy_data = "";
	
	encoded_stuff = convertChar2CP(top.main_iframe.document.address_data.lastname.value)
	addy_data = addy_data + encoded_stuff + "^"

	encoded_stuff = convertChar2CP(top.main_iframe.document.address_data.firstname.value)
	addy_data = addy_data + encoded_stuff + "^"

	encoded_stuff = convertChar2CP(top.main_iframe.document.address_data.email.value)
	addy_data = addy_data + encoded_stuff + "^"

	encoded_stuff = convertChar2CP(top.main_iframe.document.address_data.postcode.value)
	addy_data = addy_data + encoded_stuff + "^"

	encoded_stuff = convertChar2CP(top.main_iframe.document.address_data.prefecture.value)
	addy_data = addy_data + encoded_stuff + "^"

	encoded_stuff = convertChar2CP(top.main_iframe.document.address_data.addy_line_one.value)
	addy_data = addy_data + encoded_stuff + "^"

	encoded_stuff = convertChar2CP(top.main_iframe.document.address_data.addy_line_two.value)
	addy_data = addy_data + encoded_stuff + "^"

	encoded_stuff = convertChar2CP(top.main_iframe.document.address_data.addy_line_three.value)
	addy_data = addy_data + encoded_stuff
	
	SetCookie('addy_data', addy_data, 365, '/', '','' );

// finally, redirect them to the last page of the shopping cart

// Update: unfortunately, this next line doesn't seem to work in some Explorer versions, so
// send them back to the main page itself, and do the redirect from there ...
//	top.main_iframe.location = "http://mokuhan.com/shopping_cart/shopping_cart_04.php5ja";
	return found_errors;

	}

// Note: this could also be done by using this script to 'press' the submit button of the form,
// if I wanted the data to be posted to the next page ...
// e.g.    top.main_iframe.document.address_data.submit();

}


function CheckContactForm() {
// read values from form, and check for problems.
// If any found, send alert message, flag the wrong section in red, and wait for a retry

	var found_errors = false;
	
// run through the list of required fields

	found_errors = isFieldEmpty(top.main_iframe.document.contact_form.name, "「御名前」を入力ください");
	if (found_errors) { return; }
	found_errors = isFieldEmpty(top.main_iframe.document.contact_form.comments_field, "「コメント」を入力ください");
	if (found_errors) { return; }
	found_errors = isFieldEmpty(top.main_iframe.document.contact_form.email, "「メールアドレス」を入力ください");
	if (found_errors) { return; }

	if (top.main_iframe.document.contact_form.email.value == top.main_iframe.document.contact_form.email_confirm.value) {
		// do nothing
	} else {
		top.main_iframe.document.getElementById('error_display').innerHTML = "二つのメールアドレスが一致しません";
		top.main_iframe.document.getElementById('error_display').style.display = "block";
		found_errors = true;
	}
	if (found_errors) { return; }
	
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
		if (top.main_iframe.document.contact_form.email.value.match(illegalChars)) {
		top.main_iframe.document.getElementById('error_display').innerHTML = "The email address contains incorrect characters";
		top.main_iframe.document.getElementById('error_display').style.display = "block";
		found_errors = true;
	}
	if (found_errors) { return; }


// if OK, then process the order ... 
	if (!found_errors) {
		top.main_iframe.document.getElementById('error_display').innerHTML = "OK ... moving to next page ...";
		top.main_iframe.document.contact_form.submit();
	}
}


function CheckAffiliateForm() {
// read values from form, and check for problems.
// If any found, send alert message, flag the wrong section in red, and wait for a retry

	var found_errors = false;
	
// run through the list of required fields

	found_errors = isAffiliateFieldEmpty(top.main_iframe.about_iframe.document.affiliate_register.name, "「御名前」を入力ください");
	if (found_errors) { return; }
	found_errors = isAffiliateFieldEmpty(top.main_iframe.about_iframe.document.affiliate_register.email, "「メールアドレス」を入力ください");
	if (found_errors) { return; }

	if (top.main_iframe.about_iframe.document.affiliate_register.email.value == top.main_iframe.about_iframe.document.affiliate_register.email_confirm.value) {
		// do nothing
	} else {
		top.main_iframe.about_iframe.document.getElementById('error_display').innerHTML = "二つのメールアドレスが一致しません";
		top.main_iframe.about_iframe.document.getElementById('error_display').style.display = "block";
		found_errors = true;
	}
	if (found_errors) { return; }
	
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
		if (top.main_iframe.about_iframe.document.affiliate_register.email.value.match(illegalChars)) {
		top.main_iframe.about_iframe.document.getElementById('error_display').innerHTML = "The email address contains incorrect characters";
		top.main_iframe.about_iframe.document.getElementById('error_display').style.display = "block";
		found_errors = true;
	}
	if (found_errors) { return; }


// if OK, then process the order ... 
	if (!found_errors) {
		top.main_iframe.about_iframe.document.getElementById('error_display').innerHTML = "OK ... moving to next page ...";
		top.main_iframe.about_iframe.document.affiliate_register.submit();
	}
	
	function isAffiliateFieldEmpty(test_field, error_message) {
	var error = false;
 
	if (test_field.value.length == 0) {
		top.main_iframe.about_iframe.document.getElementById('error_display').innerHTML = error_message;
		top.main_iframe.about_iframe.document.getElementById('error_display').style.display = "block";
		error = true;
        } else {
		// do nothing
	}
	return error;  
}

}


function SubmitOrder() {
	// check to see if there is a subscription, and if so, have they agreed to the terms
	if (top.main_iframe.document.checked_data.agree_terms) {
		// the checkbox is there, so there must be a subscription item
		if (top.main_iframe.document.checked_data.agree_terms.checked == true) {
			// send the form on its way
			top.main_iframe.document.checked_data.submit();
		} else {
			top.main_iframe.document.getElementById('error_display').innerHTML = "分割購入に関する約束に同意のチェックを入れてください";
			top.main_iframe.document.getElementById('error_display').style.display = "block";
			top.main_iframe.document.getElementById('red_text').style.color = "#ff0000";
		}
	} else {
		top.main_iframe.document.checked_data.submit();	
	}
}



function FinishOrder() {
	DeleteCookie('cart_data', '/', 'mokuhan.com');
	UpdateLinks('');
	item = "";  new_data=""; cookie_value=""; item_list="";
}


function PaySelect() {
	// if they have selected COD, then force shipping method to be by takkyubin
if (top.main_iframe.document.choose_pay_method.pay_method[0].checked == true) {
	top.main_iframe.document.getElementById('radio_post').style.color = "#bbb";
	top.main_iframe.document.choose_pay_method.ship_method[1].checked	 = true;
	} else {
	// they might be putting it back ...
	top.main_iframe.document.getElementById('radio_post').style.color = "#333";
	}
}

function ShipSelect() {
	// if they are selecting post office shipping, then they can't have takkyubin COD
if (top.main_iframe.document.choose_pay_method.pay_method[0].checked == true) {
	 window.alert("お支払い方法に「代金引き換え」をお選びなので、\n発送は自動的に宅急便になります。"); 
	top.main_iframe.document.choose_pay_method.ship_method[1].checked	 = true;
	top.main_iframe.document.choose_pay_method.ship_method[0].checked	 = false;
	} 
}

	
function DeleteCookie(name, path, domain) {
	if ( GetCookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function randomString() {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
	var string_length = 10;
	var randomstring = '';
	for (var i=0; i<string_length; i++) {
	var rnum = Math.floor(Math.random() * chars.length);
		randomstring += chars.substring(rnum,rnum+1);
	}
	return randomstring;
}


function isFieldEmpty(test_field, error_message) {
	var error = false;
 
	if (test_field.value.length == 0) {
		top.main_iframe.document.getElementById('error_display').innerHTML = error_message;
		top.main_iframe.document.getElementById('error_display').style.display = "block";
		error = true;
        } else {
		// do nothing
	}
	return error;  
}




// below here are the conversion functions to allow Japanese to be stored in the cookies ...

var debug1 = true;
var debug2 = true;
var escapeMap = '';
var CPstring = '';


var hexNum = { 0:1, 1:1, 2:1, 3:1, 4:1, 5:1, 6:1, 7:1, 8:1, 9:1, 
				A:1, B:1, C:1, D:1, E:1, F:1, 
				a:1, b:1, c:1, d:1, e:1, f:1 };
var jEscape = { 0:1, b:1, t:1, n:1, v:1, f:1, r:1 };
var decDigit = { 0:1, 1:1, 2:1, 3:1, 4:1, 5:1, 6:1, 7:1, 8:1, 9:1 };


function dec2hex ( textString ) {
 return (textString+0).toString(16).toUpperCase();
}

function  dec2hex2 ( textString ) {
  var hexequiv = new Array ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
  return hexequiv[(textString >> 4) & 0xF] + hexequiv[textString & 0xF];
}

function  dec2hex4 ( textString ) {
  var hexequiv = new Array ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
  return hexequiv[(textString >> 12) & 0xF] + hexequiv[(textString >> 8) & 0xF] + hexequiv[(textString >> 4) & 0xF] + hexequiv[textString & 0xF];
}

function getCPfromChar ( textString ) {
	// converts a character or sequence of characters to hex codepoint values
	// copes with supplementary characters
	// returned values include a space between each hex value and at the end
	var codepoint = "";
	var haut = 0;
	var n = 0; 
	for (var i = 0; i < textString.length; i++) {
		var b = textString.charCodeAt(i); 
		if (b < 0 || b > 0xFFFF) {
			codepoint += 'Error: Initial byte out of range in getCPfromChar: '+dec2hex(b);
			}
		if (haut != 0) { // we should be dealing with the second part of a supplementary character
			if (0xDC00 <= b && b <= 0xDFFF) {
				codepoint += dec2hex(0x10000 + ((haut - 0xD800) << 10) + (b - 0xDC00)) + ' ';
				haut = 0;
				continue;
				}
			else {
				codepoint += 'Error: Second byte out of range in getCPfromChar: '+dec2hex(haut);
				haut = 0;
				}
			}
		if (0xD800 <= b && b <= 0xDBFF) { //b is the first part of a supplementary character
			haut = b;
			}
		else { // this is not a supplementary character
//			codepoint += dec2hex(b);
			codepoint += b.toString(16).toUpperCase()+' ';
			}
		} 
 //alert('>'+codepoint+'<');
	return codepoint;
	}





// ================ CP to XX ============================

function convertCP2Char ( textString ) {
  var outputString = '';
  textString = textString.replace(/^\s+/, '');
  if (textString.length == 0) { return ""; }
  	textString = textString.replace(/\s+/g, ' ');
  var listArray = textString.split(' ');
  for ( var i = 0; i < listArray.length; i++ ) {
    var n = parseInt(listArray[i], 16);
    if (n <= 0xFFFF) {
      outputString += String.fromCharCode(n);
    } else if (n <= 0x10FFFF) {
      n -= 0x10000
      outputString += String.fromCharCode(0xD800 | (n >> 10)) + String.fromCharCode(0xDC00 | (n & 0x3FF));
    } else {
      outputString += 'convertCP2Char error: Code point out of range: '+dec2hex(n);
    }
  }
  return( outputString );
}



	


function convertCP2pEsc ( textString ) {
	// textstring: sequence of Unicode code points, derived from convertChar2CP()
	var outputString = "";
	// remove initial spaces
	textString = textString.replace(/^\s+/, '');
	if (textString.length == 0) { return ""; }
	// make all multiple spaces a single space
	textString = textString.replace(/\s+/g, ' ');
	var listArray = textString.split(' ');
	// process each codepoint
	for ( var i = 0; i < listArray.length; i++ ) {
		var n = parseInt(listArray[i], 16);
		//if (i > 0) { outputString += ' ';}
		if (n == 0x20) { outputString += '%20'; }
		else if (n >= 0x41 && n <= 0x5A) { outputString += String.fromCharCode(n); } // alpha
		else if (n >= 0x61 && n <= 0x7A) { outputString += String.fromCharCode(n); } // alpha
		else if (n >= 0x30 && n <= 0x39) { outputString += String.fromCharCode(n); } // digits
		else if (n == 0x2D || n == 0x2E || n == 0x5F || n == 0x7E) { outputString += String.fromCharCode(n); } // - . _ ~
		else if (n <= 0x7F) { outputString += '%'+dec2hex2(n); }
		else if (n <= 0x7FF) { outputString += '%'+dec2hex2(0xC0 | ((n>>6) & 0x1F)) + '%' + dec2hex2(0x80 | (n & 0x3F)); } 
		else if (n <= 0xFFFF) { outputString += '%'+dec2hex2(0xE0 | ((n>>12) & 0x0F)) + '%' + dec2hex2(0x80 | ((n>>6) & 0x3F)) + '%' + dec2hex2(0x80 | (n & 0x3F)); } 
		else if (n <= 0x10FFFF) {outputString += '%'+dec2hex2(0xF0 | ((n>>18) & 0x07)) + '%' + dec2hex2(0x80 | ((n>>12) & 0x3F)) + '%' + dec2hex2(0x80 | ((n>>6) & 0x3F)) + '%' + dec2hex2(0x80 | (n & 0x3F)); } 
		else { outputString += '!Error ' + dec2hex(n) +'!'; }
		}
		return( outputString );
	}






// ================ XX to CP  ============================

function convertChar2CP ( textString ) { 
	var haut = 0;
	var n = 0;
	CPstring = '';
	for (var i = 0; i < textString.length; i++) {
		var b = textString.charCodeAt(i); 
		if (b < 0 || b > 0xFFFF) {
			CPstring += 'Error ' + dec2hex(b) + '!';
			}
		if (haut != 0) {
			if (0xDC00 <= b && b <= 0xDFFF) {
				CPstring += dec2hex(0x10000 + ((haut - 0xD800) << 10) + (b - 0xDC00)) + ' ';
				haut = 0;
				continue;
				}
			else {
				CPstring += '!erreur ' + dec2hex(haut) + '!';
				haut = 0;
				}
			}
		if (0xD800 <= b && b <= 0xDBFF) {
			haut = b;
			}
		else {
			CPstring += dec2hex(b) + ' ';
			}
		}
	CPstring = CPstring.substring(0, CPstring.length-1);

//	pEsc.value = convertCP2pEsc( CPstring );
	return (convertCP2pEsc( CPstring ));

	}




function convertpEsc2CP ( textString ) {
	// textstring: sequence of percent-escaped text
  CPstring = '';
	var outputString = "";
	var compte = 0;
	var n = 0;
	// remove all leading spaces
	textString = textString.replace(/^\s+/, '');
	if (textString.length == 0) { return ""; }
	// normalize all multiple spaces to a single space - note: there shouldn't be any spaces!
	textString = textString.replace(/\s+/g, ' ');
	// convert the whole string to percent escaped forms (to reduce work in coding)
	for ( var j = 0; j < textString.length; j++ ) {
		if (textString.charAt(j) == '%') { outputString += textString.slice(j, j+3); j += 2; }
		else { outputString += '%'+dec2hex(textString.charCodeAt(j)); }
		}
	textString = outputString; outputString = '';
	var listArray = textString.split('%');
	for ( var i = 1; i < listArray.length; i++ ) { // runs from 1 to eliminate first % (produces null array item)
		var b = parseInt(listArray[i], 16);   // alert('b:'+dec2hex(b));
		switch (compte) {
			case 0:
			if (0 <= b && b <= 0x7F) {  // 0xxxxxxx
				outputString += dec2hex(b) + ' '; } 
			else if (0xC0 <= b && b <= 0xDF) {  // 110xxxxx
				compte = 1;
				n = b & 0x1F; }
			else if (0xE0 <= b && b <= 0xEF) {  // 1110xxxx
				compte = 2;
				n = b & 0xF; } 
			else if (0xF0 <= b && b <= 0xF7) {  // 11110xxx
				compte = 3;
				n = b & 0x7; } 
			else {
				outputString += '!erreur ' + dec2hex(b) + '! ';
				}
			break;
			case 1:
			if (b < 0x80 || b > 0xBF) {
				outputString += '!erreur ' + dec2hex(b) + '! ';
				}
			compte--;
			outputString += dec2hex((n << 6) | (b-0x80)) + ' ';
			n = 0;
			break;
			case 2: case 3:
			if (b < 0x80 || b > 0xBF) {
				outputString += '!erreur ' + dec2hex(b) + '! ';
				}
			n = (n << 6) | (b-0x80);
			compte--;
			break;
		}
	}
	CPstring = outputString.replace(/ $/, '');
	
//	chars.value = convertCP2Char( CPstring );
	return (convertCP2Char( CPstring ));

	}

	
