/* 
JavaScript Document for EMPOP Programs pages 
coded by Alexander Roeck, 2005 
*/

/* input check for various fields */
/*	e = not empty
		m = email address
		r = DNA region
		f = emp file
		t = txt file
		n = integer between last before one and last argument of array
		d = float between last before one and last argument of array
*/
var msg_1 = 'The following error(s) occured:\nWrong fields are marked red.\n';
var error_color = 'red';
var allright_color = 'white';
var var_0 = new Array();
var var_1 = new Array();
var var_2 = new Array();
var_1[0] = new Array('emailaddress','m','is not a valid email address','');

var_2[0] = new Array('inputfile','e','please specify a file','');
var_2[1] = new Array('inputfile','f','please specify a file in the requested format (*.emp)','');
for (j=0;j<3;j++)
	var_2[2+j] = new Array('idhg_requiredmatchpercentage'+j,'n','is not an integer between 0 and 100','',0,100);
for (j=0;j<3;j++)
	var_2[5+j] = new Array('idhg_polymorphisms'+j,'n','is not an integer between 0 and 20','',0,20);
for (j=0;j<2;j++)
	var_2[8+j] = new Array('idhg_region'+(j+1),'r','is not a valid region',/^[0-9]{1,5}[-][0-9]{1,5}$/);

var_2[10] = new Array('idhg_Insertionsfile','t','please specify a file in the requested format (Insertions.txt)\n or leave the field blank to use the standard file','');
var_2[11] = new Array('idhg_Haplogroup_Definitionfile','t','please specify a file in the requested format (Haplogroup_Definition.txt)\n or leave the field blank to use the standard file','');
var_2[12] = new Array('idhg_Weightsfile','t','please specify a file in the requested format (Weights.txt)\n or leave the field blank to use the standard file','');

var_2[13] = new Array('phylocheck_region1','r','is not a valid region',/^[0-9]{1,5}[-][0-9]{1,5}$/);
var_2[14] = new Array('phylocheck_region2','r','is not a valid region',/^[0-9]{1,5}[-][0-9]{1,5}$/);
var_2[15] = new Array('phylocheck_tol_HV1_AGTR','d','is not a float between 0 and 10','',0,10);
var_2[16] = new Array('phylocheck_tol_HV1_Hamming','n','is not an integer between 0 and 10','',0,10);
var_2[17] = new Array('phylocheck_tol_HV2_AGTR','d','is not a float between 0 and 10','',0,10);
var_2[18] = new Array('phylocheck_tol_HV2_Hamming','n','is not an integer between 0 and 10','',0,10);
var_2[19] = new Array('phylocheck_Insertionsfile','t','please specify a file in the requested format (Insertions.txt)\n or leave the field blank to use the standard file','');
var_2[20] = new Array('phylocheck_EMPOP_HV1file','t','please specify a file in the requested format (EMPOP_HV1.nexus)\n or leave the field blank to use the standard file','');
var_2[21] = new Array('phylocheck_EMPOP_HV2file','t','please specify a file in the requested format (EMPOP_HV1.nexus)\n or leave the field blank to use the standard file','');
var_2[22] = new Array('phylocheck_Parameters_HV1file','t','please specify a file in the requested format (Parameters_HV1.txt)\n or leave the field blank to use the standard file','');
var_2[23] = new Array('phylocheck_Parameters_HV2file','t','please specify a file in the requested format (Parameters_HV2.txt)\n or leave the field blank to use the standard file','');


for (j=0;j<7;j++){
	var_2[24+j] = new Array('compreport_region'+(j+1),'r','is not a valid region',/^[0-9]{1,5}[-][0-9]{1,5}$/);
}
var_2[31] = new Array('compreport_Hotspotsfile','t','please specify a file in the requested format (Hotspots.txt)\n or leave the field blank to use the standard file','');
var_2[32] = new Array('compreport_Insertionsfile','t','please specify a file in the requested format (Insertions.txt)\n or leave the field blank to use the standard file','');

var_2[33] = new Array('mj_epsilon','n','is not an integer between 0 and 99','',0,99);
var_2[34] = new Array('mj_Hotspotsfile','t','please specify a file in the requested format (Hotspots.txt)\n or leave the field blank to use the standard file','');
var_2[35] = new Array('mj_Insertionsfile','t','please specify a file in the requested format (Insertions.txt)\n or leave the field blank to use the standard file','');

var_2[36] = new Array('nodes_Hotspotsfile','t','please specify a file in the requested format (Hotspots.txt)\n or leave the field blank to use the standard file','');
var_2[37] = new Array('nodes_Insertionsfile','t','please specify a file in the requested format (Insertions.txt)\n or leave the field blank to use the standard file','');

