var NS4 = (document.layers)?true:false;
var IE4 = (document.all)?true:false;	

function showpageinwawindow(url)
{
	window.open(url,"wa");
}

function ValidatorTrim(s) {
	
    var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}



var dtCh= "/";
var minYear=01;
var maxYear=99;
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;
}

// allows only numeric
	function numericOnly(e) {
		if(window.event) {
			key = e.keyCode; // for IE, e.keyCode or window.event.keyCode can be used
		}
		else if(e.which) {
			key = e.which; // netscape
		}
		else {
			return true;// no event, so pass through
		}
		
		if( ((key>45) && (key<58)) || (key==8) ) {
			return true; 
		}
		else {
			return false;
		}
		
		/*
		keychar = String.fromCharCode(key);
		reg = /\d/;
		return reg.test(keychar);
		*/
	}

function ddmmmyyyyToJsdate(datestr) {
	alert(datestr);
	var retarray = datestr.split("-");
	alert(retarray[0] + "/" + retarray[1] + "/" + retarray[2]);
	return(retarray[1] + "/" + retarray[0] + "/" + retarray[2]);
}



function dateTimeDiff( start, end, interval, rounding ) {

    var iOut = 0;

    // Create 2 error messages, 1 for each argument. 
    var startMsg = "Check the Start Date and End Date\n"
        startMsg += "must be a valid date format.\n\n"
        startMsg += "Please try again." ;
		
    var intervalMsg = "Sorry the dateAdd function only accepts\n"
        intervalMsg += "d, h, m OR s intervals.\n\n"
        intervalMsg += "Please try again." ;

    var bufferA = Date.parse(start) ;
    var bufferB = Date.parse(end) ;
    	
    // check that the start parameter is a valid Date. 
    if ( isNaN (bufferA) || isNaN (bufferB) ) {
        alert( startMsg ) ;
        return null ;
    }
	
    // check that an interval parameter was not numeric. 
    if ( interval.charAt == 'undefined' ) {
        // the user specified an incorrect interval, handle the error. 
        alert( intervalMsg ) ;
        return null ;
    }
    
    var number = bufferB-bufferA ;

    // what kind of add to do? 
    switch (interval.charAt(0))
    {
        case 'd': case 'D': 
            iOut = parseInt(number / 86400000) ;
            if(rounding) iOut += parseInt((number % 86400000)/43200001) ;
            break ;
        case 'h': case 'H':
            iOut = parseInt(number / 3600000 ) ;
            if(rounding) iOut += parseInt((number % 3600000)/1800001) ;
            break ;
        case 'm': case 'M':
            iOut = parseInt(number / 60000 ) ;
            if(rounding) iOut += parseInt((number % 60000)/30001) ;
            break ;
        case 's': case 'S':
            iOut = parseInt(number / 1000 ) ;
            if(rounding) iOut += parseInt((number % 1000)/501) ;
            break ;
        default:
        // If we get to here then the interval parameter
        // didn't meet the d,h,m,s criteria.  Handle
        // the error. 		
        alert(intervalMsg) ;
        return null ;
    }
    
    return iOut ;
}


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 trimval(str)
	{
	
	
	   return str.replace(/^\s+|\s+$/g, "");
	}

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 <= 2; 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/yy")
		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 != 2 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 2 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
}

function checkMultipleDots(strEmail) {
    var regExpInValid 	= /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
    var regExpValid 	= /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    if(!regExpInValid.test(strEmail) && regExpValid.test(strEmail)) {
		if(strEmail.indexOf(' ') <= 0) {
			return true;
		} else {
			return false;
		}
    } else {
		return false;
	}
    return false;
}

function emailcheck(string){
		
		// 			/ 		open search
		//			a-z 	charcters 
		//			\d		digit	
	  var invalidCharactersRegExp = /[^a-z\d.@_]/i; 
	  var isValid = !(invalidCharactersRegExp.test(string) );
	  return isValid;
}

function chkrepeat(string, str, num){
		var i =0;
		var myString = string;
		var index = myString.indexOf(str);
		while (index != -1) {
			index = myString.indexOf(str, index + 1); // start search after last match found
			i++;
		}
		if(i > num){
		  return false;
		}		
}

