function openWindow(URL, winName, width, height, fullscreen) {
	
	if(fullscreen){ 
		window.open(URL, winName, 'height=' + height + ',width=' + width + ',top = ' + (screen.availHeight - height) / 2 + ', left = ' + (screen.availWidth - width) / 2 + ', resizable=yes,status=no,toolbar=no,menubar=no,location=no, scrollbars=yes, fullscreen=yes')
	}else{
		window.open(URL, winName, 'height=' + height + ',width=' + width + ',top = ' + (screen.availHeight - height) / 2 + ', left = ' + (screen.availWidth - width) / 2 + ', resizable=yes,status=no,toolbar=no,menubar=no,location=no, scrollbars=yes')
	}
}

function openForm(strLeaveTrans) {
				
		var strHeight = 550
		var strWidth = 700
		var strTop = (screen.height - 550) / 2
		var strLeft = (screen.width - 700) / 2
		var strFeatures = 'height=' + strHeight + ', width=' + strWidth + ', top=' + strTop + ', left=' + strLeft + ', status=no, toolbar=no, menubar=no, location=no, resizable=no, scrollbars = yes'
		var strWindowName = 'LeaveRequestDetails'
		var strURL = 'viewApprovedLeaveRequest.asp?LeaveTransid=' + strLeaveTransid
		
		
		window.open(strURL, strWindowName, strFeatures)
}

function goToPage(sURL) {
	if((document.body.modulename == 'CreatePO' || document.body.modulename == 'EditPO' || document.body.modulename == 'CreateEC' || document.body.modulename == 'EditEC') && isFormModified) {
		if (!confirm('You are about to leave the form and all the modification will be loss. Are you sure to continue ?')){
			return
		}
	}
	window.location = sURL
}


function isDate(strDate) {
	
	


	var rexNumber = /^\-?\d+$/;
    
	var intCentYear = 30;
	
	var arrDate = strDate.split('/');

	
	if (arrDate.length != 3) return false
	
	if (!arrDate[1]) return  false
	
	if (!rexNumber.exec(arrDate[1])) return false
	
	if (!arrDate[2]) return false
	
	if (!rexNumber.exec(arrDate[2])) false
	
	if (!arrDate[0]) return false
	
	if (!rexNumber.exec(arrDate[0])) return false

	

	var dt_date = new Date();
	
	dt_date.setDate(1);

	
	if (arrDate[1] < 1 || arrDate[1] > 12) return false
	
	dt_date.setMonth(arrDate[1]-1);
	 
	
	if (arrDate[0] < 100) arrDate[0] = Number(arrDate[0]) + (arrDate[0] < intCentYear ? 2000 : 1900);
	
	if (arrDate[0] < 1900) return false
	
	dt_date.setFullYear(arrDate[0]);

	
	var dt_numdays = new Date(arrDate[0], arrDate[1], 0);
	
	dt_date.setDate(arrDate[2]);
	
	if (dt_date.getMonth() != (arrDate[1]-1)) return false

	

	return true

}



function getXMLHttpRequestObject() {
        
        var xmlHTTPReq
        
        if (window.XMLHttpRequest) {
            xmlHTTPReq = new XMLHttpRequest();
        }

        if (window.ActiveXObject) {	
            xmlHTTPReq = new ActiveXObject("Microsoft.XMLHTTP");
        }
        
        return xmlHTTPReq
    
    } 

 function getFormData(objForm) {
    
        var strPostData = ''
        
        for (var i=0;i<objForm.elements.length;i++) {
            strPostData += (strPostData == '' ? '' : '&') + objForm.elements[i].name + '=' + escape(objForm.elements[i].value)
        }
        
        return strPostData
    
    }

function DateAdd(timeU,byMany,dateObj) {
	var millisecond=1;
	var second=millisecond*1000;
	var minute=second*60;
	var hour=minute*60;
	var day=hour*24;
	var year=day*365;

	var newDate;
	var dVal=dateObj.valueOf();
	switch(timeU) {
		case "ms": newDate=new Date(dVal+millisecond*byMany); break;
		case "s": newDate=new Date(dVal+second*byMany); break;
		case "mi": newDate=new Date(dVal+minute*byMany); break;
		case "h": newDate=new Date(dVal+hour*byMany); break;
		case "d": newDate=new Date(dVal+day*byMany); break;
		case "y": newDate=new Date(dVal+year*byMany); break;
	}
	return newDate;
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function selectChkBox(oForm, strControlName, blnSelect) {
			
	for (var i = 0; i<oForm.elements.length; i++) {
		if (oForm.elements[i].type == 'checkbox' && oForm.elements[i].name == strControlName) {
			oForm.elements[i].checked = blnSelect
		}	
	}
	
}

function countChkBox(oForm, strControlName) {
			
	var chkboxCount = 0
	for (var i = 0; i<oForm.elements.length; i++) {
		if (oForm.elements[i].type == 'checkbox' && oForm.elements[i].name == strControlName) {
			if (oForm.elements[i].checked){
				chkboxCount++ 
			}
		}	
	}
	return chkboxCount
}

function synChkBox(oForm, strControlName) {

	var blnchkboxSelected
	
	for (var i = 0; i<oForm.elements.length; i++) {
		if (oForm.elements[i].type == 'checkbox' && oForm.elements[i].name == strControlName) {
			if (oForm.elements[i].checked) {
				blnchkboxSelected = true
				break
			}
		}	
	}
	
	oForm.SelectAll.checked = blnchkboxSelected
	
}
function debugAJAX(oForm, strFile2Test) {
			
    oForm.action = strFile2Test
    
    oForm.submit()

}

function processStringJS(strValue) {
    return strValue.replace('\'', '\\\'')
}

function hideAllSelectionBox() {
			
	for (var i = 0; i<document.forms[0].elements.length; i++) {
		
		if (document.forms[0].elements[i].type == 'select-one') {
			document.forms[0].elements[i].style.visibility = 'hidden'
		}	
	}
	
}

function showAllSelectionBox() {
			
	for (var i = 0; i<document.forms[0].elements.length; i++) {
		if (document.forms[0].elements[i].type == 'select-one') {
			document.forms[0].elements[i].style.visibility = ''
		}	
	}
	
}

function getFileNameFromURL(strURL) {
    return strURL.substring(strURL.lastIndexOf('/')+1)
}

function processEnglishName(strValue) {
	return strValue.replace(/[^a-zA-Z ]+/g, ' ').replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ').toUpperCase()
}

function IsFormDataValid(objForm, strValidationFile) {

	var xmlHTTPReq
	
	var strFormData = getFormData(objForm)

	xmlHTTPReq = getXMLHttpRequestObject()
    
	xmlHTTPReq.open("POST", strValidationFile, false);
        
	xmlHTTPReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");

	xmlHTTPReq.send(strFormData); 

    // if data is valid
	if (xmlHTTPReq.responseText.indexOf('invalid') == -1) 
	{ 	
		// Parsing RSS
		var validationresult = xmlHTTPReq.responseXML.documentElement; 
		
		// Get Channel information
		//alert(validationresult.getElementsByTagName('SQL').item(0).firstChild.data)
		var status = validationresult.getElementsByTagName('Status').item(0).firstChild.data;
		var reason = validationresult.getElementsByTagName('Reason').item(0).firstChild.data;
		
		if (status == 'Fail') {
			alert(reason)
			return false
		}
		else {
			return true
		}
	
	}
	else {
		alert('Form Data Validation Failed')
		return false
		
	}
			
}
