/************************
 * THE GLOBAL VARIABLES *
 ************************/

/*
 * regular expressions used when validating
 * the different types of input fields
 */
var r = {
	'notnumbers' : /[^\d\-]/g,
	'quotes'     : /['\''&'\"']/g,
	'date'       : /[^\d\/]/g,
	'currency'   : /[^\d\.-]/g,
	'phone'      : /[^\d\-()]/g,
	'cell'       : /[^\d\+]/g,
	'tin'        : /[^\d\-]/g,
	'sptin'      : /[^\d\-]/g 
}
var fileTypes=["bmp","gif","png","jpg","jpeg"];
/*
 * this array contains the list of possible IDs
 * for the integer fields in the form.
 */
var integerFieldIDs = new Array(
	"begno1", "begno2", "begno3", "begno4", "begno5",
	"endno1", "endno2", "endno3", "endno4", "endno5",
	"integer1", "integer2", "integer3", "integer4", "integer5",
	"integer6", "integer7", "integer8", "integer9", "integer10")

var begNoFieldIDs = new Array(
	"begno1", "begno2", "begno3", "begno4", "begno5")

var endNoFieldIDs = new Array(
	"endno1", "endno2", "endno3", "endno4", "endno5")

/*
 * this is the ID for the start and end number fields.
 */
var begNoFieldID = "begno"
var endNoFieldID = "endno"

/*
 * this array contains the list of possible IDs
 * for the date fields in the form.
 */
var dateFieldIDs = new Array(
	"begdate1", "begdate2", "begdate3", "begdate4", "begdate5",
	"enddate1", "enddate2", "enddate3", "enddate4", "enddate5",
	"date1", "date2", "date3", "date4", "date5",
	"date6", "date7", "date8", "date9", "date10", 
	"date11", "date12", "date13", "date14", "date15",
	"date16", "date17", "date18", "date19", "date20")

var begDateFieldIDs = new Array(
	"begdate1", "begdate2", "begdate3", "begdate4", "begdate5")

var endDateFieldIDs = new Array(
	"enddate1", "enddate2", "enddate3", "enddate4", "enddate5")

/*
 * this array contains the list of possible IDs
 * for the currency fields in the form.
 */
var currencyFieldIDs = new Array(
	"begcurr1", "begcurr2", "begcurr3", "begcurr4", "begcurr5",
	"endcurr1", "endcurr2", "endcurr3", "endcurr4", "endcurr5",
	"currency1", "currency2", "currency3", "currency4", "currency5",
	"currency6", "currency7", "currency8", "currency9", "currency10")

var begCurrFieldIDs = new Array(
	"begcurr1", "begcurr2", "begcurr3", "begcurr4", "begcurr5")

var endCurrFieldIDs = new Array(
	"endcurr1", "endcurr2", "endcurr3", "endcurr4", "endcurr5")

/*
 * this array contains the list of possible IDs
 * for the string fields in the form.
 */
var stringFieldIDs = new Array(
	"begcd1", "begcd2", "begcd3", "begcd4", "begcd5",
	"endcd1", "endcd2", "endcd3", "endcd4", "endcd5",
	"string1", "string2", "string3", "string4", "string5",
	"string6", "string7", "string8", "string9", "string10")

var begCodeFieldIDs = new Array(
	"begcd1", "begcd2", "begcd3", "begcd4", "begcd5")

var endCodeFieldIDs = new Array(
	"endcd1", "endcd2", "endcd3", "endcd4", "endcd5")

/*
 * this array contains the list of possible IDs
 * for the email fields in the form.
 */
var emailFieldIDs = new Array(
	"email1", "email2", "email3", "email4", "email5",
	"email6", "email7", "email8", "email9", "email10")

/*
 * this array contains the list of possible IDs
 * for the phone fields in the form.
 */
var phoneFieldIDs = new Array(
	"phone1", "phone2", "phone3", "phone4", "phone5",
	"phone6", "phone7", "phone8", "phone9", "phone10")

/*
 * this array contains the list of possible IDs
 * for the cellphone fields in the form.
 */
