//************************************************************************
//        Author: Clif Collins				Date: July 2007
//------------------------------------------------------------------------
//
// 		Copyright (c) 2008 Clifford L. Collins 
// 		All rights are reserved 
//
//************************************************************************

//============================================================
//                          common.js
//============================================================
var server = null;
var hostUrl = '';

var xmlhttp = null;
var xmldom = null;

//==========================================================================================
//                                   xmldoc$
//==========================================================================================
function xmldom$init()
{
	var parser;
	
	if (xmldom != null) return xmldom;

	try { xmldom = new ActiveXObject("MSXML2.DOMDocument"); return xmldom; } catch (e) {}
	try { parser = new DOMParser(); xmldom = parser.parseFromString(xml, "text/xml"); return xmldom; } catch (e) {}

	throw new Error( "This browser does not support XMLDom Object" );
}
//==========================================================================================
//                                   xmlhttp$
//==========================================================================================
function xmlhttp$init()
{

	if (xmlhttp != null) return xmlhttp;

	try { xmlhttp = new XMLHttpRequest(); return xmlhttp; } catch (e) {}
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.6.0"); return xmlhttp; } catch (e) {}
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); return xmlhttp; } catch (e) {}
	try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); return xmlhttp; } catch (e) {}
	try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); return xmlhttp; } catch (e) {}
	try { xmlhttp = window.createRequest(); return xmlhttp; } catch (e) {}

	throw new Error( "This browser does not support XMLHttpRequest" );
}

//=================================================================
//                         local
//=================================================================
function local()
{
	if (hostUrl.indexOf('/clif/') > 0) return true;
	return false;
}
//=================================================================
//                         toHex_A
//=================================================================
function toHex_A(c)
{
	if (c < 0) return '?';
	if (c < 10) return '' + c;
	if (c == '10') return 'A';
	if (c == '11') return 'B';
	if (c == '12') return 'C';
	if (c == '13') return 'D';
	if (c == '14') return 'E';
	if (c == '15') return 'F';	
	return '?';
}
//=================================================================
//                         toHex
//=================================================================
function toHex(c)
{
	var n,m;

	n = Math.floor(c / 16);
	m = c % 16;
	
	return (toHex_A(n) + toHex_A(m));
}
//=================================================================
//                         ekey
//=================================================================
function ekey(n)
{
	var j;

	j = n % 5;
	if (j === 0) return 57;
	if (j === 1) return 48;
	if (j === 2) return 18;
	if (j === 3) return 35;
	if (j === 4) return 29;
	return 0;
}
//=================================================================
//                         xexcape
//=================================================================
function xescape(text)
{
	var e,c,i;

	e = '';
	t = '';
	for (i = 0; i < text.length; i++)
	{
		k = text.charCodeAt(i);
		c = k ^ ekey(i);
		t += 'k = ' + k + ' c = ' + c + '\r\n';
		e += toHex(c);
	}

	return e;
}
//========================================================
//                       sendPassword
//========================================================
function sendPassword(A)
{
	var cmd,i,A;

	initilize();

	A = document.getElementsByName('A')[0];
	
	text = "" + A.value;
	if (text == "")
	{
		alert("Account Name is Required")
		A.focus();
		return;
	}
	if (text.length > 128) 
	{
		alert("Invalid Account Name");
		A.focus();
		return;
	}

	if (! confirm('Send password to your email address? ')) return;

	cmd = "command=SendPassword&A=" + A.value;
	if (! server.executeW(cmd)) return;

	i = server.text.indexOf('email=');
	alert("Password was sent to: " + server.text.substr(i+6));
}
//========================================================
//                       sendPasswordEmployee
//========================================================
function sendPasswordEmployee(A)
{
	var cmd,i,D,E;

	initilize();

	D = document.getElementsByName('D')[0];
	E = document.getElementsByName('E')[0];
	
	text = "" + D.value;
	if (text == "")
	{
		alert("Account Name is Required")
		D.focus();
		return;
	}
	if (text.length > 48) 
	{
		alert("Invalid Account Name");
		D.focus();
		return;
	}

	E = document.getElementsByName('E')[0];
	
	text = "" + E.value;
	if (text == "")
	{
		alert("Employee Name is Required")
		E.focus();
		return;
	}
	if (text.length > 128) 
	{
		alert("Invalid Employee Name");
		E.focus();
		return;
	}

	cmd = "command=AccountUrl&A=" + D.value;
	server.execute(cmd);
	url = server.text;
	if (local()) url = 'http://clif/free/';
	
	if (! confirm('Send Employee password to his/her email address? ')) return;

	cmd = "command=SendPassword&C=" + D.value + "&A=" + E.value;
	if (! server.executeW_A(cmd,url + 'Receive.asp')) return;

	i = server.text.indexOf('email=');
	alert("Password Sent to: " + server.text.substr(i+6));
}
//========================================================
//                       sendPasswordCustomer
//========================================================
function sendPasswordCustomer(A)
{
	var cmd,i,c1,c2;

	initilize();

	c1 = document.getElementsByName('C1')[0];
	c2 = document.getElementsByName('C2')[0];
	
	text = "" + c1.value;
	if (text == "")
	{
		alert("Account Name is Required")
		c1.focus();
		return;
	}
	if (text.length > 48) 
	{
		alert("Invalid Account Name");
		c1.focus();
		return;
	}

	c2 = document.getElementsByName('C1')[0];
	
	text = "" + c2.value;
	if (text == "")
	{
		alert("Customer Name is Required")
		c2.focus();
		return;
	}
	if (text.length > 128) 
	{
		alert("Invalid Customer Name");
		c2.focus();
		return;
	}

	cmd = "command=AccountUrl&A=" + c1.value;
	server.execute(cmd);
	url = server.text;
	if (local()) url = 'http://clif/free/';
	
	if (! confirm('Send Customer password to his/her email address? ')) return;

	cmd = "command=SendPassword&C=" + c1.value + "&A=" + c2.value;
	if (! server.executeW_A(cmd,url + 'Receive.asp')) return;

	i = server.text.indexOf('email=');
	alert("Password Sent to: " + server.text.substr(i+6));
}

