// JavaScript Document
//Object.dpDump(theobject);  Dumps objects, arrays, etc to a pop up window 

function goCustomerHome(url) {
	location.href = url;
};//close function
		
function chk(ob,num) {
	var isCheck = ob.checked;
  var d=document.getElementsByTagName("input");
  for(var i=0;i<d.length;i++) {
		if (num == ob.name) {
    	// Master box was checked
    	if(d[i].name.indexOf((num))!=-1) d[i].checked=isCheck;
       } 
			else {
      	// Sub- box was checked
       	if(d[i].name==num) d[i].checked=false;
  		}
		}
}; //close function

function saveCustomer(newCustomer){ 

	var method= '';
	var signUpValue='';

	if(document.getElementById('receivePDF').checked==true ) {
		getsPDF = 1;
	}
	else {
		getsPDF = 0;
	}
	
	
	signUpElement = document.getElementById('signUpDate');
	
	
	if (signUpElement!=null) {
		signUpValue = signUpElement.value;
	}

	/*
	if (document.getElementById('signUpDate')!=null){
		signUpDate = document.getElementById('signUpDate').value;
	} else {
		signUpDate = '';
	}*/
	
	// store the form data in an object
	var formData = {
		customerId: document.getElementById('customerId').value, 
		firstName: document.getElementById('fName').value, 
		lastName: document.getElementById('lName').value,
		email: document.getElementById('emailAddr').value,
		phone: document.getElementById('phone').value,
		password: document.getElementById('password').value,
		companyName: document.getElementById('companyName').value,
		addrLine1: document.getElementById('addrline1').value,
		addrLine2: document.getElementById('addrline2').value,
		city: document.getElementById('city').value,
		//state: document.getElementById('state').value,
		zip: document.getElementById('zip').value,
		stateId: document.getElementById('state').value,
		 signUpDate: signUpValue,
		//termDate: document.getElementById('termDate').value,
		receivePDF: getsPDF
		
	}
		method = (formData.customerId == "") ? ('addCustomer') : ('updateCustomer');
	// Object.dpDump(formData);
	// make the ajaxCFC request
	http('POST','/model/ajaxFacade.cfc?method=' + method , resultHandler, formData); 

	function resultHandler(obj) {
		
		if (obj.customerid!=0) {
			//document.getElementById('msg').innerHTML = "Customer Saved.";		
			window.location.href = 'index.cfm?fuseaction=customer.detail&customerId=' + obj.customerid;
		} else {
			if (obj.msg) alert(obj.msg);	
		}
		
	}

}; // close saveCustomer function
	 
function deleteCustomer(customerId,fullname) {
	if ( confirm('Do you really want to delete ' + fullname + '?') ) {
		// make the ajaxCFC request
		http('POST','/model/ajaxFacade.cfc?method=deleteCustomer',customerDeleteHandler,customerId); 
	}
  else {
  	return false;
  }
  
	function customerDeleteHandler(obj) {
		document.getElementById('msg').innerHTML = obj;
	}
}; // close deleteCustomer function
	