function checkEmail(strng)
{
	
	var error = "";
	var emailFilter=/^.+@.+\..{2,4}$/;
	if (strng == "") 
		{
		error = "You didn't enter an email-address.\n";
		}
	
	else if (!(emailFilter.test(strng))) 
		{ 
		error = "Please enter a valid email address.\n";
		}
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	if (strng.match(illegalChars))
		{
		error = "The email address contains illegal characters.\n";
		}
	return error;
}

function checkMailAddress(email) {
	var str = email;
	var invalidCharactersRegExp = /[^a-z\d\@\_\.-]/i; 
	
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.^\!^\#)/; // not valid
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; // valid
	
	if (!reg1.test(str) && reg2.test(str))
	{
		return true;
	}else{
		return false;
	}
	if(!invalidCharactersRegExp.test(str)) {
		return true;
	}else {
		return false;	
	}
}

function checkPassword (strng) 
{
	var error = "";
	var illegalChars = /[\W_]/; 
	if (strng == "") 
		{
		error = "You didn't enter password.\n";
		}
	
	else if ((strng.length < 5) || (strng.length > 15)) 
		{
  		error = "The password is of wrong length(valid length 5-15 chars).\n";
		}
	else if (illegalChars.test(strng)) 
		{
		error = "The password contains illegal characters.\n";
		}
	return error;
}

function chkselectedids(formname,objname)
{
	var chkflg,strmessageid,url,returl;
	strmessageid="";
	chkflg=false;
	for(j=0; j<document.forms.length;j++)
	{
		if(document.forms[j].name==formname)
		{
			for (i=0;i<document.forms[j].elements.length;i++)
			{
				if (document.forms[j].elements[i].name==objname) 
				{
					if (document.forms[j].elements[i].checked)
					{							
						chkflg=true;
						break;
					}
				}
			}			
		}	
	}
	return chkflg;	
}

function fnShowHint(form,hintid)  {

	window.open("itHelp.php?page="+form+"&context="+hintid, "Help", "statusbar=0, toolbar=0, menubar=0, scrollbar=0, resizable=0, height=400, width=400");
}

function isAlphabetic(val) {
	
	if (val.match(/^[a-zA-Z]+$/)) {
		return true;
	}
	else {
		return false;
	}	
}
function isAlphabetic_city(val) {
	
	if (val.match(/^[a-zA-Z]+\s*[a-zA-Z]+$/)) {
		return true;
	}
	else {
		return false;
	}	
}




// check to see if input is alphanumeric
function isAlphaNumeric(val) {
	
	if (val.match(/^[a-zA-Z0-9\-]+$/)) {
		return true;
	}
	else {
		return false;
	}
}

function removeSpaces(string) {
   var newString = '';
   for (var i = 0; i < string.length; i++) {
      if (string.charAt(i) != ' ') newString += string.charAt(i);
   }
   return newString;
}

//function to open in custom window
function openWindow(url,window_name,winWidth,winHeight,fscroll,resize) {
	
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	
	sLeft = (sWidth - winWidth) / 2;
	sTop = (sHeight - winHeight) / 2;

	if(fscroll == '') {fscroll = 0}
	if(resize == '') {resize = 'no'}
	
	window.open(url,window_name,"width=" + winWidth + ",height=" + winHeight + ",top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=" + fscroll + ",resizable="+resize);
}

function printSetup(pageName) {
	if(pageName == 'itPurchaseOrder.php') {
		var a = document.all.item("noprint");
	
		if (a!=null) {
			if (a.length!=null) {
				for (i=0; i< a.length; i++) {
					a(i).style.display = window.event.type == "beforeprint" ? "none" :"inline";
				}
			} 
			else { 
				a.style.display = window.event.type == "beforeprint" ? "none" :"inline";
			}
		}
	}
} 