//========================================================
//                       login
//========================================================
function login(A,B)
{
	var cmd,A,B;
	var h,w,url,text;
	
	initilize();

	A = document.getElementsByName('A')[0];
	B = document.getElementsByName('B')[0];

	text = "" + A.value;
	if (text == "")
	{
		alert("Account Name is Required")
		return;
	}
	
	if (text.length > 48)
	{
		alert("Invalid Account Name");
		A.focus();
		return;
	}
	text = "" + B.value;
	if (text == "")
	{
		alert("Password is Required")
		B.focus();
		return;
	}

	if (text.length > 12)
	{
		alert("Invalid Password")
		B.focus();
		return;
	}
	
	cmd = "command=LoginAccount&A=" + A.value + "&B=" + B.value;

	if (! server.executeW(cmd)) return;

	B.value = '';
	A.value = '';

	url = hostUrl + 'Receive.asp?command=showform&name=m019_Account';
	window.location.href = url;
}
//========================================================
//                       loginEmployee
//========================================================
function loginEmployee()
{
	var cmd,D,E,F;
	var h,w,url,text;

	initilize();
	
	D = document.getElementsByName('D')[0];
	E = document.getElementsByName('E')[0];
	F = document.getElementsByName('F')[0];

	text = "" + D.value;
	if (text == "")
	{
		alert("Account Name is Required")
		D.focus();
		return;
	}
	
	if (text.length > 48)
	{
		alert("Invalid Account Name");
		D.focus();
		return;
	}

	text = "" + E.value;
	if (text == "")
	{
		alert("Employee Name is Required")
		E.focus();
		return;
	}
	
	if (text.length > 128)
	{
		alert("Invalid Employee Name");
		E.focus();
		return;
	}

	text = "" + F.value;
	if (text == "")
	{
		alert("Password is Required")
		F.focus();
		return;
	}
	
	if (text.length < 4 || text.length > 12)
	{
		alert("Invalid PAssword");
		F.focus();
		return;
	}
	
	cmd = "command=AccountUrl&A=" + D.value;
	server.execute(cmd);
	url = server.text;
	if (local()) url = 'http://clif/free/';

	cmd = "command=Login&A=" + E.value + "&B=" + F.value+ "&C=" + D.value;
	if (! server.executeW_A(cmd,url + 'Receive.asp')) return;

	D.value = '';
	E.value = '';
	F.value = '';

	i = server.text.indexOf('email=');
	
	window.location.href = url + 'Receive.asp?command=showform&name=m005_Main';

}
//========================================================
//                       loginCustomer
//========================================================
function loginCustomer()
{
	var cmd,c1,c2,c3;
	var h,w,url,text;

	initilize();
	
	c1 = document.getElementsByName('C1')[0];
	c2 = document.getElementsByName('C2')[0];
	c3 = document.getElementsByName('C3')[0];

	text = "" + c1.value;
	if (text == "")
	{
		alert("Account Name is Required")
		c1.focus();
		return;
	}
	
	if (text.length > 48)
	{
		alert("Invalid Account Name");
		c1.focus();
		return;
	}

	text = "" + c2.value;
	if (text == "")
	{
		alert("Customer Name is Required")
		c2.focus();
		return;
	}
	
	if (text.length > 128)
	{
		alert("Invalid Customer Name");
		c2.focus();
		return;
	}

	text = "" + c3.value;
	if (text == "")
	{
		alert("Password is Required")
		c3.focus();
		return;
	}
	
	if (text.length < 4 || text.length > 12)
	{
		alert("Invalid PAssword");
		c3.focus();
		return;
	}
	
	cmd = "command=AccountUrl&A=" + D.value;
	server.execute(cmd);
	url = server.text;
	if (local()) url = 'http://clif/free/';

	cmd = "command=LoginCustomer&A=" + c2.value + "&B=" + c3.value+ "&C=" + c1.value;
	if (! server.executeW_A(cmd,url + 'Receive.asp')) return;

	c1.value = '';
	c2.value = '';
	c3.value = '';

	i = server.text.indexOf('email=');
	
	window.location.href = url + 'Receive.asp?command=showform&name=m001_Customer';

}
//========================================================
//                       checkEnterA
//========================================================
function checkEnterA()
{
	var B;
	
	if (event.keyCode != 13) return;
	B = document.getElementsByName('B')[0];
	B.focus();
}
//========================================================
//                       checkEnterB
//========================================================
function checkEnterB()
{
	if (event.keyCode != 13) return;
	login();
}
//========================================================
//                       checkEnterC1
//========================================================
function checkEnterC1()
{
	var C2;
	if (event.keyCode != 13) return;
	c2 = document.getElementsByName('C2')[0];
	c2.focus();
}
//========================================================
//                       checkEnterC2
//========================================================
function checkEnterC2()
{
	var C3;
	if (event.keyCode != 13) return;
	c3 = document.getElementsByName('C3')[0];
	c3.focus();
}
//========================================================
//                       checkEnterC3
//========================================================
function checkEnterC3()
{
	if (event.keyCode != 13) return;
	loginCustomer();
}
//========================================================
//                       checkEnterD
//========================================================
function checkEnterD()
{
	var E;
	if (event.keyCode != 13) return;
	E = document.getElementsByName('E')[0];
	E.focus();
}

