//==========================Ajax author:liubin============================
/**
  *  Create HTTPRequest
  *
  * @sample  oj = createHttpRequest()
  * @return   HTTPRequest
  **/



//==========================float(n,2) or int============================
var loadFlag = false;
//==========================check blank value ============================
function checkBlank(obj,str){
	if(eval("myform."+obj).value==""){
		alert("field < "+str+" > can't be blank!");
		eval("myform."+obj).focus();
		return false;
	}
	return true;
}
//==========================check the date format ============================
function checkDate(str,filedName){
	if(str.match(/^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29))$/)==null){
		alert("< "+filedName+" > format error!");
		return false;
	}
	return true;
}
//=========================for ajax================================//
function setButtonDisable(flag){
	var ajaxButton = document.all("ajaxButton");
	if(ajaxButton==null){
		return;
	}
	var len = ajaxButton.length;
	if(len>0){
		for (var i = 0; i < len; i++) {
			ajaxButton[i].disabled = flag;
		}
	}
}
//=========================add ajax obj================================//
var req; 
var codeObj;
var idObj;
var nameObj;
var loadFlag = false;
function loadXMLDoc(url,formName) {
	loadFlag = true;
	//setButtonDisable(true);
	if (window.XMLHttpRequest)
		req = new XMLHttpRequest(); 
	//branch for IE/Windows ActiveX version 
	else if (window.ActiveXObject)
		req = new ActiveXObject("Microsoft.XMLHTTP"); 
	if (req) {
		req.onreadystatechange = function(){
			if (req.readyState == 4) {
				//give value according to the number of param
				var reqText = req.responseText;
				var ids = reqText.split(";");//aaa$bbb$ccc
				if (ids == "orestricted") {
					eval(codeObj).value = "";
					eval(formName+"."+nameObj).value = "";
					loadFlag = false;
					alert('The input port in the field of Origin is restricted');
					return;
				}
				if (ids == "drestricted") {
					eval(codeObj).value = "";
					eval(formName+"."+nameObj).value = "";
					loadFlag = false;
					alert('The input port in the field of Destination is restricted');
					return;
				}
				var id = "";
				var name = "";
				var code = "";
				for(var i=0;i<ids.length;i++){
					var arr = ids[i].split("$");//aaa or bbb or ccc
					var len = arr.length;
					if(len>=1){
						if(i>0)id += ";";
						id += arr[0];
					}
					if(len>=2){
						if(i>0)name += ";";
						name += arr[1];
					}
					if(len>=3){
						if(i>0)code += ";";
						code += arr[2];
					}
				}
				eval(formName+"."+idObj).value = id;
				eval(formName+"."+nameObj).value = name;
				try{
					eval(codeObj).value = code;
				}catch(e){}
				if(eval(formName+"."+idObj).value == ""){
					if(confirm("The code you input does not exist,do you want to search?"))
						codeObj.fireEvent("ondblclick");
				}
				if(req.status == 200){//end 
					try{
						loadFlag = false;
						//setButtonDisable(false);
					}catch(e){}
				}else{
					alert("error page!");
				}
	 		}
		}
	req.open("GET", url, false); 
	req.send(null); 
	return true; 
	}
}//=====================
function checkLoading(){
	if(loadFlag){
		alert("loading...");
		checkLoading();
	}
}
function replaceAll(str,src,desc){
	if(str.indexOf(src)>-1){
		str = str.replace(src,desc);
		str = replaceAll(str,src,desc);
	}
	return str;
}
function filterLetter(str){
	str = replaceAll(str,"&","abcdefg");
	return str;
}
function filterPersent(str){
	str = replaceAll(str,"%","xpersentx");
	return str;
}
function doClear() {//clear all input fields
	checkLoading();
	var inputFields = document.all("inputField");
	for (var i = 0; i < inputFields.length; i++) {
		inputFields[i].value = "";
	}
	var check = document.getElementsByName("applyC");
	for(var i = 0; i < check.length; i++){
		if (check[i].checked == true) {
			check[i].checked = false;
		}
	}
}
//=========================================================================================//
function isMoney(k) {
	checkNum(k);
	//var kString = null;
	//if (k.value.indexOf(".") ==-1) {
	//	kString = k.value + ".0";
	//} else {
	//	kString = k.value;
	//}
	//if ((/^\-?\d{1,6}\.?\d{0,2}$/.test(kString)) == false && k.value != "") {
	//	alert("Input error,please correct it");
	//	k.select();
	//	k.focus();
	//}

}
function checkNum(k) {
	if (/^\d{1,6}$/.test(k.value) == false && k.value != "") {
		alert("\""+k.value+"\"is error,  You must enter the positive integer!");
		k.select();
		k.focus();
		return false;
	} else {
		return true;
	}
}
//==============================================================================
function pressEnterSubmit(formName) {
	if ((event.keyCode == 13)) {
		document.formName.submit();
	}
}