function validate(sender,myarray,err_hd)
{
	<!--
	var err_msg = !err_hd?new Array('The following error(s) occured:\n'):new Array(err_hd+'\n');
	var error = false;
	
	/* check if at least one program is selected */
	if(sender == "programselection") {
		if(document.forms[sender].elements["programchoiceidhg"] && !document.forms[sender].elements["programchoiceidhg"].checked &&
			document.forms[sender].elements["programchoicecompreport"] && !document.forms[sender].elements["programchoicecompreport"].checked &&
			document.forms[sender].elements["programchoicephylocheck"] && !document.forms[sender].elements["programchoicephylocheck"].checked) {
			error = true;
			err_msg.push("please select the program!");
		}
	}
	
	for (i=0;i<myarray.length;i++){
	
		if(document.forms[sender].elements[myarray[i][0]]) {
			field = document.forms[sender].elements[myarray[i][0]];
		
		/* block for nonempty fields */
		if (myarray[i][1].indexOf('e')>-1) {
			if (!field.value) {
				error = true;
				err_msg.push(myarray[i][2]);
				field.style.backgroundColor = error_color;
			}
			else field.style.backgroundColor = allright_color;
		} // end block
		
		/* block for field with emp file */
		else if (myarray[i][1].indexOf('f')>-1) {
			if (field.value) {
				if(toLowerCase(field.value.substr(field.value.length-4,field.value.length))!='.emp') {
					error = true;
					field.style.backgroundColor = error_color;
					err_msg.push(myarray[i][2]);
				}
				else field.style.backgroundColor = allright_color;
			}
		} // end block
		
		/* block for fields with txt file or empty field*/
		else if (myarray[i][1].indexOf('t')>-1) {
			if (field.value) {
				if(toLowerCase(field.value.substr(field.value.length-4,field.value.length))!='.txt') {
					error = true;
					field.style.backgroundColor = error_color;
					err_msg.push(myarray[i][2]);
				}
				else field.style.backgroundColor = allright_color;
			}
		} // end block
		
		/* block for nonempty field that contains an integer between myarray[i][4] and myarray[i][5] */
		else if (myarray[i][1].indexOf('n')>-1) {
			if (!field.value || field.value.indexOf('.')>-1 || field.value.indexOf(',')>-1 || isNaN(field.value) ||
				parseInt(field.value)<myarray[i][4] || parseInt(field.value)>myarray[i][5] ) {
				error = true;
				field.style.backgroundColor = error_color;
				err_msg.push(field.value+" "+myarray[i][2]);
			}
			else field.style.backgroundColor = allright_color;
		} // end block
		
		/* block for nonempty field that contains a float between myarray[i][4] and myarray[i][5] */
		else if (myarray[i][1].indexOf('d')>-1) {
			if (!field.value || field.value.indexOf(',')>-1 || isNaN(field.value) ||
				parseFloat(field.value)<myarray[i][4] || parseFloat(field.value)>myarray[i][5] ) {
				error = true;
				field.style.backgroundColor = error_color;
				err_msg.push(field.value+" "+myarray[i][2]);
				if(field.value.indexOf(',')>-1)
					err_msg.push("use point and not comma as decimal separator");
			}
			else field.style.backgroundColor = allright_color;
		} // end block

		/* block for email address check */
		else if (myarray[i][1].indexOf('m')>-1) {
			if(field.value=="" || field.value=="max.mustermann@domain.xy" || !field.value) {
				error=true;
				field.style.backgroundColor = error_color;
				err_msg.push("specify your email address please");
			}
			else if (field.value) {
				var usr = "([a-zA-Z0-9][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
				var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5}";
				var regex = "^"+usr+"\@"+domain+"$";
				var myrxp = new RegExp(regex);
				var check = (myrxp.test(field.value));
				if (check!=true) {
					error=true;
					field.style.backgroundColor = error_color;
					err_msg.push(field.value+" "+myarray[i][2]);
				}
			}
			else field.style.backgroundColor = allright_color;
		} // end block
		
		/* block for regexp check, checks also if first value less than second value */
		/* i.e. 16024-16200 */
		else if (myarray[i][1].indexOf('r')>-1) {
			var regexp = myarray[i][3];
			if (field.value=="") {
				error = true;
				field.style.backgroundColor = error_color;
				err_msg.push(field.value+" "+myarray[i][2]);
			}
			if (field.value) {
				var regionsplit = field.value.split('-');
				if (!regexp.test(field.value) || (parseInt(regionsplit[0]) >= parseInt(regionsplit[1]))) {
					error = true;
					field.style.backgroundColor = error_color;
					err_msg.push(field.value+" "+myarray[i][2]);
				}
				else field.style.backgroundColor = allright_color;
			}
		} // end block
		} // end if field exists
	} // end for myarray
	/* in case of an error: display all error messages, otherwise submit form */
	if (error) {
		err_msg = err_msg.join('\n\xB7 ');
		alert(err_msg);
		return false;
	}
	/* no error */
	return true;
	
	// -->
} // end function

/* function to check/uncheck a checkbox given by id 'e' */
function checkUncheck(e)
{
	<!--
	if (getElem("id",e,null)) {
		getElem("id",e,null).checked = getElem("id",e,null).checked == true ? false : true;
	}
	// -->	
}


/***********************************************
* Overlapping Content link- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function getposOffset(overlay, offsettype){
	<!--
	var totaloffset=(offsettype=="left")? overlay.offsetLeft : overlay.offsetTop;
	var parentEl=overlay.offsetParent;
	while (parentEl!=null){
		totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	return totaloffset;
	// -->
}

function overlay(curobj, subobj){
	<!--
	var subobj=getElement("id",subobj,null);
	//subobj.style.left=getposOffset(curobj, "left")+"px";
	//subobj.style.top=getposOffset(curobj, "top")+"px";
	subobj.style.display="block";
	return false;
	// -->
}

function overlayclose(subobj){
	<!--
	getElement("id",subobj,null).style.display="none";
	// -->
}
/***********************************************
* END
* Overlapping Content link- © Dynamic Drive (www.dynamicdrive.com)
***********************************************/



/***********************************************
* Tabbed Document Viewer script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var selectedtablink=""
var tcischecked=false

function handlelink(aobject){
<!--
selectedtablink=aobject.href
tcischecked=(document.tabcontrol && document.tabcontrol.tabcheck.checked)? true : false
if (document.getElementById && !tcischecked){
var tabobj=document.getElementById("tablist")
var tabobjlinks=tabobj.getElementsByTagName("A")
for (i=0; i<tabobjlinks.length; i++)
tabobjlinks[i].className=""
aobject.className="current"
document.getElementById("tabiframe").src=selectedtablink
return false
}
else
return true
// -->
}