function alltrim(str)
{
  var i,j;
  var i = 0;
  var j=str.length-1;
  var str = str.split("");
  while(i < str.length)
  {
    if(str[i]==" ")
    {
      str[i] = ""
    }
    else
    {
      break;
    }
    i++;
  }
  while(j > 0)
  {
    if(str[j]== " ")
    {
      str[j]=""
    }
    else
    {
      break;
    }
    j--;
  }
  return str.join("");
}

function checkDob(d,m,y)
{
	var x = 0;
	var res = checkInt(y);
	if (res == false)
	{
		return "Year Invalid";
	}
	if (y < 1986 || y > 1993)
	{
		return "Year out of range (1986-1993)";
	}

	res = checkInt(m);
	if (res == false)
	{
		return "Month Invalid";
	}
	if (m < 1 || m > 12)
	{
		return "Month Out of Range (1-12)";
	}

	res = checkInt(d);
	if (res == false)
	{
		return "Day Invalid";
	}
	if (m==1 || m==3 || m ==5 || m == 7 || m == 8 || m == 10 || m == 12 )
	{
		x = 31;
	}
	else if (m==2)
	{
		if ((y%4)==0)
		{
			x = 29;
		}
		else
		{
			x = 28;
		}
	}
	else
	{
		x = 30;
	}

	if (d < 1 || d > x)
	{
		return "Date out of range (1 - " + x +")";
	}
	return "OK";
}
function checkEmail(val)
{
	//alert(val.length);
	if (val.length == 0) return true;

	var re = new RegExp("^[a-zA-Z_][a-zA-Z0-9_\.]*@[a-zA-Z0-9_]+[\.][a-zA-Z0-9_]+$");
	if (val.match(re)) 
	{
		return true;
	} 
	else 
	{
		return false;
	}
}

function checkName(val)
{
	var result = checkEmpty(val,2);
	if (result == false)
	{
		return false;
	}
	var re = new RegExp("^[a-zA-Z][a-zA-Z\. ]+$");
	if (val.match(re)) 
	{
		return true;
	} 
	else 
	{
		return false;
	}
}


function setOther()
{
	
	//var y = document.appForm.applform.branch[appForm.applform.branch.selectedIndex].text;
	var el = document.getElementById("branch");
	var y = el[el.selectedIndex].text;	
	y = y.substr(0,2);
	//alert(y);
	var elem = document.getElementById("other");
	if (y == "En")
	{
		elem.innerHTML = "Applying for Medicine Also:";
	}
	else if (y == "Me")
	{
		elem.innerHTML = "Applying for Engineering Also:";
	}
	else
	{
		elem.innerHTML = "Applying for (E/AM) also:";
	}
	
	
}

function setOther1()
{
	
	if (document.getElementById("otherappl").checked)
	{
		document.getElementById("otherno").value="";
		document.getElementById("otherno").disabled=0;
		
	}
	else 	
	{
		document.getElementById("otherno").value="";
		document.getElementById("otherno").disabled=1;
		
	}

}

function setBridge()
{
	var el = document.getElementById("q_exam");
	var y = el[el.selectedIndex].text;

	if (y == "Intermediate Vocational")
	{
		document.getElementById("b_htno").value="";
		document.getElementById("b_htno").disabled=0;
		
	}
	else 	
	{
		document.getElementById("b_htno").value="";
		document.getElementById("b_htno").disabled=1;
		
	}

 return;
}

function checkRange(val, min, max)
{
	if (checkInt (val))
	 {
		var v = parseInt(val);
		if (v >= min && v <= max) return true;
	 }
	return false;
}

function checkInt(val)
{
	//alert("inside check int");
	var re = new RegExp("^[0-9]+$");
	if (val.match(re)) 
	{
		return true;
	} 
	else 
	{
		return false;
	}
}

function checkEmpty(val, minLen)
{
	var x = alltrim(val);
	resultHere = true;
	//alert("X and Its length and minLen:--"+x+"--"+x.length + "--" + minLen);
	if (x.length < minLen)
	 {
		resultHere = false;
	 }
	 //alert(resultHere);
	return resultHere;
}