//===============================change row color=====================================
function changeTR(obj, classname) {
	obj.className = classname;
	var str = obj.all.tags("td");
	for (var i = 0; i < str.length; i++) {
		str[i].className = classname;
	}
}
//==============================================================================
//==========================page submit=======================
function toNextPage(str) {
	document.forms[0].actionMethod.value = str;
	var pagenum = Number(document.forms[0].pageNum.value) + 1;
	document.forms[0].pageNum.value = pagenum;
	//alert(pagenum);
	document.forms[0].submit();
}
function toBackPage(str) {
	document.forms[0].actionMethod.value = str;
	var pagenum = Number(document.forms[0].pageNum.value)- 1;
	document.forms[0].pageNum.value = pagenum;
	//alert(pagenum);
	document.forms[0].submit();
}
function toFirstPage(str) {
	document.forms[0].actionMethod.value = str;
	document.forms[0].pageNum.value = 1;
	//alert(pagenum);
	document.forms[0].submit();
}
function toLastPage(str) {
	document.forms[0].actionMethod.value = str;
	document.forms[0].pageNum.value = document.forms[0].totalPage.value;
	//alert(pagenum);
	document.forms[0].submit();
}

//========================show the content of TextBox ,toolTips=================================
function showTips() {
	var tip;
	tip = event.srcElement.value;
	event.srcElement.title = tip;
}
//========================make all 'input' control to enabled=================================
function changeDisableToFalse() {
	var str = document.all.tags("input");
	for (var i = 0; i < str.length; i++) {
		str[i].disabled = false;
	}
}
//========================estimate the input value is letter only=================================
function isOnlyLetter(k, length) {
	var s = k.value;
	var len = 0;
	len = parseInt(length);
	var patrn = /^[a-zA-Z]{1,50}$/;
	if (!patrn.exec(s) || s == "") {
		alert("please input letters only,and the max length is " + len + "!");
		k.select();
		k.focus();
		return false;
	} else {
		return true;
	}
}
//============when press the 'Enter' key,submit the corresponding form========================
function pressEnterSubmit(formName) {
	var myform = formName;
	if ((event.keyCode == 13)) {
		document.all.myform.submit();
	}
}
//==========================trim() function for strings=======================================
String.prototype.trim = function () {
	return this.replace(/(^\s*)|(\s*$)/g, "");
};
String.prototype.ltrim = function () {
	return this.replace(/(^\s*)/g, "");
};
String.prototype.rtrim = function () {
	return this.replace(/(\s*$)/g, "");
};
//==========================allow input letter only ==========================================
function check_letter(obj)
{   	
	if (/^[A-Za-z]+$/.test( obj.value )||obj.value=="") 
	{
	   return true;
	} 
	alert("input letter only");
	obj.select();
	obj.focus();
	return false;
}
//==========================input specified length value only ======================================
function checkSpecifiedLength(obj,len)
{   	

	if (obj.value.length<=len) 
	{
	   return true;
	} 
	
	alert("input "+len+" letters only");
	obj.select();
	obj.focus();
	return false;
}
//==========================aonly ==========================================
var newwindow ="";
function showCalendar(e) {

	url="RateRequest/cal.jsp";
	destField=e;
	
	var t=e.offsetTop;
	var l=e.offsetLeft;
	while(e=e.offsetParent){
		t+=e.offsetTop;
		l+=e.offsetLeft;
	}
	//alert(l+"------show calendar-----"+t);
	if (!newwindow.closed && newwindow.location){
	
		newwindow.location.href = url;
	}else{
		//newwindow=window.open(url,"name","height=650 width=650");toolbars=0, scrollbars=0, location=0, statusbars=1, menubars=0, resizable=0,
		
		newwindow=window.open(url, "calendar", " width=2, height=2, left = "+(l+20)+", top = "+(t+145)); 
		
		//newwindow=window.open(url,'name','height=650');
		if (!newwindow.opener) {
			newwindow.opener = self;
		}
	}
	if (window.focus) {
		newwindow.focus()
	}      
}
function checkFields(isShowAlert) {
	var buyTable = document.getElementById("buyingRateTable");
	var comTable = document.getElementById("comRateTable");
	var origins = document.all("origin");
	var ports = document.all("port");
	var services = document.all("serviceType");
	var rate20s = document.all("rate20");
	var rate40s = document.all("rate40");
	var rate40hqs = document.all("rate40hq");
	var rate45s = document.all("rate45");
	var allins = document.all("allin");
	var surcharges = document.all("instructionsDetail");
	var competitors = document.all("competitor");
	var vias = document.all("via");
	var citys = document.all("city");
	
	if(document.forms[0].cneeType[0].checked){
		if(document.forms[0].cneeAndAS400.value.trim()==""||document.forms[0].cneeAndAS400.value.trim()=="-"){
			if(isShowAlert)
				alert("Must Select an Account! ");
			return false;
		}
	}else{
		if(document.forms[0].prospective.value==""){
			if(isShowAlert)
				alert("Must Select an Account! ");
			return false;
		}
	}
	//=========Must Select a Reason=============bug 194: add by Stone 2007-4-2
	var ifSelectedReason=false;
	for(var i=0;i<document.forms[0].resons.length;i++){
		if(document.forms[0].resons[i].checked==true){
			ifSelectedReason=true;
			break;
		}
	}
	if(!ifSelectedReason){
		if(isShowAlert)
			alert("Must Select a Reason!");
		return false;
	}
	//========================================
	if(document.forms[0].commodityNameAndIDs.value.trim()==";"||document.forms[0].commodityNameAndIDs.value==""){
		if(isShowAlert)
			alert("Must Select a Commodity!");
		return false;
	}
	

	
	//alert(buyTable.rows.length);
	if (buyTable.rows.length >= 3) {
		//alert("run into check");
		for (var i = 0; i < buyTable.rows.length-2; i++) {
			if (origins[i].value == "") {
				if(isShowAlert)
					alert("Must Select an Origin!");
				return false;
			}
			if (ports[i].value == "") {
				if(isShowAlert)
					alert("Must Select a Destination!");
				return false;
			}
			if (vias[i].value == "") {
				if(isShowAlert)
					alert("Must Select a via!");
				return false;
			}
			if (services[i].value == "") {
				if(isShowAlert)
					alert("Must Select a Service Type!");
				return false;
			}
//			if (citys[i].value == "") {  //bug_192:  rebecca_20070406
//				alert("Must Select a Zipcode to get a City!");
//				return false;
//			}
			if (allins[i].checked != true) {
				if (surcharges[i].value == "") {
					if(isShowAlert)
						alert("Some Surcharge is Blank!");
					return false;
				}
			}
			//bug_254:  rebecca_20070612
			if ((rate20s[i].value == ""||rate20s[i].value == "0")&&(rate40s[i].value == ""||rate40s[i].value == "0")&&(rate40hqs[i].value == ""||rate40hqs[i].value == "0")&&(rate45s[i].value == ""||rate45s[i].value == "0")){
//			if ((rate20s[i].value == ""&&rate40s[i].value == ""&&rate40hqs[i].value == ""&&rate45s[i].value == "")||(rate20s[i].value == "0"&&rate40s[i].value == "0"&&rate40hqs[i].value == "0"&&rate45s[i].value == "0") ){
				if(isShowAlert)
					alert("Must Enter One of the Rates!! ");
				return false;
			}			
		}
	}

	if (comTable.rows.length >= 3) 
	{
		if (comTable.rows.length == 3){
			if(!checkSpecifiedLength(document.all("competitor"),50)){
				return false;
			}
		}
		else{
			for (var i = 0; i < competitors.length; i++) 
			{
				//alert(competitors[i].value);
				if (competitors[i].value!="") {
					if(!checkSpecifiedLength(competitors[i],50)){
						return false;
					}
				}
			}
		}
	}
	return true;
//======================================
}


