var dialog;
String.prototype.trim = function() {
     return this.replace( /^\s*/, '').replace( /\s*$/, '' );
}
function IsAlphaNumeric(txtObj)
{
	var tmp = "";
	var digits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	for (var i=0; i<txtObj.length; i++)
	{
		tmp = txtObj.substring(i,i+1);
		if (digits.indexOf(tmp)==-1)
		{
			return false;
		}
	}
	if (txtObj == "")
		return false;
	else
		return true;
}
function IsName(txtObj)
{
	var tmp = "";
	//Important to include spacing at the end, so that the naming field allow spacing to be a valid character
	var digits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ./\()'-";
	for (var i=0; i<txtObj.length; i++)
	{
		tmp = txtObj.substring(i,i+1);
		if (digits.indexOf(tmp)==-1)
		{
			return false;
		}
	}
	if (txtObj == "")
		return false;
	else
		return true;
}
function containSpChar(txtObj)
{
	var tmp = "";
	var digits = ".,/\()'\":;?><~`!@#$%^&*|+=-{}[]";
	for (var i=0; i<txtObj.length; i++)
	{
		tmp = txtObj.substring(i,i+1);
		if (digits.indexOf(tmp)!=-1)
		{
			return true;
		}
	}
	if (txtObj == "")
		return false;
	else
		return false;
}
function IsDigit(txtObj)
{
	var tmp = "";
	for (var i=0; i<txtObj.length; i++)
	{
		tmp = txtObj.substring(i,i+1);
		if(isNaN(tmp))
		{
			return false;
		}
	}
	if(txtObj == "")
		return false;
	else
		return true;
}
function IsAlpha (txtObj)
{
	var tmp = "";
	//Important to include spacing at the end, so that the naming field allow spacing to be a valid character
	var digits = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
	for (var i=0; i<txtObj.length; i++)
	{
		tmp = txtObj.substring(i,i+1);
		if (digits.indexOf(tmp)==-1)
		{
			return false;
		}
	}
	if (txtObj == "")
		return false;
	else
		return true;
}
function IsLocalTel(phonenumber){
	if(!IsDigit(phonenumber) || phonenumber.substring(0, 1) != 6 || phonenumber.length < 8 || phonenumber.length > 8){
		return false;
	}else{
		return true;
	}
}
function IsOfficeTel(phonenumber){
	if(phonenumber.substring(0, 1) != 6 || phonenumber.length < 8 || phonenumber.length > 12){
		return false;
	}else{
		var tmp = "";
		var digits = "0123456789-";
		for (var i=0; i<phonenumber.length; i++)
		{
			tmp = phonenumber.substring(i,i+1);
			if (digits.indexOf(tmp)==-1)
			{
				return false;
			}
		}
	}
	return true;
}
function IsMobile(phonenumber){
	if(!IsDigit(phonenumber) || phonenumber.substring(0, 1) != 9 || phonenumber.length < 8 || phonenumber.length > 8){
		return false;
	}else{
		return true;
	}
}
function IsURL(website){	
	frontURL = String(website).substring(0, String(website).indexOf('.'));
	backURL = String(website).substring(String(website).indexOf('.')+1);
	if (String(website).indexOf('http')==-1 || String(website).indexOf('://')==-1 || String(website).indexOf('.') == -1 || frontURL.length <= 0 || backURL.length <= 0){
		return false;
	}else{
		return true;
	}
}
function IsValidFile(imagename, filetype){
	exts = filetype.split(",");
	for(i=0; i<exts.length;i++){
		if(String(imagename).toLowerCase().indexOf(exts[i].toLowerCase().trim()) > 0){
			return true;
		}
	}
	return false;
}
function checkEmail(email){
	substringEmail = String(email).substring(String(email).indexOf('.')+1)
	if (String(email) == "" || String(email).indexOf('@') == -1 || String(email).indexOf('.') == -1 || substringEmail.length <= 0) {
		return false;
	}else{
		return true;
	}
}

