// JavaScript Document

// make xml obj

function createXMLHttpRequest() {
	if (window.XMLHttpRequest) { //ÆÄÀÌ¾îÆø½º³ª ¸ÆÀÇ »çÆÄ¸®ÀÇ °æ¿ìÃ³¸®
		// Create XMLHttpRequest object in non-Microsoft browsers
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) { //IE°è¿­ÀÇ ºê¶ó¿ìÁ®ÀÇ °æ¿ì
		// Create XMLHttpRequest via MS ActiveX
		try {
			// Try to create XMLHttpRequest in later versions
			// of Internet Explorer
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			// Failed to create required ActiveXObject
			try {
			// Try version supported by older versions
			// of Internet Explorer
			return new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				// Unable to create an XMLHttpRequest with ActiveX
			}
		}
	}
}

// user log in and out 

function user_logout() {
	xmlHttpUser1 = 	createXMLHttpRequest();
	xmlHttpUser1.onreadystatechange = handleUserLogOut;
	xmlHttpUser1.open("GET","user/user_logout.php",true);
	xmlHttpUser1.send(null);	
}

function handleUserLogOut() {
	if (xmlHttpUser1.readyState == 4) {
		if (xmlHttpUser1.status == 200) {
			//refresh
			window.location="index.php";
		} else {
            alert("There was a problem retrieving the XML data:\n" +
                xmlHttpUser1.statusText);
        } 
	}
}

function user_login() {
	var email=document.getElementById('email').value;
	var reg_ex_email=/^[\w|\.|\-]+@[\w|\.|\-]+\.[a-zA-Z]{2,4}$/;
	
	if (!reg_ex_email.test(email))
	{
		alert("Please input valid e-mail address!    ");
		document.getElementById("email").focus();
		return;
	}
	
	url = "user/user_login.php?";
	url += "email="+document.getElementById('email').value;
	url += "&passwd="+document.getElementById('passwd').value;
	xmlHttpUser2 = 	createXMLHttpRequest();
	xmlHttpUser2.onreadystatechange = handleUserLogIn;
	xmlHttpUser2.open("GET",url,true);
	xmlHttpUser2.send(null);
}

function user_enter(e) {
	if (window.event && e.keyCode == 13) {
		user_login();
	} else if (e && e.which == 13) {
		user_login();
	}

}

function handleUserLogIn() {
	if (xmlHttpUser2.readyState == 4) {
		if (xmlHttpUser2.status == 200) {
			//refresh
			var er_txt = getErrorMsg(xmlHttpUser2);
			if (er_txt != null) {
				alert(er_txt);
			} else {
				window.location="sample_manager.php";
			}
		} else {
            alert("There was a problem retrieving the XML data:\n" +
                xmlHttpUser2.statusText);
        } 
	}
}

// Parser Error xml

function getErrorMsg(xmlObj) {
	if (xmlObj.responseXML) {
		var results = xmlObj.responseXML.documentElement;
		if (results != null) {
			if (results.getElementsByTagName("er_txt").length > 0) {
				var retStr = results.getElementsByTagName("er_txt")[0].firstChild.data;
				return retStr;
			}
		}
	}
}

function printStatus(str) {
	window.status=str;
	//var div = document.getElementById('south');
	//div.innerHTML = str+"<br/>" + div.innerHTML;
}

function addTableRow(name, value, target) {
	var row = document.createElement("tr");

//	row.innerHTML = "<td class='ginfo_name'>"+name+"</td>";
//	row.innerHTML += "<td class='ginfo_value'>"+value+"</td>";
	var cell = createCellWithText(name);
	cell.setAttribute("class","table_head");
	cell.setAttribute("className","table_head");
	row.appendChild(cell);
	cell = createCellWithText("");
	cell.innerHTML = value;
	cell.setAttribute("class","table_value2");
	cell.setAttribute("className","table_value2");
	row.appendChild(cell);
	target.appendChild(row);
}

function createCellWithText(text) {
	var cell = document.createElement("td");
	var textNode = document.createTextNode(text);
	cell.appendChild(textNode);
	return cell;
}

function exclusiveChk(obj) {
	gc = document.getElementById("gr_cr");
	gh1 = document.getElementById("gr_hv1") ;
	gh2 = document.getElementById("gr_hv2") ;
	gh3 = document.getElementById("gr_hv3") ;
	if (obj == gc && gc.checked ) {
		gh1.checked = false;
		gh2.checked = false;
		gh3.checked = false;
	} else if ( (obj == gh1 && gh1.checked) || (obj == gh2 && gh2.checked) || (obj == gh3 && gh3.checked) ) {
		gc.checked = false;
	}
}

function exclusiveChk2(obj) {
	gc = document.getElementById("ge_cr");
	gh1 = document.getElementById("ge_hv1") ;
	gh2 = document.getElementById("ge_hv2") ;
	gh3 = document.getElementById("ge_hv3") ;
	if (obj == gc && gc.checked ) {
		gh1.checked = false;
		gh2.checked = false;
		gh3.checked = false;
	} else if ( (obj == gh1 && gh1.checked) || (obj == gh2 && gh2.checked) || (obj == gh3 && gh3.checked) ) {
		gc.checked = false;
	}
}

function showLoading() {
	ld = document.getElementById("loading");
	if (ld) {
		ld.style.visibility = "visible";
	}
}

function hideLoading() {
	ld = document.getElementById("loading");
	if (ld) {
		ld.style.visibility = "hidden";
	}
}

