// contact.js - JavaScript
// Copyright 2004 Sofrosune. All rights reserved.
// Author: Sofrosune; www.sofrosune.net
// No part of this program may be reproduced or transmitted in any form or 
// by any means without permission from the author, Sofrosune.
//
// Date: November 19, 2004.
// Version: 1.00; November 19, 2004.
// Version: 1.02; February 5, 2005.
// Version: 1.03; April 1, 2005.
// Version: 1.04; July 12, 2005. (Added contact_putPhonebook function)
// Version: 1.05; October 11, 2006. (Added job part to message)

// Usage:
/**
<head>
	<script type="text/javascript" src="../scripts/meiden.js"></script>
	<script type="text/javascript" src="../scripts/meiden_data.js"></script>
	<script type="text/javascript" src="../scripts/contact.js"></script>
	<script type="text/javascript" src="../scripts/contact_data.js"></script>
</head>
*/

// Constants:

var kATMARK = unescape("%40"); // an atmark
var kDEFAULT_USERNAME = "mpubre";
var kDEFAULT_MAILDOMAIN = unescape("mb%2Emeidensha%2Eco%2Ejp");

var kCONTACT_MSG_NAME = "About: $$$name$$$\n";
var kCONTACT_MSG_ADRS = "Address: $$$adrs$$$\n";
var kCONTACT_MSG_TEL = "Phone: $$$phone$$$ Fax: $$$fax$$$\n";

var kCONTACT_PHONEBOOK_TMPL = '\
<table border="0" width="100%" cellspacing="0" cellpadding="0">\
<tr>\
<td class="contact_cell_base" align="left" valign="middle" xwidth="192">$$$job$$$</td>\
<td class="contact_cell_base" align="left" valign="middle" xwidth="300">$$$adrs$$$</td>\
<td class="contact_cell_base" align="center" valign="middle" width="160">\
	<table border="0" cellspacing="0" cellpadding="0">\
	<tr>\
	<td>Tel:</td>\
	<td>$$$phone$$$</td>\
	</tr>\
	<tr>\
	<td>Fax:</td>\
	<td>$$$fax$$$</td>\
	</tr>\
	</table>\
</td>\
</tr>\
</table>\
';

// Variables:

//var gContact_Askform_data = [ /* [key,user,name,sect] */
//	['11-101-01','user11','prod-11-101-01','prod11'],
//	['12-102-03','user12','prod-12-102-03','prod12'],
//	null];

//var gContact_Askform2_data = [ /* [user,sect] */
//	['user11','prod11 section'],
//	['user12','prod12 section'],
//	null];

var gContact_Phonebook = [ /* [key,phone,fax,adrs,job] */
//	["key","phone","fax","adrs","job"], 
	null];


// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// contact_cPref
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Usage:
//	var aPref = new contact_cPref();
//	aPref.parse();
//	var key = aPref.key;
//
//	mailform.html#HASH?KEY1=VALUE1&KEY2=VALUE2
//	var paramHash = location.hash; --> "#HASH"
//	var paramArgs = location.search; --> "?KEY1=VALUE1&KEY2=VALUE2"
//	key - a key code, may followed by ":N"
//	user - a user name (optional; default is key without ":" suffix)
//	name - a product name (optional)
//	adrs - an address (optional)
//	phone - a phone number (optional)
//	fax - a fax number (optional)

