/*------------------------------------------------------------*/
/* Globally useful methods */

var fade_time_out = 5000;

function toMoney(cents, format) {
  if(format) {
    return format.gsub(/\{\{\s*(\w+)\s*\}\}/, function(element) {
      switch(element[1]) {
      case 'amount':
        return floatToString(cents/100.0, 2);
      case 'amount_no_decimals':
        return floatToString(cents/100.0, 0);
      case 'currency':
        return '';      
      }
    });
  }
  else {
    return floatToString(cents/100.0, 2);
  }
}

function floatToString(numeric, decimals) {  
  var amount = numeric.toFixed(decimals).toString();
  
  if(amount.match(/^\.\d+/)) 
    return "0"+amount;
  else 
    return amount;
}

/*------------------------------------------------------------*/

jQuery.fn.confirm = function(text) {
   if(text == null || text == '') text = "Are you sure you wish to continue? Click OK to confirm.";
   if(confirm(text)){
   	return true;
   }
   return false;
 };
 
 jQuery.fn.selectedValue = function() {
 	var s = "";
 	var options = this.get(0).options;
 	for(var i=0; i<options.length; i++){
		if(options[i].selected){
			s = options[i].value;
		}
	}
	return s;
 };
 
 jQuery.fn.selectedText = function() {
 	var s = "";
 	var options = this.get(0).options;
	for(var i=0; i<options.length; i++){
		if(options[i].selected){
			s = options[i].text;
		}
	}
	return s;
 };
 
 jQuery.fn.showfloat = function(text) {
 	//display on the top of current screen
 	//var docHeight = document.body.clientHeight;
 	//var divHeight = 30;
 	var scrollTop = document.documentElement.scrollTop;
 	var ch = scrollTop;
 	
 	this.get(0).innerHTML = text;
	this.get(0).style.top = ch+"px";
	this.show();
 	
	return true;
 };
 
 
 $(document).ready(function() {
	
	/////////////////////////////////////////////
	//email
	/////////////////////////////////////////////
	var options = {beforeSubmit: emailFormValidate, success: emailFormResponse}; 
 
    $("#frmContact").ajaxForm(options);
    
    function emailFormValidate(formData, jqForm, options) { 
    	var form = jqForm[0]; 
    	var name = $("#txtName").attr("value");
    	var email = $("#txtEmail").attr("value");
    	var content = $("#txtContent").attr("value");
    	
    	if(name == null || name == ""){
    		alert("Please provide us your name!")
    		return false;
    	}
    	
    	if(email == null || email == ""){
    		alert("Please provide us your email address!")
    		return false;
    	}
    	
    	if(content == null || content == ""){
    		alert("Please provide us your content!")
    		return false;
    	}
    	
    	$("#frmContact").block("<div class=\"blockUI\">Processing...</div>"); 
		return true; 
	} 

	function emailFormResponse(responseText, statusText)  { 
		$("#frmContact").unblock();
		alert(responseText);
	}
	
});
 
 
 
 
 
function validate_fields(theForm)
        {
                var strMessage, blnEmpty;
                strMessage = "Please enter the following fields: \n";
                blnEmpty = false;
		
if(theForm.txtCompanyName.value == null || theForm.txtCompanyName.value == "")
							{ blnEmpty = true; strMessage = strMessage + " - Company Name\n"; }
				
                if(theForm.cboQuoteFor.selectedIndex!= 2 && theForm.cboQuoteFor.selectedIndex!=3){
												
					if(theForm.txtNumberOfEmployees.value == null || theForm.txtNumberOfEmployees.value == "")
							{ blnEmpty = true; strMessage = strMessage + " - Number of Employees\n"; }
				}
				
                if(theForm.txtContactPerson.value == null || theForm.txtContactPerson.value == "")
                        { blnEmpty = true; strMessage = strMessage + " - Contact Person's Name\n"; }
						
                if(theForm.txtAddress.value == null || theForm.txtAddress.value == "")
                        { blnEmpty = true; strMessage = strMessage + " - Address\n"; }
						
                if(theForm.txtPhone.value == null || theForm.txtPhone.value == "")
                        { blnEmpty = true; strMessage = strMessage + " - Phone Number\n"; }
						
                if(theForm.txtEmail.value == null || theForm.txtEmail.value == "" || !(isEmailAddr(theForm.txtEmail.value)))
                        { blnEmpty = true; strMessage = strMessage + " - Valid Email Address\n"; }
						
                if(theForm.txtNotes.value == null || theForm.txtNotes.value == "")
                        { blnEmpty = true; strMessage = strMessage + " - Your Message Notes\n"; }


                if(blnEmpty)
                {  alert(strMessage); return (false); }
                else
                {  return (true); }
        }
		



function isEmailAddr(email)
{
	var result = false
	var theStr = new String(email)
	var index = theStr.indexOf("@");
	if (index > 0)
	{
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
		result = true;
	}
	return result;
}
 