function isChecked(elId) {
	if (document.getElementById(elId).checked) {
		return 1;
	} else {
		return 0;
	}		
}

function initLoginNUserButtons() {
	if (Ext.get('log_button1')) {
		var bconfig = {
			handler: user_login,
			scope: this,
			minWidth: 80,
			text: "Login"
		};
		searchBtn = new Ext.Button(Ext.get('log_button1'), bconfig);
		searchBtn.getEl().setStyle("margin-left","3px");
	}
	if (Ext.get('log_button2')) {
		var user_join = function() {
			window.location='user_join.php';
		};
		var bconfig = {
			handler: user_join,
			scope: this,
			minWidth: 80,
			text: "Register"
		};
		searchBtn = new Ext.Button(Ext.get('log_button2'), bconfig);
		searchBtn.getEl().setStyle("margin-left","3px");
	}
	if (Ext.get('log_button3')) {
		var bconfig = {
			handler: user_logout,
			scope: this,
			minWidth: 80,
			text: "Logout"
		};
		searchBtn = new Ext.Button(Ext.get('log_button3'), bconfig);
		searchBtn.getEl().setStyle("margin-left","3px");
	}
	if (Ext.get('log_button4')) {
		var user_edit = function() {
			window.location='user_edit.php';
		};
		var bconfig = {
			handler: user_edit,
			scope: this,
			minWidth: 100,
			text: "Change Profile"
		};
		searchBtn = new Ext.Button(Ext.get('log_button4'), bconfig);
		searchBtn.getEl().setStyle("margin-left","3px");
	}
	if (Ext.get('user_button1')) {
		var bconfig = {
			handler: emailchk,
			scope: this,
			minWidth: 80,
			text: "OK"
		};
		searchBtn = new Ext.Button(Ext.get('user_button1'), bconfig);
		searchBtn.getEl().setStyle("margin-left","3px");
	}
	if (Ext.get('user_button2')) {
		var user_reset = function() {
			document.forms.ue_form.reset();
		};
		var bconfig = {
			handler: user_reset,
			scope: this,
			minWidth: 80,
			text: "Reset"
		};
		searchBtn = new Ext.Button(Ext.get('user_button2'), bconfig);
		searchBtn.getEl().setStyle("margin-left","3px");
	}
	if (Ext.get('user_button3')) {
		var bconfig = {
			handler: passwordChk,
			scope: this,
			minWidth: 80,
			text: "OK"
		};
		searchBtn = new Ext.Button(Ext.get('user_button3'), bconfig);
		searchBtn.getEl().setStyle("margin-left","3px");
	}
	if (Ext.get('user_button4')) {
		var user_reset = function() {
			document.forms.ue_form.reset();
			fillingForm();
		};
		var bconfig = {
			handler: user_reset,
			scope: this,
			minWidth: 80,
			text: "Reset"
		};
		searchBtn = new Ext.Button(Ext.get('user_button4'), bconfig);
		searchBtn.getEl().setStyle("margin-left","3px");
	}
}

function ltrim(value) {
  var LeftTrimValue = "";
  var i=0;
  if( value == "" )
        return "";
  strLen = value.length;
  while(i<strLen)
  {
    if(value.charAt(i) != " ") break;
    i++;
  }
  for(s=i;s<strLen;s++)
  {
    LeftTrimValue = LeftTrimValue + value.charAt(s);
  }
  return LeftTrimValue;
}
function rtrim(value) {
  var RightTrimValue = "";
  if( value == "" )
        return "";
  strLen = value.length;
  --strLen;
  while( 0 <= strLen)
  {
    if(value.charAt(strLen) != " ") break;
    --strLen;
  }
  for(s=0;s<=strLen;s++)
  {
    RightTrimValue = RightTrimValue + value.charAt(s);
  }
  return RightTrimValue;
}
function trim(value) {
  return rtrim(ltrim(value));
}
function sequence_check(seq) {
	var genes = seq.split(/[ ,\t\n\r]/);
	for (i = 0 ; i < genes.length ; i++) {
		var gene = trim(genes[i]);
		var num = 0;
		if (gene.length == 0) continue;
		if (gene.match(/^[1-9]([0-9]{0,4})[dDcatgrykmswnCATGRYKMSWN=]$/)) {
			num = parseInt(gene.substring(0,gene.length-1));
		} else if (gene.match(/^[1-9]([0-9]{0,4})\.[1-9]([0-9]{0,2})[catgrykmswnCATGRYKMSWN]$/)) {
			num = parseInt(gene.substring(0,gene.indexOf('.')));
		} else if (gene.match(/^[1-9]([0-9]{0,4})\.p[catgrykmswnCATGRYKMSWN]$/)) {
			num = parseInt(gene.substring(0,gene.length-2));
		} else if (gene.match(/^[1-9]([0-9]{0,4})$/)) {
			num = parseInt(gene);
		} else {
			alert("'"+gene+"' is invalid sequence format!    ");
			return false;
		}

		if (num >= 16024 && num <= 16569) {
		} else if (num >= 1 && num <= 437) {
		} else if (num >= 438 && num <= 576) {
		} else if (num >= 1 && num <= 16569) {
			alert("'"+gene+"' is out of control region!    ");
			return false;
		} else {
			alert("'"+gene+"' is invalid sequence format!    ");
			return false;	
		}
	}
	return true;
}

Ext.EventManager.onDocumentReady(initLoginNUserButtons, this, true);