var cellphoneFieldIDs = new Array(
	"cell1", "cell2", "cell3", "cell4", "cell5",
	"cell6", "cell7", "cell8", "cell9", "cell10")

/*
 * this array contains the list of possible IDs
 * for the select fields in the form.
 */
var selectFieldIDs = new Array(
  "select1", "select2", "select3", "select4", "select5",
  "select6", "select7", "select8", "select9", "select10")

var tinboxIDs = new Array("tinbox1", "tinbox2", "tinbox3")
var sptinboxIDs = new Array("sptinbox1", "sptinbox2", "sptinbox3")

var fileUps = new Array("file1")

var addIDs = new Array("add1","add2")

/*************
 * varANTS *
 *************/

/*
 * varants defining the windows events
 */
 

var KEYUP_EVENT = "keyup"
var FOCUS_EVENT = "focus"
var BLUR_EVENT = "blur"
var LOAD_EVENT = "load"
var CHANGE_EVENT = "change"
var CLICK_EVENT = "click"

var PROPERTY_EVENT = "PropertyChange"

/*
 * varants defining the alignment of the value
 * in the input field
 */
var RIGHT_ALIGNMENT = "right"

/*
 * varants used in numeric validation
 */
var MAX_INTEGER_VALUE = "9999999999"
var MAX_CURRENCY_VALUE = "999999999.99"
var MAX_STRING_VALUE = "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
 
/*
 * varants used in date validation
 */
var DATE_SEPARATOR = "/"
var MN_YEAR = 1900
var MAX_YEAR = 3000

/*********************************************
 * LOADING OF WINDOW                         *
 * adding events to the form when the window *
 * is loading.                               *
 *********************************************/
addEvent(window, LOAD_EVENT, myScriptInit, false)


/*************
 * FUNCTIONS *
 *************/

/*
 * the actual adding of events for the various
 * input fields when the form is loading.
 */