function checkSelection(obj)
{
	var y = obj[obj.selectedIndex].text;
	y = y.substr(0,2);
	if (y == "--")
	{
		return false;
	}
	return true;
}

function errorMessage(obj, msg)
{
	alert("Error: " + msg);
	obj.focus();
}

function validate()
{
	var formOk=false;
	var x,y,z, res, result;
	var elem;
	// Application Category  (E/AM)
	elem = document.getElementById("branch");
	res = checkSelection(elem);
	if (res == false)
	 {
		errorMessage(elem, "Select Engineering/Medicine");
		return;
	 }

	

	// Candidate Name
	elem = document.getElementById("cname");
	x = alltrim(elem.value);
	result = checkName(x);
	if ( result == false)
	{
		errorMessage(elem, "Name too Small or invalid characters in name");
		return;
	}

	// Candidate Father Name
	elem = document.getElementById("fname");
	x = alltrim(elem.value);
	result = checkName(x);
	if ( result == false)
	{
		errorMessage(elem, "Father Name too Small or Invalid characters in Name");
		return;
	}

	// Check DOB
	var e1 = document.getElementById("dobday");
	var e2 = document.getElementById("dobmonth");
	var e3 = document.getElementById("dobyear");
	x = alltrim(e1.value);
	y = alltrim(e2.value);
	z = alltrim(e3.value);
	res = checkDob(x,y,z);
	if (res != "OK")
	{
		alert (res);
		y = res.substr(0,2);
		//alert("Y=."+y+".");
		if (y == "Ye")
		{
			e3.focus();
		}
		else if (y == "Mo")
		{
			e2.focus();
		}
		else 
		{
			e1.focus();
		}
		return;
	}
    //Qualifying Exam
	elem = document.getElementById("q_exam");
	res = checkSelection(elem);
	if (res == false)
	 {
		errorMessage(elem, "Select Qualifying Examination");
		return;
	 }

     

	// Qualifying Exam HTNO
	elem = document.getElementById("q_htno");
	x = alltrim(elem.value);
	//alert("Inter HTNO: --"+x+"--");
	result = checkEmpty(x, 5);
	//alert(result);
	if ( result == false)
	{
		errorMessage(elem, "Qualifying Exam HTNO too small");
		return;
	}

	// Bridge Course HTNO validation
    var qex = document.getElementById("q_exam");
	var yex_text = qex[qex.selectedIndex].text;

	if (yex_text == "Intermediate Vocational")
	{
		var b_ht = document.getElementById("b_htno");
		var v_x = alltrim(b_ht.value);
		//alert("Inter HTNO: --"+x+"--");
		result = checkEmpty(v_x, 5);
		//alert(result);

		if ( result == false)
		{
			errorMessage(elem, "Bridge Course HT.NO too small");
			return;
		}
	}

	// Qualifying Exam Appearence Year
	elem = document.getElementById("q_appyear");
	x = alltrim(elem.value);
	result = checkRange(x,1999,2009);
	if ( result == false)
	{
		errorMessage(elem, "Qualfying Examination Year invalid or out of range (1999 to 2009)");
		return;
	}
	
	// Qualifying Exam pass year Year
	elem = document.getElementById("q_year");
	x = alltrim(elem.value);
	result = checkRange(x,1999,2009);
	if ( result == false)
	{
		errorMessage(elem, "Qualfying Examination Year invalid or out of range (1999 to 2009)");
		return;
	}

	// Sex 
	elem = document.getElementById("sex");
	res = checkSelection(elem);
	if (res == false)
	 {
		errorMessage(elem, "Select Gender");
		return;
	 }

	// category 
	elem = document.getElementById("category");
	res = checkSelection(elem);
	if (res == false)
	 {
		errorMessage(elem, "Select Category");
		return;
	 }

	// E and AM consistancy Check .. Both RegNos Should be present
	e1 = document.getElementById("otherno");
	//e2 = document.getElementById("othercode");
	e3 = document.getElementById("otherappl");
	x = alltrim(e1.value);
	//y = alltrim(e2.value);

	if (e3.checked)
	{
		result = checkInt(x);
		if (result == false || x.length != 7)
		{
			errorMessage(e1, "invalid other application registration number");
			return;
		}
		/*
		result = checkInt(y);
		if (result == false || y.length != 7)
		{
			errorMessage(e2,"invalid other application code");
			return;
		}*/
	}

	//localarea
	elem = document.getElementById("localarea");
	res = checkSelection(elem);
	if (res == false)
	 {
		errorMessage(elem, "Select Your Local Area");
		return;
	 }

	// Center Code 
	var center = document.getElementById("center");
	res = checkSelection(center);
	if (res == false)
	 {
		errorMessage(center, "Select Examination Centre");
		return;
	 }

	

	
			
	// Email
	elem = document.getElementById("email");
	x = alltrim(elem.value);
	//alert("email:"+x+":");
	result = checkEmail(x);
	if ( result == false)
	{
		errorMessage(elem, "E-Mail ID not proper");
		return;
	}
	// Income 
	elem = document.getElementById("income");
	res = checkSelection(elem);
	if (res == false)
	 {
		errorMessage(elem, "Select Income");
		return;
	 }

	 //place of SSC
    elem = document.getElementById("placeofssc");
	res = checkSelection(elem);
	if (res == false)
	 {
		errorMessage(elem, "Select Place of S.S.C or Equivalent");
		return;
	 }

	 //place of inter
    elem = document.getElementById("placeofinter");
	res = checkSelection(elem);
	if (res == false)
	 {
		errorMessage(elem, "Select Place of Intermediate or Equivalent");
		return;
	 }

		//address 	elem = document.getElementById("add1");
	res = checkEmpty(elem.value,3);
	if (res == false)
	 {
		errorMessage(elem, "Fill Flat/Door/Block No.");
		return;
	 }	elem = document.getElementById("add2");
	res = checkEmpty(elem.value,3);
	if (res == false)
	 {
		errorMessage(elem, "Fill Name of Premises/Building/Village");
		return;
	 }	elem = document.getElementById("add3");
	res = checkEmpty(elem.value,3);
	if (res == false)
	 {
		errorMessage(elem, "Fill Road/Street/Line/Post office");
		return;
	 }	elem = document.getElementById("add4");
	res = checkEmpty(elem.value,3);
	if (res == false)
	 {
		errorMessage(elem, "Fill Area/Locality/Taluka/Mondal");
		return;
	 }	elem = document.getElementById("add5");
	res = checkEmpty(elem.value,3);
	if (res == false)
	 {
		errorMessage(elem, "Fill Town/City/District :");
		return;
	 }	elem = document.getElementById("pin");
	res = checkEmpty(elem.value,3);
	if (res == false)
	 {
		errorMessage(elem, "Fill Pin Code :");
		return;
	 }
	 
	 //validating image size
	 /* var oas = new ActiveXObject("Scripting.FileSystemObject");
	  var d =document.getElementById("photofile");
		var e = oas.getFile(d);
		var f = e.size;
		errorMessage(d, f+"bytes :");*/
	
	// Everything Ok, Submit the form.
	var submitButton = document.getElementById("submitButton");
	//submitButton.innerHTML = "<b>Form Submitted... Please Wait while it is processed</b>";


	document.appForm.submit();
}
function checkddnumber(val)
{
	var x = alltrim(val);
	resultHere = true;
	//alert("X and Its length and minLen:--"+x+"--"+x.length + "--" + minLen);
	if (x.length < 6 || x.length > 12)
	 {
		resultHere = false;
	 }
	 //alert(resultHere);
	return resultHere;
}
function validateDDDetails()
{
	//dd number validation
	var elem = document.getElementById("ddnumber");
	var val = elem.value;
	var res = checkddnumber(val);
	if(res == false)
	{
		errorMessage(elem,"DD Number Should be 6 to 12 characters");
		return;
	}

	
	//Date validation
	elem = document.getElementById("ddday");
	var day = document.getElementById("ddday").value;
	var month = document.getElementById("ddmonth").value;
	var year = document.getElementById("ddyear").value;
	var status = checkDDDate(day,month,year);
	if(status != "OK")
	{
		errorMessage(elem,status);
		return;
		
	}
	//bank name validation
	elem = document.getElementById("bankname");
	val = elem.value;
	res = checkName(val);
	if(res == false)
	{
		errorMessage(elem,"Minimum Length 3 characters and Enter Only Alphabets");
		return;
	}
		
	//bank branch validation
	elem = document.getElementById("branchname");
	val = elem.value;
	res = checkEmpty(val,2);
	if(res == false)
	{
		errorMessage(elem,"Enter Valid Branch Name");
		return;
	}
	
	var appformdiv = document.getElementById("applform");
	var ddformdiv = document.getElementById("ddform");
	if(appformdiv.style.visibility=='hidden')
	{
		appformdiv.style.visibility='visible';
		ddformdiv.style.visibility='hidden';
	}
}