function showBillingForm (rowNum,theStyle,billingId) {
	var billingParams = {billingId: billingId}
	if (theStyle=='none') {
		document.getElementById("frmBillingProfile_" + rowNum).style.display='block';
		// make the ajaxCFC request
		http('POST','/model/ajaxFacade.cfc?method=getBillingProfile', billingFormResultHandler,billingParams); 
	}
	else {
		document.getElementById("frmBillingProfile_" + rowNum).style.display='none';
	}
	// populates billing profile form 
	 function billingFormResultHandler(obj) {
	 	var checkIt = false;
	 	var billingId = obj.billingid;
		if (obj.isdefault == 1) {
			var checkIt = true;
		}
		 	document.getElementById("paymentTypeId_" + billingId).selectedIndex = obj.paymenttypeid - 1;

		//	document.getElementById("paymentTypeId_" + billingId).options[obj.paymentTypeId - 1].selected = true;			
			
		 	document.getElementById("contact_" + billingId).value = obj.contact;
		 	document.getElementById("company_" + billingId).value = obj.company;
			document.getElementById("defaultProfile_" + billingId).checked = checkIt;
		 	document.getElementById("description_" + billingId).value = obj.description;
		 	document.getElementById("address1_" + billingId).value = obj.address1;
		 	document.getElementById("address2_" + billingId).value = obj.address2;
			document.getElementById("city_" + billingId).value = obj.city;
		 	document.getElementById("stateId_" + billingId).value = obj.stateid;
		 	document.getElementById("zip_" + billingId).value = obj.zip;
			document.getElementById("country_" + billingId).value = obj.country;
			document.getElementById("email_" + billingId).value = obj.email;
			document.getElementById("phone_" + billingId).value = obj.phone;
			document.getElementById("ext_" + billingId).value = obj.ext;
			document.getElementById("altphone_" + billingId).value = obj.altphone;
			document.getElementById("altext_" + billingId).value = obj.altext;
			document.getElementById("fax_" + billingId).value = obj.fax;
			document.getElementById("cc_name_on_" + billingId).value = obj.ccname;
			document.getElementById("cc_number_" + billingId).value = obj.ccnumber;
			document.getElementById("cvv_" + billingId).value = obj.cvv;
			document.getElementById("cc_exp_mo_" + billingId).value = obj.ccexpmo;
			document.getElementById("cc_exp_yr_" + billingId).value = obj.ccexpyr;
	 }
}; // close showBillingForm

function saveBillingProfile(billingId){ 

		if ( document.getElementById('defaultProfile_' + billingId).checked ) {
			document.getElementById('defaultProfile_' + billingId).value = 1;
		}
		else {
			document.getElementById('defaultProfile_' + billingId).value = 0;
		}
	// store the form data in an object
	var billingProfileFormData = {
		billingId: billingId, 
		customerid: document.getElementById("customerId").value,
		description: document.getElementById("description_" + billingId).value,
		company: document.getElementById("company_" + billingId).value,
		address1: document.getElementById("address1_" + billingId).value,
		address2: document.getElementById("address2_" + billingId).value,
		city: document.getElementById("city_" + billingId).value,
		stateId: document.getElementById("stateId_" + billingId).value,
		zip: document.getElementById("zip_" + billingId).value,
		country: document.getElementById("country_" + billingId).value,
		email: document.getElementById("email_" + billingId).value,
		fax: document.getElementById("fax_" + billingId).value,
		ccname: document.getElementById("cc_name_on_" + billingId).value,
		ccnumber: document.getElementById("cc_number_" + billingId).value,
		cvv: document.getElementById("cvv_" + billingId).value,
		ccexpmo: document.getElementById("cc_exp_mo_" + billingId).value,
		ccexpyr: document.getElementById("cc_exp_yr_" + billingId).value,
		phone: document.getElementById('phone_' + billingId).value,
		paymentTypeId: document.getElementById('paymentTypeId_' + billingId).value,
		contact: document.getElementById('contact_' + billingId).value,
		ext: document.getElementById('ext_' + billingId).value,
		altphone: document.getElementById('altphone_' + billingId).value,
		altext: document.getElementById('altext_' + billingId).value,
		isdefault: document.getElementById('defaultProfile_' + billingId).value
	}
	// make the ajaxCFC request
	 http('POST','/model/ajaxFacade.cfc?method=saveBillingProfile', saveBillingHandler, billingProfileFormData); 
	 
	 function saveBillingHandler(obj) {
	 document.getElementById('billingProfileAdded').innerHTML = obj;
	
	 }
}; // close saveBillingProfile function

function showNewBillingForm() {
	var myDiv = document.getElementById('newBillingProfile');
	var myBtn = document.getElementById('btnAddBillingProfile');
	if (myDiv.style.display == "none") {
			myDiv.style.display = "block";
			myBtn.value = 'Cancel';
	}
	else {
			clearNewBillingForm(myDiv,myBtn);
	}
}; // close showNewBillingForm function
				
