// JavaScript Document
function doLogin(frm){
    var requiredFields = new Array("IMS","PWD");
    var fieldNames = new Array("Username","Password");
    var fieldCheck   = true;
    var fieldsNeeded = "\n Following Information is Required:\n\n\t";

    for(var fieldNum=0; fieldNum < requiredFields.length; fieldNum++) {
        if ((frm.elements[requiredFields[fieldNum]].value == "") ||
            (frm.elements[requiredFields[fieldNum]].value == " ")) {

            fieldsNeeded += fieldNames[fieldNum] + "\n\t";
            fieldCheck = false;
        }
    }

    // ALL REQUIRED FIELDS HAVE BEEN ENTERED
    if(!fieldCheck)    {
        alert(fieldsNeeded);
		return false;
    }
	if(document.getElementById("captchaVal") && document.getElementById("captchaVal").value =="") {
		alert("Please enter the charactors displayed in images.");
		return false;
	}
	else {
		//Ajax call.
		var http=getHTTPObject();
		var url='/captcha/doMemberLogin.cfm?IMS='+frm.IMS.value+'&PWD='+frm.PWD.value;
		if(document.getElementById("captchaVal")) {
			url=url+'&captchaVal=' + frm.captchaVal.value + '&hashref=' + frm.hashref.value;
		}
		http.open("GET", url, false);
		http.send(null);
		//window.open(null).document.write(http.responseText)
		//set err and password value after posted.
		document.getElementById("errmsg").innerHTML = '';
		document.getElementById("pwd").value = '';
		var captcha=http.responseText;
		var pos = captcha.indexOf("|");
		var pos2 = captcha.lastIndexOf("|");
		var rtcode = parseInt(captcha.substring(0, pos));
		var divCaptcha = document.getElementById("captcha");
		//alert(rtcode);
		if (rtcode > 0 && rtcode <3) {
			document.getElementById("errmsg").innerHTML = "<span class='smRed'>Login Failed, Please Try Again.</span>";
			//showLoginErr();
			divCaptcha = document.getElementById("captcha");
			divCaptcha.innerHTML = '';
		}
		else if (rtcode > 2 && rtcode != 500 && rtcode !=600 && rtcode!= 700) {
			document.getElementById("errmsg").innerHTML = "<span class='smRed'>Login failed, please input user name, password along with the characters in the images.</span>";
			reloadCaptcha(http);
			showChatButton();
		}
		else if (rtcode == 500) {
			divCaptcha.innerHTML = "<span class='smRed'>Login Failed, Please contact support 713-629-1900 x 276</span>"
			showChatButton();
		}
		else if (rtcode == 600) {
			document.getElementById("errmsg").innerHTML = "<span class='smRed'>Multiple failed attempts previously, please input user name, password along with the characters in the images.</span>";
			reloadCaptcha(http);
			showChatButton();
		}
		else if (rtcode == 700) {
			document.getElementById("errmsg").innerHTML = "<span class='smRed'>Image verification error, Please retype the characters, they are in lower case letters or numeric numbers.</span>";
			reloadCaptcha(http);
			showChatButton();
		}
		else if (rtcode == 0) {
			//submit here when login successful
			document.getElementById("Member_Number").value= captcha.substring(pos+1, pos2);
			document.getElementById("EUID").value = captcha.substring(pos2+1);
			//alert(document.getElementById("EUID").value);
			//frm.submit();
			return true;
		}
		return false;
	}
}

function reloadCaptcha (http) {
	if(!http) {
		http=getHTTPObject();
	}
 	http.open("get", "/captcha/dispCaptcha.cfm", false);
	http.send(null);
	var divCaptcha = document.getElementById("captcha");
	if (divCaptcha) {
		divCaptcha.innerHTML = http.responseText;
		//alert(http.responseText);
	}
	
}

function getHTTPObject() {
  var xmlhttp;
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
  
  return xmlhttp;
}

function agents_available() {
	document.getElementById('smartbutton').innerHTML = '<A HREF="" onClick="window.open(\'/ischat/frmChat.htm\',\'custclient\',\'width=500,height=360,scrollbars=0\');return false;"><IMG SRC="/ischat/graphics/chat.jpg" border="0"></A><br/><span class="sm">Need help? Chat online with a HAR Member Service Representative.</span>';
	return true;
}
 
// IF AGENTS ARE NOT AVAILABLE:
function agents_not_available() {
	document.getElementById('smartbutton').innerHTML = '';
	return true;
}

function showChatButton() {
	document.getElementById("divchat").style.display="block";	
}
