function trim(txt)
{
	var newstr = txt.replace(/^\s*/,"");
	newstr = newstr.replace(/\s*$/,"");
	return newstr;
}

function check_date(dates)
{
	var vdate = dates.split("/");
	var mth;
	
	if (vdate.length != 3) {
		return false;
	}

	if (vdate[0].charAt(0) == '0') {
		vdate[0] = vdate[0].charAt(1);
	}	
	
	var day = parseInt(vdate[0]);
	//special treatment for mth '08' & '09' only. dont know why js can't recognise
	if (vdate[1] == '08') {
		mth = 7;
	} else if (vdate[1] == '09') {
		mth = 8;
	} else {
		mth = parseInt(vdate[1]) - 1;
	}			
	var yr = parseInt(vdate[2]);
	var curr_date = new Date(yr, mth, day);
	var system_yr = curr_date.getYear();

	//it seems that for 19th century yr, it didnt show as 19xx. just xx. so need to add 1900.
	if ((yr - 1900) < 100) {
		system_yr += 1900;
	}	

	if (yr == system_yr && (mth == curr_date.getMonth()) && (day == curr_date.getDate())) {
		return true;
	}
	return false;
}

function popup_window2(myurl, widths, heights, tops, lefts) {
  txt = 'width=' + widths + ', height=' + heights + ', top=' + tops + ', left=' + lefts + ', resizable=1, scrollbars=yes' ;
  popup = window.open(myurl, 'popup_window2', txt);
  popup.focus();
}

function change_pg(docs, tot_pg)
{
	var doc = document.auction;
		
	if (docs == 'submit1') { //this to submit prop. search (located at left-hand prop. main page)
		doc.act.value = 1;
		doc.AM_id.value = 0;
	} else if (docs == 'submit2') { //refresh location in add/edit property (purpose of refresh: change mukim accordingly)
		doc.act.value = '';
	}	
	
	if (docs == 'submit1' || docs == 'submit2') {
		doc.submit();
		return;
	}	
	
	//this will be used to show prop listing for both admin & user. used in dropdown & prev/next button
	if (docs != 'action_button') {
		if (!tot_pg) {
			eval('var doc1 = document.' + docs + ';');	
			doc.pg.value = doc1.pg[doc1.pg.selectedIndex].value;
		} else {
			doc.pg.value = tot_pg;
		}		
	} else if (docs == 'action_button') { //to export property list in xls format
		doc.action.value = 'admin_export';
	}	
	doc.submit();
}	

function MM_jumpMenuCIMBGroup(targ,selObj,restore){ //v3.0
    if (selObj.options[selObj.selectedIndex].value != ""){
      window.open(selObj.options[selObj.selectedIndex].value, targ);
      if (restore) selObj.selectedIndex=0;
    }
}

function check_loc_dist(docs, acts)
{
	eval('doc = document.' + docs + ';');
	
	y = 0;
	txt = 'Location';
	if (doc.action.value == 'admin_dist') {
		txt = 'District';
	}	
	
	if (acts == 'add') {
		if (!trim(doc.keyword.value)) {
			alert('Please key in ' + txt + ' Name');
			doc.keyword.focus();
			return false;
		}	
	} else {	
		for (x = 0; x < doc.length; x++) {
			if (doc.elements[x].type == 'checkbox' && doc.elements[x].checked) {
				if (acts == 'update' && !trim(doc.elements[x - 1].value)) {
					alert('Please key in ' + txt + ' data for update purposes');					
					doc.elements[x - 1].focus();
					return false;
				}					
				y++;
			}				
		}
		if (!y) {
			alert('Please check at least 1 checkbox to Update / Delete!');
			return false;
		}	else {
			if (!window.confirm('Are you sure you want to ' + acts + ' the selected ' + y + ' record(s)?')) {
				return false;
			}	
		}
	}
	
	doc.act.value = acts;
	doc.submit();
}

function check_prop(acts)
{
	var doc = document.auction;

	var inputs = new Array('', '', '', 'Reference', 'Description', 'Reserve Price', 'Market Price', 'Address');
	inputs[8] = 'Land/Floor Area';
	inputs[9] = 'Solicitor';
	inputs[10] = 'Auctioneer';
	inputs[11] = 'Auction Date';
	inputs[12] = 'Effective Date ';
	inputs[13] = 'Restriction';
	inputs[14] = 'Status';
	inputs[15] = 'Attachment';
	
	if (acts == 'delete') {
		if (!window.confirm('Are you sure you want to delete this property?')) {
			return false;
		}	
	} else {		
		for (x = 3; x < doc.length - 5; x++) {
			txt = '';
			if ((doc.elements[x].type.toLowerCase() == 'text' || doc.elements[x].type.toLowerCase() == 'file') && !trim(doc.elements[x].value)) {
				txt = inputs[x] + ' field cannot be blank';
				if (doc.elements[x].name == 'AC_img_name') {
					txt = '';
				}	
			} 
			if (doc.elements[x].name == 'AC_reserve_price' || doc.elements[x].name == 'AC_market_price' || doc.elements[x].name == 'AC_floor_area') {
				if (isNaN(doc.elements[x].value) || parseFloat(doc.elements[x].value) <= 0) {
					txt = 'Invalid number for ' + inputs[x];
				}	
			}
			if ((doc.elements[x].name == 'AC_date' || doc.elements[x].name == 'AC_start_date') && !check_date(doc.elements[x].value)) {
				txt = 'Invalid date format for ' + inputs[x];
			}
			
			if (txt) {
				alert(txt);
				doc.elements[x].focus();
				return false;
			}	
		}	
	}
	doc.act.value = acts;
	doc.submit();
}