//========================================================
//                       checkEnterE
//========================================================
function checkEnterE()
{
	var F;
	if (event.keyCode != 13) return;
	F = document.getElementsByName('F')[0];
	F.focus();
}

//========================================================
//                       checkEnterF
//========================================================
function checkEnterF()
{
	if (event.keyCode != 13) return;
	loginEmployee();
}

//========================================================
//                     server$
//========================================================
function server$()
{
	this.show			= server$show;
	this.execute			= server$execute;
	this.executeW			= server$executeW;  // execute with warning (error) messages	
	this.executeW_A			= server$executeW_A;  // execute with warning (error) messages	
	this.response = "";

	this.receiver = hostUrl + "Receive.asp?";


try
{
	this.xmlhttp = xmlhttp$init();
}
catch (e) { activeXHint('Microsoft.XMLHTTP',e); }
}
//========================================================
//                     server$executeW
//========================================================
function server$executeW(cmd)
{
	var text,n;
	if (this.execute(cmd)) return true;
	text = this.xmlhttp.responseText;
	n = text.indexOf('HTML');

	if (n > 0) this.show();
	else alert(this.xmlhttp.responseText);

	return false;
}	
//========================================================
//                     server$executeW_A
//========================================================
function server$executeW_A(cmd,url)
{
	var temp;
	
	if (this.execute(cmd,url)) return true;
	text = this.xmlhttp.responseText;

	if (text.indexOf('HTML') >= 0) this.show();
	else alert(this.xmlhttp.responseText);

	return false;
}	

