var Emphunter = Class.create();
//var total_srv_selected	=	0.00;


Emphunter.prototype = {
 	
 	initialize : function() {
   		jQuery.noConflict();
   		
   		this.total_srv_selected	=	"0.00";
   		
   		this.total_arr			= new Object();
   		
   		
 	},
 	
 	show_price_service: function(url){
		//alert(url);
		jQuery.ajax({
			  url:url,
			  dataType:"html",	
			  type: "GET",
			  cache: false,
			  beforeSend: function(data){
				jQuery("#service_price").html("<img src='/images/loading.gif' align='absmiddle'/> loading please wait...");
			  },
			  success: function(data){
				 jQuery("#service_price").html(data);
			  }
		});
		
	},
	
	
	show_price_service_modal: function(url){
		//alert(url);
		jQuery.ajax({
			  url:url,
			  dataType:"html",	
			  type: "GET",
			  cache: false,
			  beforeSend: function(data){
				jQuery.blockUI({message:"Loading, Please. wait...", overlayCSS:{opacity:'0.0'}, css:{color:'#FFF',padding:'15px',backgroundColor: '#999999'}}); 
			  },
			  success: function(data){
				 jQuery.unblockUI();
				 jQuery("#service_price").html(data);
			  }
		});
		
	},
	
	
	save: function(url){
		
		jQuery.ajax({
				  url:url,
				  dataType:"html",	
				  type: "POST",
				  data: Form.serialize('frmsave'),
				  cache: false,
				  beforeSend: function(data){
						jQuery.blockUI({message:"Loading, Please. wait...", overlayCSS:{opacity:'0.0'}, css:{color:'#FFF',padding:'15px',backgroundColor: '#999999'}}); 
				  },
				  success: function(data){
				  		
				  }
			});
		
	},
	
	addnewrate: function(elem){
		
		var tyme = Math.round(new Date().getTime()/1000);
		var rate_id = "rate_"+tyme;
		var on_click = "jQuery('#"+rate_id+"').remove()";
		
		jQuery("#rate_grid_container div:last").clone(true).insertAfter("#rate_grid_container div:last");
		jQuery("#rate_grid_container span:last").html('<a href="javascript:void(0)" onclick="'+on_click+'"><img alt="Delete" title="Delete" src="/sfPropelPlugin/images/delete.png" align="absmiddle"/></a>');
		jQuery("#rate_grid_container div:last").attr({'id':rate_id});
	},
	
	addnewoption: function(elem){
		
		var tyme = Math.round(new Date().getTime()/1000);
		var option_id = "option_"+tyme;
		var on_click = "jQuery('#"+option_id+"').remove()";
		
		jQuery("#option_grid_container div:last").clone(true).insertAfter("#option_grid_container div:last");
		jQuery("#option_grid_container span:last").html('<a href="javascript:void(0)" onclick="'+on_click+'"><img alt="Delete" title="Delete" src="/sfPropelPlugin/images/delete.png" align="absmiddle"/></a>');
		jQuery("#option_grid_container div:last").attr({'id':option_id});
	},
	
	add_Invoice_item: function(){
		
		var cnt = jQuery("joel").size();
		
		var tyme = Math.round(new Date().getTime()/1000);
		//var rate_id = "rate_"+tyme;
		var rate_id	=	"item_"+parseInt(cnt + 1);
		var on_click = "jQuery('#"+rate_id+"').remove()";
		var qty_id	=	"qty_"+parseInt(cnt + 1);
		var price_id	=	"price_"+parseInt(cnt + 1);
		var amount_id	=	"amount_"+parseInt(cnt + 1);
		
		
		jQuery("#invoice_grid_container div:last").clone(true).insertAfter("#invoice_grid_container div:last");
		jQuery("#invoice_grid_container span:last").html('<a href="javascript:void(0)" onclick="'+on_click+'"><img alt="Delete" title="Delete" src="/sfPropelPlugin/images/delete.png" align="absmiddle"/></a>');
		jQuery("#invoice_grid_container div:last").attr({'id':rate_id});
		
		
		jQuery("#invoice_grid_container #qty_"+cnt+":last").attr({'id':qty_id});
		jQuery("#invoice_grid_container #price_"+cnt+":last").attr({'id':price_id});
		jQuery("#invoice_grid_container #amount_"+cnt+":last").attr({'id':amount_id});
		
		
		/*
	    jQuery("#qty_1").unbind('keypress', function(){})
	                  
	    
		
		jQuery("#"+qty_id).bind("keypress", function(e){
		       var c_ = String.fromCharCode(e.which);
		       
		       if(IsNumeric(c_)){
		      		var cnt_	= jQuery("#"+qty_id).val()+c_;
		      		if(cnt_.length<6){
		      			var amt_	=	cnt_*jQuery("#"+price_id).val();
		      			jQuery("#"+amount_id).html(parseFloat(Number(amt_).toFixed(2)));
		      			//alert(parseFloat(Number(amt_).toFixed(2)))
		      		}
		      	}
		          	
			  
		});
		*/
		
	},
	
	
	show_price_rate_service: function(pricetype){
		//alert(url);
		jQuery.ajax({
			  url:'/admin/services/selectrate/type/'+pricetype,
			  dataType:"html",	
			  type: "GET",
			  cache: true,
			  beforeSend: function(data){
				jQuery("#rates").slideUp("slow");
				jQuery("#rates").html("<img src='/images/loading.gif' align='absmiddle'/> loading please wait...");
			  
			  },
			  success: function(data){
				 //jQuery("#rates").html(data);
				 jQuery("#rates").slideDown("slow").html(data);
				 
				 
			  }
		});
		
	},
	
	
	save_new_user_jobservice: function(url){
		
		jQuery.ajax({
				  url:url,
				  dataType:"html",	
				  type: "POST",
				  data: Form.serialize('frmsave'),
				  cache: false,
				  beforeSend: function(data){
						jQuery.blockUI({message:"Loading, Please. wait...", overlayCSS:{opacity:'0.0'}, css:{color:'#FFF',padding:'15px',backgroundColor: '#999999'}}); 
				  },
				  success: function(data){
				  		//jQuery.blockUI({message:data, css:{color:'#FFF',padding:'15px',backgroundColor: '#999999'}}); 
				  		jQuery.unblockUI();
				  		jQuery("#service_price").html(data);
				  }
			});
		
	},
	
	
	ShowCVcheckByNum: function(cvnum){
		
		if(cvnum !== "none"){
			//alert(url);
			jQuery.ajax({
				  url:'/my_account/showcvcheckbynum/'+cvnum,
				  dataType:"html",	
				  type: "GET",
				  cache: false,
				  beforeSend: function(data){
					jQuery("#num_for_check").html("<img src='/images/loading.gif' align='absmiddle'/> loading please wait...");
				  },
				  success: function(data){
					 jQuery("#num_for_check").html(data);
				  }
			});
		}
	},
	
	showJob_sub_details: function(url){
			
			jQuery.ajax({
				  url:url,
				  dataType:"html",	
				  type: "GET",
				  cache: false,
				  beforeSend: function(data){
					jQuery("#job_sub_detail").html("<img src='/images/loading.gif' align='absmiddle'/> loading please wait...");
				  },
				  success: function(data){
					 jQuery("#job_sub_detail").html(data);
				  }
			});
				
	},
	
	is_attach: function(elem){
	
			if(elem.checked == true){
				jQuery("#attach_layer").css({display:'block'});
			}else{
				jQuery("#attach_layer").css({display:'none'});
			}
			
	},
	
	
	showBillingDetails: function(url){
			
			jQuery.ajax({
				  url:url,
				  dataType:"html",	
				  type: "GET",
				  cache: false,
				  beforeSend: function(data){
					jQuery("#billing").html("<img src='/images/loading.gif' align='absmiddle'/> loading please wait...");
				  },
				  success: function(data){
					 jQuery("#billing").html(data);
				  }
			});
				
	},
	
	calculate_invoice: function(url){
		
		jQuery.ajax({
				  url:url,
				  dataType:"html",	
				  type: "POST",
				  data: Form.serialize('frmsave'),
				  cache: false,
				  beforeSend: function(data){
						jQuery.blockUI({message:"Loading, Please. wait...", overlayCSS:{opacity:'0.0'}, css:{color:'#FFF',padding:'15px',backgroundColor: '#999999'}}); 
				  },
				  success: function(data){
				  		jQuery.unblockUI();
				  		jQuery("#line_items").html(data);
				  }
			});
		
	},
	
	modal_edit: function(url){
		//alert(url);
		jQuery.ajax({
			  url:url,
			  dataType:"html",	
			  type: "GET",
			  cache: false,
			  beforeSend: function(data){
				jQuery.blockUI({message:"Loading, Please. wait...", overlayCSS:{opacity:'0.0'}, css:{color:'#FFF',padding:'15px',backgroundColor: '#999999'}});
			  },
			  success: function(data){
				 jQuery.blockUI({message:data, overlayCSS:{opacity:'0.0'}});
			  }
		});
		
	},
	
	modal_edit_post: function(url,elem){
		//alert(url);
		jQuery.ajax({
			  url:url,
			  dataType:"html",	
			  type: "POST",
			  data: Form.serialize('frmsave'),
			  cache: false,
			  beforeSend: function(data){
				jQuery.blockUI({message:"Loading, Please. wait...", overlayCSS:{opacity:'0.0'}, css:{color:'#FFF',padding:'15px',backgroundColor: '#999999'}});
			  },
			  success: function(data){
				 jQuery.unblockUI();
				 jQuery("#"+elem).html(data);
			  }
		});
		
	},
	
	modal_delete: function(url){
		//alert(url);
		jQuery.ajax({
			  url:url,
			  dataType:"html",	
			  type: "GET",
			  cache: false,
			  beforeSend: function(data){
				jQuery.blockUI({message:"Loading, Please. wait...", overlayCSS:{opacity:'0.0'}, css:{color:'#FFF',padding:'15px',backgroundColor: '#999999'}});
			  },
			  success: function(data){
				 jQuery.blockUI({message:data});
			  }
		});
		
	},
	
	show_service_option: function(elem,layer,srv_id, srv_option_price,pricing_type){
	
			if(elem.checked == true){
				jQuery("#"+layer).show('slow');
				if(pricing_type == "PerHour"){	
					//jQuery('#row_total_'+srv_id).val(roundNumber(srv_option_price,2));
					//this.total_srv_selected	=	parseFloat(this.total_srv_selected) + parseFloat(roundNumber(srv_option_price,2));
				}
			}else{
				jQuery("#"+layer).hide('slow');
				jQuery('#row_total_'+srv_id).val(0);
				
				var opt_val = 'service_option_'+srv_id+'[]';
				var chc = document.forms['frmsave'][opt_val];
				for(var i=0; i<chc.length; i++){
					chc[i].checked = false;
				}
							    
				if(pricing_type == "PerHour"){
					this.total_srv_selected	=	parseFloat(this.total_srv_selected) - parseFloat(jQuery('#row_total_'+srv_id).val());
					
					jQuery('#srv_rate_'+srv_id).val("none");
					jQuery('#last_rate_row_'+srv_id).val(0);
					jQuery('#last_hour_row_'+srv_id).val(0);
					jQuery('#hours_'+srv_id).val("none");
				}
			}
			
			
			
			this.total_arr['srv_'+srv_id] = jQuery('#row_total_'+srv_id).val();
			var _total	= "0.00";			
		    jQuery.each(this.total_arr, function(i, val) {
		      	_total =  parseFloat(_total)+parseFloat(val);
		    });
			
			jQuery('#total_service_applied').html(roundNumber(_total,2));
			
	},
	
		
	calculate_total_byhour_rate: function(price){
			
			var price_ = explode("-", price);
			
			var	srv_id				= price_[0];
			//var srv_option_price	= price_[2];
			var srv_option_price	= 0.00;
			
			if(srv_id !== "none")
			{
				var price_rate 			= price_[1];
				var row_total 			= roundNumber(parseFloat(price_rate)+parseFloat(srv_option_price),2);
				
				jQuery('#last_rate_row_'+srv_id).val(price_rate);
				
				if(jQuery('#last_hour_row_'+srv_id).val()!=='0'){
					var row_total = roundNumber(parseFloat(row_total)*parseFloat(jQuery('#last_hour_row_'+srv_id).val()),2);
					jQuery('#row_total_'+srv_id).val(row_total);
				}else{
					jQuery('#row_total_'+srv_id).val(row_total);
				}
				
				this.total_srv_selected	=	parseFloat(this.total_srv_selected) + parseFloat(price_rate);
				
			}else{
				var	srv_id				= price_[1];
				jQuery('#row_total_'+srv_id).val(roundNumber(srv_option_price,2));
				jQuery('#last_rate_row_'+srv_id).val(0);
				
				if(jQuery('#last_hour_row_'+srv_id).val()!=='0'){
					var row_total = roundNumber(parseFloat(0)+parseFloat(srv_option_price),2);
					var row_total = roundNumber(parseFloat(row_total)*parseFloat(jQuery('#last_hour_row_'+srv_id).val()),2);
					jQuery('#row_total_'+srv_id).val(row_total);
				}
				
				this.total_srv_selected	=	parseFloat(this.total_srv_selected) + parseFloat(srv_option_price);
				
			}
			
			this.total_arr['srv_'+srv_id] = jQuery('#row_total_'+srv_id).val();
			var _total	= "0.00";			
		    jQuery.each(this.total_arr, function(i, val) {
		      	_total =  parseFloat(_total)+parseFloat(val);
		    });
			
			jQuery('#total_service_applied').html(roundNumber(_total,2));
			
	},
	
	calculate_total_byhour_number: function(hour){//(hour_number,service_id,service_option_price)
			
			var hour_ = explode("-", hour);
			
			var	hour_number			= hour_[0];
			var srv_id				= hour_[1];
			//var srv_option_price	= hour_[2];
			var srv_option_price	= 0.00;
			
			
			if(hour_number !== "none")
			{
				var row_total = roundNumber(parseFloat(jQuery('#last_rate_row_'+srv_id).val())+parseFloat(srv_option_price),2);
				var row_total = roundNumber(parseFloat(row_total)*parseFloat(hour_number),2);
				
				jQuery('#row_total_'+srv_id).val(row_total);
				jQuery('#last_hour_row_'+srv_id).val(hour_number);
				
				this.total_srv_selected	=	parseFloat(this.total_srv_selected) + parseFloat(row_total);
				
			}else{
				var row_total = roundNumber(parseFloat(jQuery('#last_rate_row_'+srv_id).val())+parseFloat(srv_option_price),2);
				jQuery('#row_total_'+srv_id).val(row_total);
				jQuery('#last_hour_row_'+srv_id).val(0);
				
				this.total_srv_selected	=	parseFloat(this.total_srv_selected) + parseFloat(row_total);
			}	
			
			this.total_arr['srv_'+srv_id] = jQuery('#row_total_'+srv_id).val();
			var _total	= "0.00";			
		    jQuery.each(this.total_arr, function(i, val) {
		      	_total =  parseFloat(_total)+parseFloat(val);
		    });
			
			jQuery('#total_service_applied').html(roundNumber(_total,2));
			
	},
	
	
	calculate_total_byoption_per_item: function(elem,srv_id, price){
	
			if(elem.checked == true){
				var row_total	= roundNumber(parseFloat(jQuery('#row_total_'+srv_id).val()),2)+roundNumber(price,2); 
				jQuery('#row_total_'+srv_id).val(roundNumber(row_total,2));
			}else{
				var row_total	= roundNumber(parseFloat(jQuery('#row_total_'+srv_id).val()),2) - roundNumber(price,2); 
				jQuery('#row_total_'+srv_id).val(roundNumber(row_total,2));
			}
			
			this.total_arr['srv_'+srv_id] = row_total;
			var _total	= "0.00";			
		    jQuery.each(this.total_arr, function(i, val) {
		      	_total =  parseFloat(_total)+parseFloat(val);
		    });
			
			jQuery('#total_service_applied').html(roundNumber(_total,2));
			
	},
	
	user_new_service: function(url){
		
		jQuery.ajax({
				  url:url,
				  dataType:"html",	
				  type: "POST",
				  data: Form.serialize('frmsave'),
				  cache: false,
				  beforeSend: function(data){
						jQuery.blockUI({message:"Loading, Please. wait...", overlayCSS:{opacity:'0.0'}, css:{color:'#FFF',padding:'15px',backgroundColor: '#999999'}}); 
				  },
				  success: function(data){
				  		jQuery('#service_option').hide('slow').show('slow').html(data);
				  		jQuery.unblockUI();
				  }
			});
		
	},
	
	
	user_edit_service: function(url){
		
		jQuery.ajax({
				  url:url,
				  dataType:"html",	
				  type: "POST",
				  data: Form.serialize('frmsave'),
				  cache: false,
				  beforeSend: function(data){
						jQuery.blockUI({message:"Loading, Please. wait...", overlayCSS:{opacity:'0.0'}, css:{color:'#FFF',padding:'15px',backgroundColor: '#999999'}}); 
				  },
				  success: function(data){
				  		jQuery('#editcontainer').html(data);
				  		jQuery.unblockUI();
				  }
			});
		
	},
	
	agree_terms: function(){
		var chk = jQuery("#frmsave [id=terms]");
		chk[0].checked = true;		
	}
	
	
 	
}