function myScriptInit() {

	//test if there is DOM support for the browser
	//if not, the function is bypassed.
  if (!document.getElementById ||!document.getElementsByTagName || !document.createElement) {
		//alert("noDOMsupport")
    return
  }

  //setup the event listeners for the integer fields
	for (var i=0; i<=integerFieldIDs.length; i++) {
		if (document.getElementById(integerFieldIDs[i])) {
			var integerBox = document.getElementById(integerFieldIDs[i])
			//integerBox.style.textAlign = RIGHT_ALIGNMENT
			addEvent(integerBox, KEYUP_EVENT, numKey, false)
			addEvent(integerBox, LOAD_EVENT, initNo, false)
			addEvent(integerBox, FOCUS_EVENT, initNo, false)
			addEvent(integerBox, BLUR_EVENT, validateNo, false)
		}
	}

	//setup the event listeners for the date fields
	for (var i=0; i<=dateFieldIDs.length; i++) {
		if (document.getElementById(dateFieldIDs[i])) {
			var dateBox = document.getElementById(dateFieldIDs[i])
			addEvent(dateBox, KEYUP_EVENT, dateKey, false)
			addEvent(dateBox, FOCUS_EVENT, initDate, false)
			addEvent(dateBox, BLUR_EVENT, validateDate, false)
		}
	}

	//setup the event listeners for the currency fields
	for (var i=0; i<=currencyFieldIDs.length; i++) {
		if (document.getElementById(currencyFieldIDs[i])) {
			var currencyBox = document.getElementById(currencyFieldIDs[i])
			currencyBox.style.textAlign = RIGHT_ALIGNMENT
			addEvent(currencyBox, KEYUP_EVENT, currencyKey, false)
			addEvent(currencyBox, FOCUS_EVENT, initCurrency, false)
			addEvent(currencyBox, BLUR_EVENT, validateCurrency, false)
		}
	}

	//setup the event listeners for the string fields
	for (var i=0; i<=stringFieldIDs.length; i++) {
		if (document.getElementById(stringFieldIDs[i])) {
			var stringBox = document.getElementById(stringFieldIDs[i])
			addEvent(stringBox, FOCUS_EVENT, initString, false)
			addEvent(stringBox, BLUR_EVENT, validateString, false)
		}
	}

	//setup the event listeners for the email fields
	for (var i=0; i<=emailFieldIDs.length; i++) {
		if (document.getElementById(emailFieldIDs[i])) {
			var mailBox = document.getElementById(emailFieldIDs[i])
			addEvent(mailBox, FOCUS_EVENT, initString, false)
			addEvent(mailBox, BLUR_EVENT, validateMail, false)
		}
	}

	//setup the event listeners for the phone fields
	for (var i=0; i<=phoneFieldIDs.length; i++) {
		if (document.getElementById(phoneFieldIDs[i])) {
			var phoneBox = document.getElementById(phoneFieldIDs[i])
			addEvent(phoneBox, FOCUS_EVENT, initString, false)
			addEvent(phoneBox, KEYUP_EVENT, phoneKey, false)
		}
	}

	//setup the event listeners for the cellphone fields
	for (var i=0; i<=cellphoneFieldIDs.length; i++) {
		if (document.getElementById(cellphoneFieldIDs[i])) {
			var cellBox = document.getElementById(cellphoneFieldIDs[i])
			addEvent(cellBox, KEYUP_EVENT, cellKey, false)
			addEvent(cellBox, FOCUS_EVENT, initCell, false)
			addEvent(cellBox, BLUR_EVENT, validateCell, false)
		}
	}

	//setup the event listeners for the select fields
	for (var i=0; i<=selectFieldIDs.length; i++) {
		if (document.getElementById(selectFieldIDs[i])) {
			var combo = document.getElementById(selectFieldIDs[i])
	    addEvent(combo, CHANGE_EVENT, submitForm, false)
		}
	}
	
	for (var i=0; i <=tinboxIDs.length; i++) {
		if (document.getElementById(tinboxIDs[i])) {
			var tin = document.getElementById(tinboxIDs[i])
			addEvent(tin,KEYUP_EVENT,tinKey,false)
		}
	}
	
	for (var i=0; i <=sptinboxIDs.length; i++) {
		if (document.getElementById(sptinboxIDs[i])) {
			var sptin = document.getElementById(sptinboxIDs[i])
			addEvent(sptin,KEYUP_EVENT,sptinKey,false)
		}
	}
	
	//setup the event listeners for file file fields
	if (document.getElementById("file1")) {
	  
		var file = document.getElementById("file1")
		addEvent(file,CHANGE_EVENT,uploadImg,false)
			
		
	}
	if (document.getElementById("displaydiv")) {
		
		makeRequest()
	}
	
	if(document.getElementById("drugtest1")){
	var test = document.getElementById("drugtest1")
	  addEvent(test,CHANGE_EVENT,setDrugValue,false)
	}
}


/**************************
 * SPECIAL FORM FUNCTIONS *
 **************************/

/*
 * Cross-browser event handling for IE and Mozilla
 */
function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture)
		return true
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn)
		return r
	}
	else {
		elm['on' + evType] = fn
	}
}

/*
 * Get the target source
 */
function getSource(e) {
	//check the target object
	var t = window.event ? window.event.srcElement : e ? e.target : null
	return t
}

/*
 * Get the key events depending on the browser type.
 */
function getKey(e) {
	if (e && e.which) {
		var code = e.which
	}
	else if (e && e.keyCode) {
		var code = e.keyCode
	}
	else if (window.event && window.event.keyCode) {
		var code = window.event.keyCode
	}
	else {
		return
	}
	return code
}

/*
 * This is called on an onchange event for a select field.
 */
function submitForm() {
	document.myForm.submit()
}

/************************
 * KEYPRESSED FUNCTIONS *
 ************************/

/*
 * Keypress for the numeric field.
 */
function numKey (e) {
	var t = getSource(e)
	var code = getKey(e)
	if (!t) {
		return
	}
	//var character = String.fromCharCode(code).toLowerCase()
	t.value = t.value.replace(r['notnumbers'],'')
}