function close_popup(txt)
{	
	alert(txt);
	window.close();
	opener.history.go();
	opener.focus();    
}	

function check_bulk()
{
	var doc = document.auction;
	if (trim(doc.AC_start_date.value) && !check_date(doc.AC_start_date.value)) {
		alert('Invalid date format for Effective Date');
		doc.AC_start_date.focus();
		return false;
	}	
	
	return true;
}	

function check_time(times)
{
	var vtime = times.split(":");
	
	if (vtime.length != 2) {
		return false;
	}
	
	hr = vtime[0];
	min = vtime[1];

	if (isNaN(hr) || isNaN(min)) {
		return false;
	}	
	
	
	//special treatment for '08' & '09' only. dont know why js can't recognise
	if (hr == '08') {
		hr = 8;
	} else if (hr == '09') {
		hr = 9;		
	}

	if (min == '08') {
		min = 8;
	} else if (min == '09') {
		min = 9;		
	}
	
	hr = parseInt(hr);
	min = parseInt(min);
	
	if (hr < 0 || hr > 23 || min < 0 || min > 59) {
		return false;
	} else {
		return true;
	}	
}	

function check_calendar(dt)
{
	var doc = document.auction;

 	if (dt != undefined && dt.indexOf('action=admin_upload_prop') != -1) {
		if (!window.confirm('Are you sure you want to delete the selected record?')) {
			history.go();
			return true;
		} else {
			location.href = 'auction.php?' + dt;
			return true;
		}
	}	
	
	if (dt != undefined && dt) {
		if (!window.confirm('Are you sure you want to delete the selected record?')) {
			history.go();
		} else {
			vals = dt.split('~!~');
			doc.dt.value = vals[0];
			doc.venue.value = vals[1];
			doc.submit();
			return true;
		}		
	}	
		
	if (!check_date(trim(doc.AD_date.value))) {
		alert('Invalid date format');
		doc.AD_date.focus();
		return false;
	}
	if (!check_time(trim(doc.AD_time.value))) {
		alert('Invalid time format');
		doc.AD_time.focus();
		return false;
	}
	
	if (!trim(doc.AD_venue.value)) {
		alert('Venue field cannot be blank');
		doc.AD_venue.focus();
		return false;
	}
	
	for (x = 0; x < doc.elements.length; x++) {
		if (doc.elements[x].type == 'select-multiple') {			
			y = x;
			break;
		}	
	}		
		
	if (!doc[y].length) {
		alert('Please key in at least 1 combination of Auctioneer Info (name & phone)');
		doc.AD_name.focus();
		return false;
	}	

	for (x = 0;  x < doc[y].length; x++) {		
		doc[y].options[x].selected = true;		
	}	

	return true;
}	

function addremove_auc(acts) 
{
	var doc = document.auction;

	for (x = 0; x < doc.elements.length; x++) {
		if (doc.elements[x].type == 'select-multiple') {			
			y = x;
			break;
		}	
	}
	
	numbers = (doc[y].length) - 1;
	
	if (acts == 'add') {
		val1 = trim(doc.AD_name.value);
		if (!val1) {
			alert('Auctioneer Name cannot be blank');
			doc.AD_name.focus();
			return false;
		}	
		val2 = trim(doc.AD_phone.value);
		if (!val2) {
			alert('Auctioneer Phone cannot be blank');
			doc.AD_phone.focus();
			return false;
		}	
		doc[y].options[numbers + 1] = new Option();
		doc[y].options[numbers + 1].text = val1 + ', ' + val2;
		doc[y].options[numbers + 1].value = val1 + '~!~' + val2;		
	} else {
		if ((numbers + 1) > 0) {
			z = 0;
			id_arr = new Array();
			id_arr1 = new Array();			
			for (x = 0; x < numbers + 1; x++) {
				if (!doc[y].options[x].selected) {
					id_arr[z] = doc[y].options[x].value;
					id_arr1[z] = doc[y].options[x].text;
					z++;
				} else {
					tmp_val = doc[y].options[x].value.split("~!~");
					auc_name = tmp_val[0];
					auc_phone = tmp_val[1];
				}	
			}
			for (x = numbers; x >= 0; x--) {
				doc[y].options[x] = null;
			}		
			
			if (id_arr.length > 0) {			
				for (x = 0; x < id_arr.length; x++) {
					doc[y].options[x] = new Option();
					doc[y].options[x].text = id_arr1[x];
					doc[y].options[x].value = id_arr[x];
				}
			}
			
			if (auc_name) {
				doc.AD_name.value = auc_name;
				doc.AD_phone.value = auc_phone;				
			}	
					
		}		
	}		
}
	