var g_sid				= "";
var g_username			= "";
var b2b_prgid			= "";
var POSThttp_request 	= false;
var dbg					= 0;
var http 				= getHTTPObject(); // We create the HTTP Object
var POSThttp_request 	= false;
// AJAX Section
function b2b_agent_post(which) {
	str	= "";
	for (i=0;i<which.length;i++) {
		var tempobj=which.elements[i];
		if ((tempobj.name == "ta_code") && (tempobj.value == "") ) {
			alert("Please enter the Agent id.");
			tempobj.focus();
			return;
		}
		
		if ((tempobj.name == "userid") && (tempobj.value == "") ) {
			alert("Please enter the User id.");
			tempobj.focus();			
			return;
		}
		if ((tempobj.name == "pin") && (tempobj.value == "") ) {
			alert("Please enter the password...");
			tempobj.focus();			
			return;
		}

		str = str + "&" + tempobj.name + "=" + tempobj.value;
	}
	if (str != "") {
		b2b_prgid	= "b2b_login";
		b2b_General_POSTRequest('b2blogin_post.php', str);
	}
	//b2b_agent_get('b2blogin_post.php', str);	
}

//---------------------------------------------------------------
// Validation


function num_validate(field) {
	var valid = "0123456789";			// abcdefghijklmnopqrstuvwxyz
	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") {
		alert("Invalid entry! Only numbers are accepted!");
		field.focus();
		field.select();
   }
}

 
function char_validate(field) {
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";			// abcdefghijklmnopqrstuvwxyz
	var ok = "yes";
	var temp;
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
		}
		if (ok == "no") {
		alert("Invalid entry! Only Alphabet and Numbers are accepted!");
		field.focus();
		field.select();
   }
}

// GET Function
function b2b_agent_get(url, str) {
	urlpar		= url + "?m=y" + str;
	//alert("0, " + urlpar);
	http.open("GET", urlpar, false);
	http.onreadystatechange 		= b2b_agent_getHttpResponse;
	http.send(null);
}		


function getHTTPObject() {
	var xmlhttp;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	@else
	xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}

	return xmlhttp;
}


//------------------------------------------------------------------
function get_response(key, arresponse) {
	sval	= "";
	delim	= "|";
	for (x=0; x<arresponse.length; x++) {
		temp	= arresponse[x];
		arkey	= temp.split(delim);
		if (key == arkey[0]) {
			sval = arkey[1];
			break;
		}
	}
	return sval;
}

function b2b_checkuserid(userid) {
	
}

function d_alert(txt) {
	if (dbg == 1) {alert(txt);}
}

//-----------------------------------------------------------
// General AJAX POST

function b2b_General_POSTRequest(url, parameters) {
  POSThttp_request = false;
  if (window.XMLHttpRequest) { // Mozilla, Safari,...
	 POSThttp_request = new XMLHttpRequest();
	 if (POSThttp_request.overrideMimeType) {
		// set type accordingly to anticipated content type
		//http_request.overrideMimeType('text/xml');
		POSThttp_request.overrideMimeType('text/html');
	 }
  } else if (window.ActiveXObject) { // IE
	 try {
		POSThttp_request = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		try {
		   POSThttp_request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	 }
  }
  if (!POSThttp_request) {
	 alert('Cannot create XMLHTTP instance');
	 return false;
  }
  POSThttp_request.onreadystatechange = b2b_General_POSTResponse;
  POSThttp_request.open('POST', url, true);
  POSThttp_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  POSThttp_request.setRequestHeader("Content-length", parameters.length);
  POSThttp_request.setRequestHeader("Connection", "close");
  POSThttp_request.send(parameters);
}

function b2b_General_POSTResponse() {
  if (POSThttp_request.readyState == 4) {
	 if (POSThttp_request.status == 200) {
		result 			= POSThttp_request.responseText;
		//alert(result);
		arresponse		= result.split("\n");
		if (b2b_prgid == "b2b_login") {			
			goto			= "";
			arresponse		= result.split("\n");
			poststatus		= get_response("TASKSTATUS",	arresponse);
			hno				= get_response("HNO",			arresponse);
			hotel_no		= get_response("HOTEL_NO",		arresponse);
			mtype	 	    = get_response("MEMBERTYPE",	arresponse);
			marknr	 	    = get_response("MARKNR",		arresponse);
			ta_code	 	    = get_response("TA_CODE",		arresponse);
			grpno	 	    = get_response("GROUPNO",		arresponse);
			errorno			= get_response("ERRORNO",		arresponse);
			errorstr		= errorno + " " + get_response("ERRORSTR",		arresponse);
			alert_result	= get_response("ALERT_RESULT",	arresponse);			
			if (alert_result == "1") {
				alert("B2B Post: " + result);
			}
			if (poststatus == "ACK")  {
				if (mtype == "HOTEL STAFF") {
					urlpar			= "?hno=" + hno + "&ta_code=" + ta_code + "&marknr=" + marknr + "&grpno=" + grpno + "&ssid=" + sid;	
					if (grpno <= "1") {  // no Group
						goto			= "../admin/chotel.php" 	+ urlpar;
					} else {
						goto			= "../admin/chotel_grp.php" + urlpar ;
					}
				}
				if (mtype == "HOTEL_ADMIN")  {
					urlpar			= "?hno=" + hno + "&ta_code=" + ta_code + "&marknr=" + marknr + "&grpno=" + grpno + "&ssid=" + sid;	
					if (grpno <= "1") {  // no Group
						goto			= "../admin/chotel.php" 	+ urlpar;
					} else {
						goto			= "../admin/chotel_grp.php" + urlpar;
					}
				}							
				if (mtype == "TRAVEL AGENT") {
					urlpar			= "?b2b=1&hno=" + hno + "&ta_code=" + ta_code + "&marknr=" + marknr + "&ssid=" + sid;					
					goto			= "../admin/b2b_menu.php" + urlpar ;
				}
				if (mtype == "HOTEL_SYSTEM") {
					urlpar			= "?hno=" + hno + "&ta_code=" + ta_code + "&marknr=" + marknr + "&grpno=" + grpno + "&ssid=" + sid;	
					goto			= "../admin/chotel.php" + urlpar ;
				}
				if (goto == "") {alert(mtype);}
				//alert(goto);
				window.location = goto;
			} else {
				alert(errorstr);
			}
		}		
		//alert(b2b_prgid + "\n" + errorstr);		
	 } else {
		alert('Please try again...');
	 }
  }
}