/*
 * Keypress for the date field.
 */
function dateKey (e) {
	var target = getSource(e)
	var code = getKey(e)
	if (!target) {
		return
	}
	else {
		if (target.value.length > 13) {
			target.value = target.value.substring(0,10)
		}
		target.value = dateOnKeyDown(target,code)
		target.value = target.value.replace(r['date'],'')
	}
}

/*
 * Keypress for the currency field.
 */
function currencyKey (e) {
	var target = getSource(e)
	var code = getKey(e)
	if (!target) {
		return
	}
	else {
		var k=String.fromCharCode(code)
		var p=new String(target.value+k).indexOf(".")
		if((p<target.value.length-3) && p>-1 ) {
			target.value = target.value.substring(0, target.value.length -1)
		}
		if (target.value.indexOf("-") > 0) {
			target.value = target.value.substring(0, target.value.length -1)
		}
		target.value = target.value.replace(r['currency'],'')
	}
}

/*
 * Keypress for the phone numbers field.
 */
function phoneKey(e) {
	var target = getSource(e)
	if (!target) {
		return
	}
	else {
		target.value = target.value.replace(r['phone'],'')
	}
}

/*
 * Keypress for the cellphone numbers field.
 */
function cellKey(e) {
	var target = getSource(e)
	var code = getKey(e)
	if (!target) {
		return
	}
	else {
		target.value = getCell(target,code)
		target.value = target.value.replace(r['cell'],'')
	}
}
/*
 * Keypress for the tin fields.
 */
function tinKey (e) {
	var target = getSource(e)
	var code = getKey(e)
	if (!target) {
		return
	}
	else {
		if (target.value.length > 12) {
			target.value = target.value.substring(0,11)
		}
		target.value = tinKeyDown(target,code)
		target.value = target.value.replace(r['tin'],'')
	}
}

function sptinKey (e) {
	var target = getSource(e)
	var code = getKey(e)
	if (!target) {
		return
	}
	else {
		if (target.value.length > 16) {
			target.value = target.value.substring(0,15)
		}
		target.value = sptinKeyDown(target,code)
		target.value = target.value.replace(r['sptin'],'')
	}
}

/*************************************************
 * CALLED FUNCTIONS FOR THE KEYPRESSED FUNCTIONS *
 *************************************************/

function dateOnKeyDown(obj, keycode) {
	var strLength = obj.value.length
	switch(strLength) {
		case 1:
			if (obj.value > 1 ) {
				obj.value = "0" + String.fromCharCode(keycode) + "/"
			}
			break
		case 2:
			if (obj.value > 12 || obj.value < 1) {
				obj.value = obj.value.substring(0,strLength-1)
			}
			else {
				obj.value = obj.value + "/"
			}
			break
		case 5:
			var dateparams = obj.value.split('/')
			var d = dateparams[1]
			if (d > 31 || d < 1) {
				obj.value = obj.value.substring(0,strLength -1)
			}
			else {
				obj.value = obj.value + "/"
			}
			break
		case 10:
			var dateparams = obj.value.split('/')
			var y = dateparams[2]
			var m = dateparams[0]
			var d = dateparams[1]
			if (y < MIN_YEAR) {
				obj.value = m + "/" + d + "/" + MIN_YEAR
			}
			break
		case 11:
			obj.value = obj.value.substring(0,10)
 	}

	//backsapce
	if (keycode == 8) {
		switch(strLength) {
			case 2:
				obj.value = obj.value.substring(0,1)
				break
			case 5:
				obj.value = obj.value.substring(0,4)
		}
	}
	return (obj.value) //return false to set value to obj
}

function tinKeyDown(obj, keycode) {
	var strLength = obj.value.length
	//backsapce
	if (keycode == 8) {
		switch(strLength) {
			
			case 3:
				obj.value = obj.value.substring(0,2)
				break;
			case 7:
				obj.value = obj.value.substring(0,6)
				break
		}
	} else {

		switch(strLength) {
			case 3:
			
				obj.value = obj.value  + "-"
			
				break
			case 7:
				obj.value = obj.value +  "-"
				break
			case 12:
				obj.value = obj.value.substring(0,11)
 		}
	}
	
	
	return (obj.value) //return false to set value to obj
}