var Emphunter = new Emphunter();




function explode( delimiter, string, limit ) {
    var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2
        || typeof arguments[0] == 'undefined'
        || typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === ''
        || delimiter === false
        || delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function'
        || typeof delimiter == 'object'
        || typeof string == 'function'
        || typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}







function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}


function roundNumber(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}


sfHover = function() {
	var sfEls = document.getElementById("menu").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);



var checked=false;
function checkedAll() {
		var c = document.getElementById("del_container");
		var checks = c.getElementsByTagName("input");
		 if (checked == false){
			   checked = true
		 }else{
			  checked = false
		 }
		for (var i =0; i < checks.length; i++){
			 checks[i].checked = checked;
		}
 }
 
 function deleteRows(m){
		var c = document.getElementById("del_container");
		var checks = c.getElementsByTagName("input");
		var ids = '';
		
		for(var i= 0; i < checks.length; i++){
			if(checks[i].checked){
				var indx = checks[i].value;
				if (i > 0){
					ids += ',';
				}
				ids += indx;
			}
		}
		
		if(ids !== ""){
			
			if(m){
				var mes = "Do you want to Delete these items permanently?";
			}else{
				var mes = "Do you want to move these items to your trash folder?";
			}
			
			if(confirm(mes)){	
				return true;
			}else{
				return false;
			}
		}else{
			alert('Please select item!');
			return false;
		}
		
}	