function checkDDDate(d,m,y)
{

var d1 = new Date();
var curr_date = d1.getDate();
var curr_month = d1.getMonth();
var curr_year = d1.getFullYear();

	var res = checkInt(y);
	var x = 0;
	if (res == false)
	{
		return "Year Invalid";
	}
	
	if (y != curr_year)
	{
		return "Enter Valid Year";
	}

	res = checkInt(m);
	if (res == false)
	{
		return "Month Invalid";
	}
	if (m < 1 || m > 12)
	{
		return "Month Out of Range (1-12)";
	}

	if (m < 1 || m > curr_month + 1)
	{
		return "Please Enter Valid Month";
	}

	res = checkInt(d);

	if (res == false)
	{
		return "Day Invalid";
	}
	if (m==1 || m==3 || m ==5 || m == 7 || m == 8 || m == 10 || m == 12 )
	{
		x = 31;
	}
	else if (m==2)
	{
		if ((y % 4) == 0)
		{
			x = 29;
		}
		else
		{
			x = 28;
		}
	}
	else
	{
		x = 30;
	}

	if (d < 1 || d > x)
	{
		return "Date out of range (1 - " + x +")";
	}
	/** if(d<1 || d < curr_date )
	    return "Enter Valid Date"; **/
		
	return "OK";
}