function sptinKeyDown(obj, keycode) {
	var strLength = obj.value.length
	//backsapce
	if (keycode == 8) {
		switch(strLength) {
			
			case 3:
				obj.value = obj.value.substring(0,2)
				break;
			case 7:
				obj.value = obj.value.substring(0,6)
				break;
			case 11:
				obj.value = obj.value.substring(0,10)
				break
		}
	} else {

		switch(strLength) {
			case 3:
			    obj.value = obj.value  + "-"
				break
			case 7:
				obj.value = obj.value +  "-"
				break
			case 11:
				obj.value = obj.value +  "-"
				break
			case 16:
				obj.value = obj.value.substring(0,15)
 		}
	}
	
	
	return (obj.value) //return false to set value to obj
}

function getCell(obj, code) {
	var strl = obj.value.length
	if ((obj.value == "")|| (obj.value == null)) {
		obj.value = "+63" + String.fromCharCode(code)
	}
	if (strl > 13) {
		obj.value = obj.value.substring(0,13)
	}
	return (obj.value)
}


/************************
 * VALIDATION FUNCTIONS *
 ************************/

/*
 * Validate the integer fields
 */
function validateNo(e) {
	var n = getSource(e)
	if ((n.value == "") || (n.value == null)) {
		n.value = "0"
	}
	//validate the end numbers fields
	for (var i=0; i<=endNoFieldIDs.length; i++) {
		if (n.id == endNoFieldIDs[i]) {
			if (n.value == "0") {
				n.value = MAX_INTEGER_VALUE
			}
			//get the name of the begno field
			endNoFieldID = n.id
			pos1 = endNoFieldID.length
			fieldIDNo = endNoFieldID.substring(endNoFieldID.length-1)
			begNoFieldID = "begno" + fieldIDNo
			begNo = document.getElementById(begNoFieldID)
			endNo = n
			if (endNo.value < begNo.value) {
				logMessage("The end number should be greater than or equal to the start number!")
			}
			else {
				logMessage("")
			}
		}
	}
}

/*
 * Validate the date field.
 */
function validateDate(e){
	var dt = getSource(e)
	//validate all dates
	//test only if the value is not empty or null
	 
	if ((dt.value != "") && (dt.value != null)) {
		if (isDate(dt.value) == false || dt.value.length != 10) {
			
			//alert("Invalid date: (mm/dd/yyyy)")
			//fantonog to set focus on the error field
			setTimeout(function() {
        				dt.disabled = false
        				dt.focus()	
    							}, 50)

				
			return false
		}
		else {
			logMessage("")
			
		}
	}else{
		if(dt.readOnly==true){
			
		}else{	
		//alert("Date Empty")	
		   
		}
	}
	//validate begdates
	for (var i=0; i<=begDateFieldIDs.length; i++) {
		if (dt.id == begDateFieldIDs[i]) {
			if ((dt.value == "") || (dt.value == null)) {
				alert("Please fill up the start date field!")
			}
		else {
			logMessage("")
		}
		}
	}
	//validate enddates
	for (var i=0; i<=endDateFieldIDs.length; i++) {
		if (dt.id == endDateFieldIDs[i]) {
			if ((dt.value == "") || (dt.value == null)) {
				logMessage("Please fill up the end date field!")
			}
			else {
				logMessage("")
			}
			//get the begdate field ID
			endDateFieldID = dt.id
			pos1 = endDateFieldID.length
			fieldIDNo = endDateFieldID.substring(endDateFieldID.length-1)
			begDateFieldID = "begdate" + fieldIDNo
			begDate = document.getElementById(begDateFieldID)
			endDate = dt
			//test if enddate value is greater than the begdate value
			var rBegDate = reverseDate(begDate.value)
			var rEndDate = reverseDate(endDate.value)
			if (rEndDate < rBegDate) {
				logMessage("The end date should be greater than or equal to the start date!")
				return false
			}
			else {
				logMessage("")
			}
		}
	}
}