//========================================================
//                     server$execute
//========================================================
function server$execute(cmd,url)
{
	var text;
	var result;
	var node,d;
	
   	text = this.receiver;     
   	if (arguments.length == 2) text = url;

   	
	var xml_dom = xmldom$init();
	xml_dom.loadXML('<?xml version="1.0" ?> <root/>');
	xml_dom.documentElement.setAttribute("xmlns:dt", "urn:schemas-microsoft-com:datatypes");
	var node = xml_dom.createElement("data");
	node.dataType = "string";

   	if (arguments.length == 2) 
		 node.nodeTypedValue = cmd;
	else node.nodeTypedValue = xescape(cmd);
   	
	xml_dom.documentElement.appendChild(node);

try
{
   	this.xmlhttp.open("POST",text,false);
}
catch (e) { alert('Logging in from this site requires you to lower your security to Low for trusted sites'); return false; }

  	this.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  	this.xmlhttp.send(xml_dom);
  	  	
  	this.text = this.xmlhttp.responseText;
	if (this.xmlhttp.responseText.indexOf('status=ok') >= 0) return true;
	return false;
}
//==========================================================================
//                           show (show data in a new window)
//==========================================================================
function server$show()
{
	this.win = window.showModelessDialog("blank.htm","",
	"dialogHeight: 700px; dialogWidth: 800px; dialogTop: 100px; dialogLeft:" + 
	"200px; edge: Raised; center: Yes; help: Yes; resizable: Yes; status: Yes;");

	if (this.win == null) return;
	this.win.document.onreadystatechange = server_onReadyStateChange;
	server_onReadyStateChange();
}
//==========================================================================
//                           show (show data in a new window)
//==========================================================================
function server_onReadyStateChange()
{
   if (server.win.document.readyState=="complete")
   {
		server.win.document.body.innerHTML = server.xmlhttp.responseText;
   }
}
//========================================================
//                       initilize
//========================================================
function initilize()
{

	if (server != null) return;
	server = new server$();

}
//========================================================
//                       init
//========================================================
function init()
{

	var text,list,url;
	var D,E,F;
	
	text = unescape(window.location.href);

	list = text.split('?');
	hostUrl = list[0];
	url= list[0];

	if (hostUrl.indexOf('/clif/') > 0) 
	     hostUrl = 'http://clif/CustomerCareSystem/Accounts/';
	else hostUrl = 'http://CustomerCareSystem.com/Accounts/';

	if (url.indexOf('/hp/') > 0) hostUrl = 'http://hp/CustomerCareSystem/Accounts/';

	server = new server$();

	if (list.length <= 1) return;

	list = list[1].split('&');
	if (list.length <= 1) return;
	
	D = document.getElementsByName('D')[0];
	E = document.getElementsByName('E')[0];
	F = document.getElementsByName('F')[0];
	
	if (list.length >= 1) D.value = list[0];
	if (list.length >= 2) E.value = list[1];
	if (list.length >= 3) F.value = list[2];
	
}