function clearNewBillingForm (myDiv,myBtn) {
	myDiv.style.display = "none";
	myBtn.value = 'Add Profile';
	document.getElementById("paymentTypeId_new").value = "";
	document.getElementById("contact_new").value = "";
	document.getElementById("company_new").value = "";
	document.getElementById("defaultProfile_new").value = "";
	document.getElementById("description_new").value = "";
	document.getElementById("address1_new").value = "";
	document.getElementById("address2_new").value = "";
	document.getElementById("city_new").value = "";
	document.getElementById("stateId_new").value = "";
	document.getElementById("zip_new").value = "";
	document.getElementById("country_new").value = "";
	document.getElementById("email_new").value = "";
	document.getElementById("phone_new").value = "";
	document.getElementById("ext_new").value = "";
	document.getElementById("altphone_new").value = "";
	document.getElementById("altext_new").value = "";
	document.getElementById("fax_new").value = "";
	document.getElementById("cc_name_on_new").value = "";
	document.getElementById("cc_number_new").value = "";
	document.getElementById("cvv_new").value = "";
	document.getElementById("cc_exp_mo_new").value = "";
	document.getElementById("cc_exp_yr_new").value = "";
	document.getElementById("defaultProfile_new").checked = false;
}; // close clearNewBillingForm function
				
function createProfile(customerId) {
	if ( document.getElementById('defaultProfile_new').checked ) {
	document.getElementById('defaultProfile_new').value = 1;
	}
	else {
	document.getElementById('defaultProfile_new').value = 0;
	}
	// store the form data in an object
	var billingProfileFormData = {
		customerId: customerId,
		description: document.getElementById("description_new").value,
		company: document.getElementById("company_new").value,
		address1: document.getElementById("address1_new").value,
		address2: document.getElementById("address2_new").value,
		city: document.getElementById("city_new").value,
		stateId: document.getElementById("stateId_new").value,
		zip: document.getElementById("zip_new").value,
		country: document.getElementById("country_new").value,
		email: document.getElementById("email_new").value,
		fax: document.getElementById("fax_new").value,
		ccname: document.getElementById("cc_name_on_new").value,
		ccnumber: document.getElementById("cc_number_new").value,
		cvv: document.getElementById("cvv_new").value,
		ccexpmo: document.getElementById("cc_exp_mo_new").value,
		ccexpyr: document.getElementById("cc_exp_yr_new").value,
		phone: document.getElementById("phone_new").value,
		paymentTypeId: document.getElementById("paymentTypeId_new").value,
		contact: document.getElementById("contact_new").value,
		ext: document.getElementById("ext_new").value,
		altphone: document.getElementById("altphone_new").value,
		altext: document.getElementById("altext_new").value,
		isdefault: document.getElementById("defaultProfile_new").value
	}

	// make the ajaxCFC request
	http('POST','/model/ajaxFacade.cfc?method=newBillingProfile', saveNewBillingProfileHandler, billingProfileFormData); 

	function saveNewBillingProfileHandler(obj) {
		document.getElementById('billingProfileAdded').innerHTML = obj;
		var myDiv = document.getElementById('newBillingProfile');
		var myBtn = document.getElementById('btnAddBillingProfile');
		clearNewBillingForm(myDiv,myBtn);
		location.reload();
	}

}; //close createProfile function

	function saveCustomerCategory (customerId, categoryId) {
	
		var params = {
		customerId: customerId,
		categoryId: categoryId
		}

		// make the ajaxCFC request
		http('POST','/model/ajaxFacade.cfc?method=saveCustomerCategory', saveCustomerCategoryResultHandler, params); 
		 
   function saveCustomerCategoryResultHandler(obj) {
	 }
	
	}; //close saveCustomerCategories function
	
	
	// move to banner.js when created TODO
	function saveBannerCategory (bannerId, categoryId) {
	
		var params = {
		bannerId: bannerId,
		categoryId: categoryId
		}

		// make the ajaxCFC request
		http('POST','/model/ajaxFacade.cfc?method=saveBannerCategory', saveBannerCategoryResultHandler, params); 
		 
   function saveBannerCategoryResultHandler(obj) {
	 }
	
	}; //close saveBannerCategory function
	 
	 
	