/*
 * Validate the currency field.
 */
function validateCurrency(e){
	var currency = getSource(e)
	var filter =  /[^\d-]+(\.\d\d)?$/
	var invalidValue = filter.test(currency.value)
	var pos = currency.value.indexOf(".")
	if (invalidValue) {
		currency.value = "0.00"
	}
	else {
		if (pos == -1) {
			if ((currency.value == "") || (currency.value == null)) {
				currency.value = "0.00"
			}
			else {
				currency.value = currency.value + ".00"
			}
		}
		else {
			var x = currency.value.split(".")
			if (x[1].length > 2) {
				currency.value = x[0] + "." + x[1].substring(0,2)
			}
			else if (x[1].length == 1) {
				currency.value = x[0] + "." + x[1] + "0"
			}
		}
	}

	//validate the end currency fields
	for (var i=0; i<=endCurrFieldIDs.length; i++) {
		if (currency.id == endCurrFieldIDs[i]) {
			if ((currency.value == "") || (currency.value == null) ||
			    (currency.value == ".00") || (currency.value == "0.00")) {
				currency.value = MAX_CURRENCY_VALUE
			}
			//get the name of the begcurr field
			endCurrFieldID = currency.id
			pos1 = endCurrFieldID.length
			fieldIDCurr = endCurrFieldID.substring(endCurrFieldID.length-1)
			begCurrFieldID = "begcurr" + fieldIDCurr
			begCurr = document.getElementById(begCurrFieldID)
			endCurr = currency
			if (endCurr.value < begCurr.value) {
				logMessage("The end currency value should be greater than or equal to the start currency value!")
			}
			else {
				logMessage("")
			}
		}
	}
}

/*
 * Validate the text field.
 */
function validateString(e){
	var st = getSource(e)

	//validate the end string fields
	for (var i=0; i<=endCodeFieldIDs.length; i++) {
		if (st.id == endCodeFieldIDs[i]) {
			if ((st.value == "") || (st.value == null)) {
				st.value = MAX_STRING_VALUE
			}
			//get the name of the begCode field
			endCodeFieldID = st.id
			pos1 = endCodeFieldID.length
			fieldIDCode = endCodeFieldID.substring(endCodeFieldID.length-1)
			begCodeFieldID = "begcd" + fieldIDCode
			begCode = document.getElementById(begCodeFieldID)
			endCode = st
			if (endCode.value < begCode.value) {
				logMessage("The end code value should be greater than or equal to the start code value!")
			}
			else {
				logMessage("")
			}
		}
	}
}

/*
 * Validate the email address field.
 */
function validateMail(e) {
	var target = getSource(e)
	var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
	var returnval=emailfilter.test(target.value)
	if (returnval==false){
		if ((target.value == "") || (target.value == null)) {
			target.value = ""
		}
		else {
			target.value = "invalid"
		}
	}
}

/*
 * Validate the cellphone number field.
 */
function validateCell(e) {
	var target = getSource(e)
	if ((target.value.charAt(0) != "+") && (target.value.length < 13)) {
		target.value = "invalid"
		return false
	}
	if (target.value == "+63") {
		target.value = ""
	}
}

/******************
 * INIT FUNCTIONS *
 ******************/

/*
 * Initialize the value in an integer field
 */
function initNo(e) {
	var target = getSource(e)
	if ((target.value == "") || (target.value == null)) {
		target.value = "0"
	}
	target.select()
}

/*
 * Initialize the value in a currency field
 */
function initCurrency(e) {
	var target = getSource(e)
	if ((target.value == "") || (target.value == null)) {
		target.value = "0.00"
	}
	target.select()
}

/*
 * Initialize the value in an ordinary text field
 */
function initString(e) {
	var target = getSource(e)
	if (target.value == null) {
		target.value = ""
	}
	target.select()
}

/*
 * Initialize the value in a cellphone field
 */