startday = new Date();
clockStart = startday.getTime()+1200000;
function initStopwatch() { 
	var myTime = new Date(); 
	return((clockStart - myTime.getTime())/1000); 
}
function getSecs(urlpath) { 
	var tSecs = Math.round(initStopwatch()); 
	var iSecs = tSecs % 60;
	var iMins = Math.round((tSecs-30)/60); 
	var sSecs ="" + ((iSecs > 9) ? iSecs : "0" + iSecs);
	var sMins ="" + ((iMins > 9) ? iMins : "0" + iMins);
	window.status = "Session will timeout in: " + sMins+":"+sSecs;
	window.setTimeout('getSecs(\'' + urlpath + '\')',1000);
	if(iSecs<=0 && iMins==1){
		if(confirm("We have not heard from you for some time.\nFor security purpose, your session will end in " + sMins+":"+sSecs + " min. Do you wish to continue?")){
			window.location.reload(true);	
		}else{
			window.location.href = urlpath;
		}
	}
}
function mouseMoving(tablewidth){
	if (event.clientX < screen.width){
		desc.style.left = event.clientX+document.body.scrollLeft-(tablewidth/2)+2;
	}
	desc.style.top = event.clientY+document.body.scrollTop+20;
}
function mouseMoveOut(){
	desc.style.visibility = "hidden";
}
function checkAll(ca){
	checkboxes = document.getElementsByTagName("input");
	if(isNaN(checkboxes.length)){
		ca.checked = checkboxes.checked;
	}else{
		for (i=0; i<checkboxes.length; i++){
			if(checkboxes[i].type=="checkbox"){
				checkboxes[i].checked = ca.checked;
			}
		}
	}
}
function setCheckAll(ca){
	cb = document.getElementsByTagName("input");
	if(isNaN(cb.length)){
		ca.checked = cb.checked;
	}else{
		var c = true;
		for(i=0; i<cb.length; i++){
			if(cb[i].type=="checkbox"){
				if(cb[i].checked == false && cb[i] != ca){
					c = false;
					break;
				}
			}
		}
		ca.checked = c;
	}
}	
function setSubmit(formname,path){
	formname.action = path;
}
function setBoxEnable(mainBox, subBox){
	if(mainBox.checked==false){
		if(!isNaN(subBox.length)){
			for(i=0; i<subBox.length;i++){
				subBox[i].disabled = false;				
			}
		}else{
			subBox.disabled = false;
		}
	}else{
		if(!isNaN(subBox.length)){
			for(i=0; i<subBox.length;i++){
				subBox[i].disabled = true;
			}
		}else{
			subBox.disabled = true;
		}
	}
}
function IsValidDate(theDay, theMonth, theYear){

	var maxDate = 31;
	var leapYear = false;
	theDay = parseInt(theDay, 10);
	theMonth = parseInt(theMonth, 10);
	theYear = parseInt(theYear, 10);

	if((theYear%4) == 0){
		leapYear = true;
	}
	thisYear = (new Date()).getFullYear();
	if(!IsDigit(theDay) || !IsDigit(theMonth) || !IsDigit(theYear) || theYear.length < 4){
		return false;
	}else if(theMonth <= 12){
		if(theMonth==2 && leapYear==true){		
			maxDate = 29;
		}else if(theMonth == 2 & leapYear==false){
			maxDate = 28;
		}else if(theMonth==1 || theMonth==3 || theMonth==5 || theMonth==7 || theMonth==8 || theMonth==10 || theMonth==12){
			maxDate = 31;
		}else{
			maxDate = 30;
		}
		if((theDay > maxDate) || (theDay<1) || (theMonth<1)){
			return false;
		}else{
			return true;
		}
		
	}else{
		return false;
		
	}
}
function IsRadioBtnChecked(groupname){
	//for radio btn that must at least have one selected.
	if(!isNaN(groupname.length)){
		for(i=0;i<groupname.length;i++){
			if(groupname[i].checked==true){
				return groupname[i].value;
			}
		}
	}else{
		if(groupname.checked==true){
			return groupname.value;
		}
	}
	return false;
}
function IsCheckBoxChecked(groupname){
	//for checkbox that must at least have one selected.
	checkValues = "";
	if(groupname !=null){
		if(!isNaN(groupname.length)){
			for(i=0;i<groupname.length;i++){
				if(groupname[i].checked==true){
					checkValues = checkValues + groupname[i].value;
					if((i+1)<groupname.length){
						checkValues = checkValues + ",";
					}
				}
			}
		}else{
			if(groupname.checked==true){
				checkValues = groupname.value;
			}
		}
	}
	if(checkValues.trim()!=""){
		return checkValues.trim();
	}else{
		return false;
	}
}
function specifyOthers(myArray, searchme){
	for(i=0;i<myArray.length;i++){
		if(myArray[i].trim()==searchme){
			return true;
		}
	}
	return false;
}
function IsStrMaxLen(str, maxLen){
	if(String(str).length <= maxLen){
		return true;
	}
	return false;
}
function setDateFields(dayField, mthField, yearField, minYear, maxYear, cDateDefault){
	if (cDateDefault==false){
	dayField.options[dayField.length] = new Option("-","");
	mthField.options[mthField.length] = new Option("-","");
	yearField.options[yearField.length] = new Option("-","");
	}
	for(i=1;i<=31;i++){
		dayField.options[dayField.length] = new Option(i,i);
	}
	allMonths = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	for(j=0;j<allMonths.length;j++){
		mthField.options[mthField.length] = new Option(allMonths[j],j+1);
	}
	for(k=minYear;k<=maxYear;k++){
		yearField.options[yearField.length] = new Option(k,k);
	}
	if (cDateDefault==true){
		setCurrentDateSel(dayField, mthField, yearField);
	}
}
function setCurrentDateSel(dayField, mthField, yearField){
	var currentDate = new Date();
	myDate = currentDate.getDate();
	myMth = currentDate.getMonth();
	myYear = currentDate.getYear();

	dayField.options[myDate-1].selected = true;
	mthField.options[myMth].selected = true;
	for(i=0;i<yearField.options.length;i++){
		if(yearField.options[i].value == myYear){
			yearField.options[i].selected = true;
			break;
		}
	}
}
function setTimeFields(hrField, minField, ttField, setTimeDefault){
	for(i=1;i<=12;i++){
		hrField.options[hrField.length] = new Option(i, i);
	}
	for(j=0;j<=59;j++){
		if(j.toString().length==1){
			strMin = "0" + j.toString();
		}else{
			strMin = j;
		}
		minField.options[minField.length] = new Option(strMin, strMin);
	}
	ttField.options[ttField.length] = new Option("AM", "AM");
	ttField.options[ttField.length] = new Option("PM", "PM");
	if (setTimeDefault==true){
		setCurrentTimeSel(hrField, minField, ttField);
	}else{
		hrField.options[11].selected = true;
	}
	
}
function setCurrentTimeSel(hrField, minField, ttField){
	var currentTime = new Date();
	myHr = currentTime.getHours();
	myMin = currentTime.getMinutes();

	if(myHr > 12){
		myHr -= 12;
		myTT = 1;
	}else{
		myTT = 0;
	}

	hrField.options[myHr-1].selected = true;
	minField.options[myMin].selected = true;
	ttField.options[myTT].selected = true;
}
function resetDate(dayField, mthField, yearField, emptyOption){
	loopDate = new Date(yearField.options[yearField.selectedIndex].value, mthField.options[mthField.selectedIndex].value, 1);
	diff = loopDate - 86400000;
	date2 = new Date(diff);
	daysInMonth = date2.getDate();
	if(emptyOption){
		days = dayField.length-1;
	}else{
		days = dayField.length;
	}
	tempVar = days - daysInMonth;
	if(tempVar<0){	
		tempVar*=-1;
		for(j=0;j<tempVar;j++){
			dayField.options[dayField.length] = new Option(dayField.length,dayField.length);
		}
	}else{
		for(j=0; j<tempVar; j++){
			dayField.remove(dayField.length-1);
		}
	}		
}
function decimal(num) {
string = "" + num;
if (string.indexOf('.') == -1)
return string + '.00';
seperation = string.length - string.indexOf('.');
if (seperation > 3)
return string.substring(0,string.length-seperation+3);
else if (seperation == 2)
return string + '0';
return string;
}
function ImgDimension(imagesrc, maxWidth, maxHeight) {
 	var im = new Image();
 	im.src = imagesrc;
	if(im.width > maxWidth || im.height > maxHeight || im.width < (maxWidth-5) || im.height < (maxHeight-5)){
		return true;
	}else{
		return false;
	}
}
function removetext(me,org_txt){
	if(me.value.trim()==org_txt){
		me.value="";
	}
}
function getColor(color_field,preview_color){
	var color = showModalDialog("include/popups/select_color.html", "","resizable: no; help: no; status: no; scroll: no;");
	if(color!=null){
	color_field.value = "#"+color;
	preview_color.style.backgroundColor = "#"+color;
	}
}
function PopulateOrderList(listbox, total, matchVal){
	for(i=listbox.length;i>0;i--){
		listbox.remove(i-1);
	}
	for(i=1; i<=total; i++){
		listbox.options[listbox.length] = new Option(i,i);
	}
	if(matchVal==""){
		matchVal = total;
	}
	listbox.options[matchVal-1].selected = true;
}
function PopulateListBox(listbox, opts, matchVal, combine, filterFor, removeDefault){
	if(removeDefault){
		minIndex = 0;
	}else{
		minIndex = 1;
	}
	for(i=listbox.length;i>minIndex; i--){
		listbox.remove(i-1);
	}
	for(i=0; i<opts.length; i++){
		if(combine){ // always combine index 0 & 1
			val = opts[i][0] + ":" + opts[i][1];
		}else{ //default checking uses index 0
			val = opts[i][0];
		}
		if(filterFor=="" || val==filterFor){
			listbox.options[listbox.length] = new Option(opts[i][3], opts[i][2]) //opts[][2] = value, opts[][3] = label
			if(matchVal==opts[i][2]){
				listbox.options[listbox.length-1].selected = true;
			}
		}
	}
	if(listbox.length==0){
		listbox.disabled =true;
	}else{
		listbox.disabled = false;
	}
	
}
function ChangeOrder(me){
	val = me.value - me.form["org_"+me.name].value;
	var tagSel = me.form.all.tags("select");
	for(k=0;k<tagSel.length;k++){
		if(tagSel[k].name != me.name){
			if(tagSel[k].value >= me.form["org_"+me.name].value && tagSel[k].value <= me.value){
				tagSel[k].options[tagSel[k].selectedIndex-1].selected = true;
			}else if(tagSel[k].value <= me.form["org_"+me.name].value && tagSel[k].value >= me.value){
				tagSel[k].options[tagSel[k].selectedIndex+1].selected = true;
			}
			me.form["org_"+tagSel[k].name].value = tagSel[k].options[tagSel[k].selectedIndex].value;
		}
	}
	me.form["org_"+me.name].value = me.value;
}
function reduceImgSize(me,fixed_width){
	if(me.width>fixed_width){
		ratio = fixed_width/me.width;
		me.width = Math.round(me.width*ratio);
	}
}
function select_page(){
	var winWidth=300;
	var winHeight=100;
	var winLeft = (window.screen.availWidth/2)-(winWidth/2);
	var winTop = (window.screen.availHeight/2)-(winHeight/2);
	dialog = window.open("/include/gotopage.asp", "thepage", "width="+winWidth+",height="+winHeight+",left="+winLeft+",top="+winTop);
	dialog.focus();
}
function focusDialog(){
    if(dialog && !dialog.closed){
       	dialog.focus();
  	}
}