function contact_cPref() {
	this.key = "";
	this.user = "";
	this.name = "";
	this.job = "";
	this.adrs = "";
	this.phone = "";
	this.fax = "";
	this.comet = "";
	this.msg = "";

	this.parse = function () {

		var paramHash = unescape(location.hash);
		var paramArgs = location.search;

	//	window.alert("hash="+paramHash+"\nargs="+paramArgs);

		if (paramArgs != "") {
			var args = paramArgs.replace(/^\?/,"").split("&");
			for (var i = 0; i < args.length; i++) {
				var tuple = args[i].split("=");
				var key = unescape(tuple[0]);
				var value = unescape(tuple[1]);
				if (key == "key") {
				//	window.alert(value);
					if (value.search(/^\!/) != -1) {
						value = meiden_uudecode(value.replace(/^\!/,""));
					}
				//	window.alert(value);
					this.key = value;
				} else if (key == "user") {
					this.user = value;
				} else if (key == "name") {
					this.name = value;
				} else if (key == "adrs") {
					this.adrs = value;
				} else if (key == "phone") {
					this.phone = value;
				} else if (key == "fax") {
					this.fax = value;
				}
			//	window.alert("key="+key+"\nvalue="+value);
			}
		}

		this.generateComet();
		this.generateMessage();
	}

	this.generateComet = function () {
		// user="!xyz" --> comet=meiden_uudecode(user.replace(/^!/,""))
		// user="abc<ATMARK>def" --> comet=user
		// user="abc" --> comet=user + AT + DOMAIN
		var user = this.user;
		if (user == "") { user = this.key.replace(/:.+$/,""); }
		if (user == "") {
			user = kDEFAULT_USERNAME;
		} else if (user.search(/^\!/) != -1) {
			user = meiden_uudecode(user.replace(/^\!/,""));
		}
		if (user.indexOf(kATMARK) == -1) {
			user += kATMARK + kDEFAULT_MAILDOMAIN;
		}
		this.comet = user;
	//	window.alert("comet="+this.comet);
	}

	this.generateMessage = function () {

		// search for phone book
		var key = this.key;
	//	window.alert(key);
		for (var n = 0; n < gContact_Phonebook.length; n++) {
			var tuple = gContact_Phonebook[n];
			if (tuple == null) { continue; }
			if (key == tuple[0]) {
				if (this.phone == "") { this.phone = tuple[1]; }
				if (this.fax == "") { this.fax = tuple[2]; }
				if (this.adrs == "") { this.adrs = tuple[3]; }
				if (this.job == "") { this.job = tuple[4]; }
			//	window.alert(tuple[3]);
				break;
			}
		}

		// generate message
		var msg = "";
		if (this.name != "") {
			var text = kCONTACT_MSG_NAME.replace("$$$name$$$",this.name);
			msg += text;
		}
		if (this.adrs != "") {
			var text = kCONTACT_MSG_ADRS.replace("$$$adrs$$$",this.adrs);
			msg += text;
		}
		if (this.job != "") {
			var text = kCONTACT_MSG_NAME.replace("$$$name$$$",this.job);
			msg += text;
		}
		if ((this.phone != "") || (this.fax != "")) {
			var text = kCONTACT_MSG_TEL.replace("$$$phone$$$",this.phone);
			text = text.replace("$$$fax$$$",this.fax);
			msg += text;
		}
		this.msg = msg;
	//	window.alert("msg="+this.msg);
	}
}


// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// contact_setup_params
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

function contact_setup_params(formname) {

	var aPref = new contact_cPref();
	aPref.parse();

	var form = eval("document."+formname);

	var fieldText = document.getElementById("PARAM_TEXT_category");
	fieldText.innerHTML = aPref.msg.replace(/\n/g,"<br>\n");

	var fieldText2 = form.elements["PARAM_TEXTAREA_category"];
	fieldText2.value = aPref.job;

//	var fieldText = form.elements["PARAM_TEXTAREA_memo"];
//	fieldText.value = aPref.msg;

	var fieldComet = form.elements["PARAM_HIDDEN_receipt"];
//	var fieldComet = form.elements["PARAM_TEXT_email"];
	fieldComet.value = aPref.comet;

}


// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// contact_putPhonebook
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Usage:
//	contact_putPhonebook(key,user);
//	key - (1) "abc[:suffix]" (2) "abcATMARKdef" (3) "!616263"
//	if user (or decoded user) does not follow ATMARK, it will be filled by default domain.

function contact_putPhonebook(key) {

	// param:key
	if ((key == undefined) || (key == null)) {
		key = "";
	} else if (key.search(/^\!/) != -1) {
		key = meiden_uudecode(key.replace(/^\!/,""));
	}
//	window.alert("key="+key);

	// search for phone book
	var phone, fax, adrs, job;
	for (var n = 0; n < gContact_Phonebook.length; n++) {
		var tuple = gContact_Phonebook[n];
		if (tuple == null) { continue; }
		if (key == tuple[0]) {
			phone = (tuple[1] == "" ? "&nbsp;" : tuple[1]);
			fax = (tuple[2] == "" ? "&nbsp;" : tuple[2]);
			adrs = (tuple[3] == "" ? "&nbsp;" : tuple[3]);
			job = (tuple[4] == "" ? "&nbsp;" : tuple[4]);
			break;
		}
	}
	if (job == "&nbsp;") {
		job = adrs;
		adrs = "&nbsp;"
	}

	var msg = kCONTACT_PHONEBOOK_TMPL;
	msg = msg.replace('$$$phone$$$',phone).replace('$$$fax$$$',fax).replace('$$$adrs$$$',adrs).replace('$$$job$$$',job);

	document.write(msg);
}


// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// contact_askform_debug
// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
// Usage:

function contact_askform_debug() {

	var aPref = new contact_cPref();
	aPref.parse();

}


// end of javascript