function validatePrice(objPriceField, objEvent) {

	if(!objEvent.shiftKey && (objEvent.keyCode == 8 || objEvent.keyCode == 9 || objEvent.keyCode == 35 || objEvent.keyCode == 36 || objEvent.keyCode == 37 ||
		objEvent.keyCode == 39 || objEvent.keyCode == 46 || objEvent.keyCode == 110 || objEvent.keyCode == 190 ||
		(objEvent.keyCode >= 48 && objEvent.keyCode <= 57) || (objEvent.keyCode >= 96 && objEvent.keyCode <= 105))) {
		if(objPriceField.value.indexOf(".") >= 0 && (objEvent.keyCode == 190 || objEvent.keyCode == 110)) {
			return false;
		} else {
			return true;			
		}
	} else {
		return false;
	}
}

function roundNumber(nValue, nDecimal) {
	
	var nRoundedValue	= Math.round(nValue * Math.pow(10, nDecimal)) / Math.pow(10, nDecimal);
	return(nRoundedValue);
}

function isValidAmount(strAmount) {

	if(isNullText(strAmount)) {
		return false;
	} else {
		if(isNaN(strAmount)) {
			return false;
		} else {
			if(roundNumber(parseFloat(strAmount), 2) == 0) {
				return false;
			} else {
				return true;
			}
		}
	}
	return false;
}

function isNullText(strText) {

	strText = strText.replace(/[\r\n\s\'\"]/g,"");
	if(strText != "") {
		return false;
	}
	return true;
}

function dateRangeCheck(fromDate, toDate) {

	var dtFrom		= new Date(fromDate);
	var dtTo		= new Date(toDate);
	var dtCurrent	= new Date();	
	if(dtFrom > dtCurrent) {
		alert("From date must be less than the current date.");
		return false;
	} else if(dtTo > dtCurrent) {
		alert("To date must be less than the current date.");
		return false;
	} else if(dtFrom > dtTo) {
		alert("From date must be less than the To date.");
		return false;
	} else {
		return true;
	}
	return false;
}

function selectOption(selDateRange, txtFromDate, txtToDate, objFromCalendar, objToCalendar) {

	if(selDateRange.options[selDateRange.selectedIndex].value == "Custom") {
		objFromCalendar.disabled	= false;
		objToCalendar.disabled		= false;				
	} else {
		txtFromDate.value			= "";
		txtToDate.value				= "";
		objFromCalendar.disabled	= true;
		objToCalendar.disabled		= true;						
	} 
}

function selectPageRange(objForm) {
	objForm.submit();
}

var nCheckboxSelectedCount	= 0;

function selectCheckbox(objCheckbox, objSelectAll, objCheckBoxes) {

	if(objCheckbox.checked) {
		nCheckboxSelectedCount++;
	} else {
		nCheckboxSelectedCount--;
	}
	if(objSelectAll != null) {
		if(objSelectAll.checked) {
			objSelectAll.checked = false;
		} else {
			if(nCheckboxSelectedCount == objCheckBoxes.length) {
				objSelectAll.checked = true;
			}
		}
	}
}

function onSelectDeselect(nMode, objSelectAll) {
	if(objSelectAll.checked != nMode) {
		objSelectAll.click();
	}
}

function selectAllCheckbox(objCheckBox, objForm, checkBoxId) {
	
	var	nCount			= 0;	
	var checkBoxList	= eval("objForm." + checkBoxId);
	if(checkBoxList != null) {
		nCount	= checkBoxList.length;
	}
	
	if(nCount > 1) {
		for(i = 0; i < nCount; i++) {
			checkBoxList[i].checked	= objCheckBox.checked;
		}
		if(objCheckBox.checked) {
			nCheckboxSelectedCount	= nCount;
		} else {
			nCheckboxSelectedCount	= 0;
		}
	} else {
		if(checkBoxList != null) {
			checkBoxList.checked	= objCheckBox.checked;
			
			if(objCheckBox.checked) {
				nCheckboxSelectedCount	= 1;
			} else {
				nCheckboxSelectedCount	= 0;
			}
		}
	}
}

function validateUsername(objTextField, objEvent) {
	if(objEvent.shiftKey) {
		if(objEvent.keyCode == 188 || objEvent.keyCode ==190) {
			return false;
		}
	}
	return true;
}


