
function requiredInputWarning (inputId) {
	var inputItem = document.getElementById(inputId);
	if (inputItem.value != '') {
		inputItem.className='reqcomplete';
	} else {
		inputItem.className='reqincomplete';
	}
}

function changeListingFormAction (button) {
	if (button == 'SavePictures') {
		var formItem = document.getElementById('next_action');
		formItem.value = 'upload_pictures';
	} else if (button == 'SaveCheckout') {
		var formItem = document.getElementById('next_action');
		formItem.value = 'checkout';
	} else if (button == 'Update') {
		formItem = document.getElementById('update_house');
		formItem.action = '/Listing/update/';
	} else {
		inputItem = document.getElementById('edit_mode');
		if (inputItem.value == 0) {
			formItem = document.getElementById('add_house');
			formItem.action = '/Listing/add/';
		} else {
			inputItem2 = document.getElementById('listing_id');
			formItem = document.getElementById('update_house');
			formItem.action = '/Listing/editshow/?listing_id='+inputItem2.value;
                }
	}
}

function checkform (form) 
{
	var is_land = form.land_listing.checked || form.land_listing.value;

	
	if (form.first_name.value == "")
	{
		alert( "Please enter first name." );
		form.first_name.focus();
		return false;
	}
	else if (form.last_name.value == "") 
	{
		alert( "Please enter last name." );
		form.last_name.focus();
		return false;
	}
	else if (form.phone.value == "") 
	{
		alert( "Please enter a complete phone number." );
		form.phone.focus();
		return false;
	}
	else if (form.location.value == "" && is_land) 
	{
		alert( "Please enter a valid property location." );
		form.location.focus();
		return false;
	}
	else if (form.utilities.value == "" && is_land) 
	{
		alert( "Please enter a valid utilities description." );
		form.utilities.focus();
		return false;
	}
	else if (form.address.value == "" && !is_land) 
	{
		alert( "Please enter a valid street address." );
		form.address.focus();
		return false;
	}
	else if (form.city.value == "" && !is_land) 
	{
		alert( "Please select a city." );
		form.city.focus();
		return false;
	} 
	else if (form.property_style.value == "" && !is_land) 
	{
		alert( "Please select a property style." );
		form.property_style.focus();
		return false;
	}
	else if (form.sale_price.value == "") 
	{
		alert( "Please enter expected sale price." );
		form.sale_price.focus();
      return false;
   } 
	else 
	{
   	return true;
   }
}

function calculate_total () {
	var service_num = document.service_form.service_num.value;
	var total = 0; 
	var qty = 0;
	for (i=1;i<=service_num+1;i++) {
		var elmQtyId = "qty_" + i; 
		var elmFeeId = "fee_" + i; 
		var elmGstId = "gst_" + i; 
		var elmPstId = "pst_" + i; 
		var elmQty = document.getElementById(elmQtyId);
		var elmFee = document.getElementById(elmFeeId);
		var elmGst = document.getElementById(elmGstId);
		var elmPst = document.getElementById(elmPstId);
		if (elmQty != null) {
			if (elmQty.type == 'checkbox') {
				qty = (elmQty.checked == true)?1:0;
			} else {
				qty = elmQty.value;
			}

			total = total + qty * elmFee.value * (1+elmGst.value/100+elmPst.value/100); 
		}
	}
	document.service_form.total.value = round_decimals(total,2);
}

function round_decimals (original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals);
    var result2 = Math.round(result1);
    var result3 = result2 / Math.pow(10, decimals);
    return pad_with_zeros(result3, decimals);
}

function pad_with_zeros (rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()
    
    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {
        
        // If no, then all decimal places will be padded with 0s
        decimal_part_length = 0
        
        // If decimal_places is greater than zero, tack on a decimal point
        value_string += decimal_places > 0 ? "." : ""
    }
    else {

        // If yes, then only the extra decimal places will be padded with 0s
        decimal_part_length = value_string.length - decimal_location - 1
    }
    
    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length
    
    if (pad_total > 0) {
        
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    return value_string
}

function changeFormAction (buttonId) {
	var listingId = document.service_form.listing_id.value;
	if (buttonId == 'prev' || buttonId == 'next') {
		var year = document.service_form.year.value;
		var month = document.service_form.month.value;
		var currentYear = document.service_form.current_year.value;
		var currentMonth = document.service_form.current_mon.value;

		document.service_form.prev_year.value = currentYear;
		document.service_form.prev_mon.value = currentMonth;

		if (buttonId == 'prev') {
			if (currentYear == year) {
				if (currentMonth > month) {
					currentMonth = currentMonth - 1;
				} else {
					currentMonth = month;
				}
			} else if (currentYear > year) {
				if (currentMonth == 1) {
					currentYear = currentYear - 1;
					currentMonth = 12;
				} else {
					currentMonth = currentMonth - 1;
				}
			} else {
				currentYear = year;
				currentMonth = month;
			}
		}

		if (buttonId == 'next') {
			if (currentMonth == 12) {
				currentMonth = 1;
				currentYear = parseInt(currentYear) + 1;
			} else {
				currentMonth = parseInt(currentMonth) + 1;
			}
		}

		document.service_form.current_year.value = currentYear;
		document.service_form.current_mon.value = currentMonth;
		document.service_form.action = '/Listing/service/?listing_id='+listingId+'&f_mode=new';
	}
}

function my_confirm_frm (msg, f) {
	var agree = confirm(msg);
	if (agree) {
		f.submit();
		return true;
	} else {
		return false;
	}
}

function email_check (elem) {

	var str=elem.value;
	lFocus = elem;
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		lFocus.focus();
		alert("Invalid E-mail ID")
		return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		lFocus.focus();
		alert("Invalid E-mail ID")
		return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		lFocus.focus();
		alert("Invalid E-mail ID")
		return false
	}

	if (str.indexOf(at,(lat+1))!=-1){
		lFocus.focus();
		alert("Invalid E-mail ID")
		return false
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		lFocus.focus();
		alert("Invalid E-mail ID")
		return false
	}

	if (str.indexOf(dot,(lat+2))==-1){
		lFocus.focus();
		alert("Invalid E-mail ID")
		return false
	}

	if (str.indexOf(" ")!=-1){
		lFocus.focus();
		alert("Invalid E-mail ID")
		return false
	}

	return true
}