// function to calculate local time
// in a different city
function calcTime() {

	var onecount;
	onecount=32;
	subcat = new Array();
	
	subcat[0] = new Array("-720","GMT-12");
	subcat[1] = new Array("-660","GMT-11");
	subcat[2] = new Array("-600","GMT-10");
	subcat[3] = new Array("-540","GMT-9");
	subcat[4] = new Array("-480","GMT-8");
	subcat[5] = new Array("-420","GMT-7");
	subcat[6] = new Array("-360","GMT-6");
	subcat[7] = new Array("-300","GMT-5");
	subcat[8] = new Array("-240","GMT-4");
	subcat[9] = new Array("-210","GMT-3:30");
	subcat[10] = new Array("-180","GMT-3");
	subcat[11] = new Array("-120","GMT-2");
	subcat[12] = new Array("-60","GMT-1");
	subcat[13] = new Array("0","GMT");
	subcat[14] = new Array("60","GMT+1");
	subcat[15] = new Array("120","GMT+2");
	subcat[16] = new Array("180","GMT+3");
	subcat[17] = new Array("240","GMT+4");
	subcat[18] = new Array("270","GMT+4:30");
	subcat[19] = new Array("300","GMT+5");
	subcat[20] = new Array("330","GMT+5:30");
	subcat[21] = new Array("345","GMT+5:45");
	subcat[22] = new Array("360","GMT+6");
	subcat[23] = new Array("390","GMT+6:30");
	subcat[24] = new Array("420","GMT+7");
	subcat[25] = new Array("480","GMT+8");
	subcat[26] = new Array("540","GMT+9");
	subcat[27] = new Array("570","GMT+9:30");
	subcat[28] = new Array("600","GMT+10");
	subcat[29] = new Array("660","GMT+11");
	subcat[30] = new Array("720","GMT+12");
	subcat[31] = new Array("780","GMT+13");

	// create Date object for current location
	d = new Date();
	// convert to msec
	// add local time zone offset 
	// get UTC time in msec
	utc = d.getTime() + (d.getTimezoneOffset() * 60000);
	
	for (i=0;i < onecount; i++)
	{
		//document.TabTimeZone.TimeZone.options[document.TabTimeZone.TimeZone.length] = new Option(subcat[i][1], subcat[i][0]);
		if(subcat[i][0] == -d.getTimezoneOffset()){
			return subcat[i][1];
		}
	}
	
	//return "The local time in " + city + " is " + nd.toLocaleString();
	//alert(calcTime('Bombay', '+5.5'));
}



