//initPage function
window.onload = initPage;
function initPage() {
	//1.set focus to first element
	if(document.forms[0])
	for(i = 0; i < document.forms[0].elements.length; i++) {
		try{
			document.forms[0].elements[i].focus();
			break;
		}catch(e){ continue;} 
	}
	if(window.initPageHook)
		initPageHook();
}
function clearFields(formobject) {
	//1.set focus to first element
	if(formobject)
	for(i = 0; i < formobject.elements.length; i++) {
		try{
			if(formobject.elements[i].type == "text" || formobject.elements[i].type == "password" || formobject.elements[i].type == "textarea"){
				formobject.elements[i].value = "";
			}
			if(formobject.elements[i].type == "checkbox") {
				formobject.elements[i].checked =false;
			}
		}catch(e){ continue;} 
	}
}
//validation message style
var validationAttributes = new Array();
var validationRegExp = new Array();
var validationMessages = new Array();
//required validation
validationAttributes[0] = "required";
validationRegExp[0] = /\w+/;
validationMessages[0] = "Please enter correct value in field: ";
//email validation
validationAttributes[1] = "email";
validationRegExp[1] = /^[a-zA-Z]*([\.-_]?\w+)+\@[\a-zA-Z0-9-_]+(\.[\a-zA-Z-_]+)+$/;
validationMessages[1] = "Please enter correct email id: ";
//phone number validation
validationAttributes[2] = "phone";
validationRegExp[2] = /^[0-9]+$/;
validationMessages[2] = "Please enter correct phone number: ";
//firstname lastname validation
validationAttributes[3] = "personname";
validationRegExp[3] = /^[a-zA-Z\'.]+$/;
validationMessages[3] = "Please enter correct name: ";
//username validation
validationAttributes[4] = "username";
validationRegExp[4] = /^[a-zA-Z0-9\'.]+$/;
validationMessages[4] = "Please enter correct username: ";
//password validation
validationAttributes[5] = "password";
validationRegExp[5] = /^[a-zA-Z0-9_.-]+$/;
validationMessages[5] = "Valid password characters are [a-zA-Z0-9_.-]\nPlease enter valid chracters: ";




function validateForm(formObject){
	for(i = 0; i < formObject.elements.length; i++) {
		for(j = 0; j < validationAttributes.length; j++) {
			if(formObject.elements[i].getAttribute(validationAttributes[j]) == 'Y') {
				if(validationAttributes[j] != "required" && !validationRegExp[j].test(formObject.elements[i].value)) {
					if(validationAttributes[j] != "required" && formObject.elements[i].value=="")
						continue;
					alert(validationMessages[j] + formObject.elements[i].title);
					formObject.elements[i].focus();
					return false;						
				} else {
					if(formObject.elements[i].value=="") {
						alert("Please enter value in the field");
						formObject.elements[i].focus();
						return false;
					}
				}
			}
		}
	}
	if(window.validationHook){
		if(validationHook(formObject)==false)
			return false;
	}
	return true;
}
function addEventRow(tableid, rowid){
	var tableobject = document.getElementById(tableid);
	var rowobject = document.getElementById(rowid);
	newrowobject = tableobject.insertRow(rowobject.rowIndex);
	var eventdata = rowobject.innerHTML;
	cell1 = newrowobject.insertCell();
	cell1.align = "right";
	cell1.innerHTML = '<input name="MOREEVENTDETAILS_KEY[]" type="text" class="input" id="MOREEVENTDETAILS_KEY[]" style="width:100px" />';
	cell2 = newrowobject.insertCell();
	cell2.innerHTML = '<input name="MOREEVENTDETAILS_VALUE[]" type="text" class="input" id="MOREEVENTDETAILS_VALUE[]" style="width:200px" />';
}
function moveListItems(fbox, tbox) {
	for (i = 0; i < tbox.options.length; i++) {
		if (tbox.options[i].text == "") {
			tbox.options[i] = null;	
		}
	}
	for (i=0; i < fbox.options.length; i++) {
		if (fbox.options[i].selected && fbox.options[i].value != "") {
			var no = new Option();
			no.value = fbox.options[i].value;
			no.text = fbox.options[i].text;
			tbox.options[tbox.options.length] = no;
			fbox.options[i] = null;
			i = i -1;
		}
	}
}
function selectOptions(listobject) {
	if(listobject.length==0){
		alert('Please assign sections to book');
		return false;
	} else {
		for(i = 0; i< listobject.length; i++){
			listobject.options[i].selected = true;
		}
		return true;
	}
	return false;
}
function selectOptionsToMove(){
	if(listobject.length>0){
		for(i = 0; i< listobject.length; i++){
			listobject.options[i].selected = true;
		}
	}
}
function Trim(value)
{
	var temp = value;
	var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
	if (obj.test(temp))
	{
		temp = temp.replace(obj, '$2');
	}
	var obj = /  /g;
	while (temp.match(obj))
	{
		temp = temp.replace(obj, " ");
	}
	return temp;
}
function submitForm(formid){
	var formobject = document.getElementById(formid);
	if(validateForm(formobject)){
		return formobject.submit();
	}
	return false;
}
function ToggleDisplay(objectid){
	var object = document.getElementById(objectid);
	if(object.style.display == 'block'){
		object.style.display ='none';
	} else {
		object.style.display ='block';
	}
}
function SwapDivImg(descid){
	MM_swapImgRestore();
	document.getElementById('contentdetails').style.display = 'none';
	document.getElementById('printdetails').style.display = 'none';
	document.getElementById('classdetails').style.display = 'none';
	if(descid==1){
		document.getElementById('contentdetails').style.display = 'block';
		MM_swapImage('Image1','','../../../../../Accountability%20Tracker/htdocs/common/images/book_description_grey.jpg',1);
	}
	if(descid==2){
		document.getElementById('printdetails').style.display = 'block';
		MM_swapImage('Image2','','../../../../../Accountability%20Tracker/htdocs/common/images/print_details_grey.jpg',1);
	}
	if(descid==3){
		document.getElementById('classdetails').style.display = 'block';
		MM_swapImage('Image3','','../../../../../Accountability%20Tracker/htdocs/common/images/class_grey.jpg',1);
	}
}

// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

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;
}

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;
}

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 );
}
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
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,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<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

var popUpWin=0;
function popUpWindow(URLStr, left, top, width, height)
{
  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menub ar=no,scrollbar=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