function validateAppStatus()
{

	/*elem = document.getElementById("request");
	res = checkSelection(elem);
	if (res == false)
	 {
		errorMessage(elem, "Select Application Number / Registration Number ");
		return false;
	 }*/
	
	elem = document.getElementById("number");
	val = elem.value;
	
	res = checkEmpty(val,5);
	if(res == false)
	{
		errorMessage(elem,"Enter valid Application / Registration Number (should have minimum 5 digits) ");
		return false;
	}

	res = checkInt(val);
	if(res == false)
	{
		errorMessage(elem,"Enter valid Application / Registration Number (enter only numbers) ");
		return false;
	}

	elem = document.getElementById("security_code");
	val = elem.value;
	res = checkEmpty(val,5);
	if(res == false)
	{
		errorMessage(elem,"Enter Security Code ");
		return false;
	}
	// Everything Ok, Submit the form.
	


	
}

function validateViewResults()
{
	

	/*elem = document.getElementById("request");
	res = checkSelection(elem);
	if (res == false)
	 {
		errorMessage(elem, "Select Application Number / Registration Number ");
		return false;
	 }*/

// Sex 
	elem = document.getElementById("testtype");
	var booleanval = 0;
	for(var i=0;i<document.viewresults.testtype.length;i++)
	{
		if(document.viewresults.testtype[i].checked)
		{
			booleanval = 1;
		}
	}
	if(booleanval == 0)
	{
		errorMessage(elem,"Chosse test type");
		return false;
	}
	
	elem = document.getElementById("htno");
	val = elem.value;
	
	res = checkEmpty(val,7);
	if(res == false)
	{
		errorMessage(elem,"Enter valid hallticket (should have 7 digits) ");
		return false;
	}

	res = checkInt(val);
	if(res == false)
	{
		errorMessage(elem,"Enter valid hallticket Number (enter only numbers) ");
		return false;
	}

	elem = document.getElementById("security_code");
	val = elem.value;
	res = checkEmpty(val,5);
	if(res == false)
	{
		errorMessage(elem,"Enter Security Code ");
		return false;
	}
	// Everything Ok, Submit the form.
	


	
}