function initCell(e) {
	var target = getSource(e)
	if ((target.value == "") || (target.value == null)) {
		target.value = "+63"
	}
	target.select()
}

/******************
 * DATE FUNCTIONS *
 ******************/

/*
 * Determine if a given string is a valid date value.
 */
function isDate(dtStr){
	var daysInMonth = daysArray(12)
	var pos1=dtStr.indexOf(DATE_SEPARATOR)
	var pos2=dtStr.indexOf(DATE_SEPARATOR,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) {
		strDay=strDay.substring(1)
	}
	if (strMonth.charAt(0)=="0" && strMonth.length>1) {
		strMonth=strMonth.substring(1)
	}
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) {
			strYr=strYr.substring(1)
		}
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		//alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		// alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		//alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<MIN_YEAR || year>MAX_YEAR){
		//alert("Please enter a valid 4 digit year between "+MIN_YEAR+" and "+MAX_YEAR)
		return false
  }
	if (dtStr.indexOf(DATE_SEPARATOR,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, DATE_SEPARATOR))==false){
		//alert("Please enter a valid date")
		return false
	}
	return true
}

/*
 * This will expect a date in the format mm/dd/yyyy
 * and return a date value in the yyyymmdd format.
 */
function reverseDate(inputDate) {
	var pos1 = inputDate.indexOf(DATE_SEPARATOR)
	var pos2 = inputDate.indexOf(DATE_SEPARATOR,pos1+1)
	var mm   = inputDate.substring(0,pos1)
	var dd   = inputDate.substring(pos1+1,pos2)
	var yy   = inputDate.substring(pos2+1)

	var reversedDate = yy + mm + dd
	return reversedDate
}

/*
 * Initialize the value in a date field to an empty string,
 * if the content is "mm/dd/yy" or "MM/DD/YYYY"
 */
function initDate(e) {
	var dt = getSource(e)
	if (dt.value == "mm/dd/yyyy" || dt.value == "MM/DD/YYYY") {
		dt.value = ""
	}
	dt.select()
}

/**********************
 * DATE SUB-FUNCTIONS *
 **********************/

/*
 * Determine the last day of February,
 * given the year.
 */
function daysInFebruary (year){
	//february has 29 days in any year evenly divisible by four,
	//EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 )
}

/*
 * Determine the last days of the month.
 */
function daysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {
			this[i] = 30
		}
		if (i==2) {
			this[i] = 29
		}
	}
	return this
}

//test if a number is an integer
function isInteger(s){
	var i
	for (i = 0; i < s.length; i++){
		// Check that current character is number.
		var c = s.charAt(i)
		if (((c < "0") || (c > "9"))) {
			return false
		}
	}
	//all characters are numbers.
	return true
}

//remove all characters not in the bag
//and return as a string
function stripCharsInBag(s, bag){
	var i
	var returnString = ""
	//search through string's characters one by one.
	//if character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++){
		var c = s.charAt(i)
		if (bag.indexOf(c) == -1) {
			returnString += c
		}
	}
	return returnString
}

/*
 * Log messages on the browser's status bar
 */
function logMessage(message) {
	window.status = message
	return true
}


function uploadImg(e){
	
	var source = document.getElementById("file1").value	
	var browser=navigator.appName;
	var b_version=navigator.appVersion;
	var version=parseFloat(b_version);
	var flag = 0;
	if (browser== "Microsoft Internet Explorer") {
		
		var tmp = source.split("\\");
		for (var i=0; i<tmp.length; i++) {
			source = tmp[i]
		}	
			
	}
		
	var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
    for (var i=0; i<fileTypes.length; i++){
    	 if (fileTypes[i]==ext) {
    	 	flag =1;
    	 	 break;
    	 }
    }
    if (flag>0) {
  	
    document.myForm.submit();
    show_progressbar("loading");
    // setTimeout("clicked()",5000)
    }else {
    	alert("invalid Image type")
    } 
}

//set the value to NEGATIVE
function setDrugValue(e)
{
	document.myForm.ObjValue28.value="NEGATIVE"
}





//--AJAX


